color4.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "strings"
  7. )
  8. // Color4 describes an RGBA color
  9. type Color4 struct {
  10. R float32
  11. G float32
  12. B float32
  13. A float32
  14. }
  15. // NewColor4 creates and returns a pointer to a new Color4
  16. // with the specified standard web color name (case insensitive)
  17. // and an optional alpha channel value.
  18. // Returns nil if the specified color name not found
  19. func NewColor4(name string, alpha ...float32) *Color4 {
  20. c, ok := mapColorNames[strings.ToLower(name)]
  21. if !ok {
  22. return nil
  23. }
  24. a := float32(1)
  25. if len(alpha) > 0 {
  26. a = alpha[0]
  27. }
  28. return &Color4{c.R, c.G, c.B, a}
  29. }
  30. // Color4Name returns a Color4 with the specified standard web color name
  31. // and an optional alpha channel value.
  32. func Color4Name(name string, alpha ...float32) Color4 {
  33. c := mapColorNames[strings.ToLower(name)]
  34. a := float32(1)
  35. if len(alpha) > 0 {
  36. a = alpha[0]
  37. }
  38. return Color4{c.R, c.G, c.B, a}
  39. }
  40. // Set sets this color individual R,G,B,A components
  41. // Returns pointer to this updated color
  42. func (c *Color4) Set(r, g, b, a float32) *Color4 {
  43. c.R = r
  44. c.G = g
  45. c.B = b
  46. c.A = b
  47. return c
  48. }
  49. // SetHex sets the color RGB components from the
  50. // specified integer interpreted as a color hex number
  51. // Alpha component is not modified
  52. // Returns pointer to this updated color
  53. func (c *Color4) SetHex(value uint) *Color4 {
  54. c.R = float32((value >> 16 & 255)) / 255
  55. c.G = float32((value >> 8 & 255)) / 255
  56. c.B = float32((value & 255)) / 255
  57. return c
  58. }
  59. // SetName sets the color RGB components from the
  60. // specified standard web color name
  61. // Returns pointer to this updated color
  62. func (c *Color4) SetName(name string) *Color4 {
  63. *c = Color4Name(name, 1)
  64. return c
  65. }
  66. // Add adds to each RGBA component of this color the correspondent component of other color
  67. // Returns pointer to this updated color
  68. func (c *Color4) Add(other *Color4) *Color4 {
  69. c.R += other.R
  70. c.G += other.G
  71. c.B += other.B
  72. c.A += other.A
  73. return c
  74. }
  75. // MultiplyScalar multiplies each RGBA component of this color by the specified scalar.
  76. // Returns pointer to this updated color
  77. func (c *Color4) MultiplyScalar(v float32) *Color4 {
  78. c.R *= v
  79. c.G *= v
  80. c.B *= v
  81. c.A *= v
  82. return c
  83. }
  84. // FromColor sets this Color4 fields from Color and an alpha
  85. func (c *Color4) FromColor(other *Color, alpha float32) {
  86. c.R = other.R
  87. c.G = other.G
  88. c.B = other.B
  89. c.A = alpha
  90. }
  91. // ToColor returns a Color with this Color4 RGB components
  92. func (c *Color4) ToColor() Color {
  93. return Color{c.R, c.G, c.B}
  94. }