color4.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package math32
  5. import ()
  6. type Color4 struct {
  7. R float32
  8. G float32
  9. B float32
  10. A float32
  11. }
  12. var Black4 = Color4{0, 0, 0, 1}
  13. var White4 = Color4{1, 1, 1, 1}
  14. var Red4 = Color4{1, 0, 0, 1}
  15. var Green4 = Color4{0, 1, 0, 1}
  16. var Blue4 = Color4{0, 0, 1, 1}
  17. var Gray4 = Color4{0.5, 0.5, 0.5, 1}
  18. func NewColor4(r, g, b, a float32) *Color4 {
  19. return &Color4{R: r, G: g, B: b, A: a}
  20. }
  21. // Set sets this color individual R,G,B,A components
  22. func (c *Color4) Set(r, g, b, a float32) *Color4 {
  23. c.R = r
  24. c.G = g
  25. c.B = b
  26. c.A = b
  27. return c
  28. }
  29. // SetHex sets the color RGB components from the
  30. // specified integer interpreted as a color hex number
  31. // Alpha component is not modified
  32. func (c *Color4) SetHex(value uint) *Color4 {
  33. c.R = float32((value >> 16 & 255)) / 255
  34. c.G = float32((value >> 8 & 255)) / 255
  35. c.B = float32((value & 255)) / 255
  36. return c
  37. }
  38. // SetName sets the color RGB components from the
  39. // specified HTML color name
  40. // Alpha component is not modified
  41. func (c *Color4) SetName(name string) *Color4 {
  42. return c.SetHex(colorKeywords[name])
  43. }
  44. func (c *Color4) Add(other *Color4) *Color4 {
  45. c.R += other.R
  46. c.G += other.G
  47. c.B += other.B
  48. c.A += other.A
  49. return c
  50. }
  51. func (c *Color4) MultiplyScalar(v float32) *Color4 {
  52. c.R *= v
  53. c.G *= v
  54. c.B *= v
  55. return c
  56. }
  57. // FromColor sets this Color4 fields from Color and an alpha
  58. func (c *Color4) FromColor(other *Color, alpha float32) {
  59. c.R = other.R
  60. c.G = other.G
  61. c.B = other.B
  62. c.A = alpha
  63. }
  64. // ToColor returns a Color with this Color4 RGB components
  65. func (c *Color4) ToColor() Color {
  66. return Color{c.R, c.G, c.B}
  67. }