color4.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Type 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 specified 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. return &Color4{c.R, c.G, c.B, alpha}
  25. }
  26. // Color4Name returns a Color4 with the specified standard web color name
  27. // and specified alpha channel.
  28. func Color4Name(name string, alpha float32) Color4 {
  29. c := mapColorNames[name]
  30. return Color4{c.R, c.G, c.B, alpha}
  31. }
  32. // Set sets this color individual R,G,B,A components
  33. func (c *Color4) Set(r, g, b, a float32) *Color4 {
  34. c.R = r
  35. c.G = g
  36. c.B = b
  37. c.A = b
  38. return c
  39. }
  40. // SetHex sets the color RGB components from the
  41. // specified integer interpreted as a color hex number
  42. // Alpha component is not modified
  43. func (c *Color4) SetHex(value uint) *Color4 {
  44. c.R = float32((value >> 16 & 255)) / 255
  45. c.G = float32((value >> 8 & 255)) / 255
  46. c.B = float32((value & 255)) / 255
  47. return c
  48. }
  49. // SetName sets the color RGB components from the
  50. // specified standard web color name
  51. func (c *Color4) SetName(name string) *Color4 {
  52. *c = Color4Name(name, 1)
  53. return c
  54. }
  55. func (c *Color4) Add(other *Color4) *Color4 {
  56. c.R += other.R
  57. c.G += other.G
  58. c.B += other.B
  59. c.A += other.A
  60. return c
  61. }
  62. func (c *Color4) MultiplyScalar(v float32) *Color4 {
  63. c.R *= v
  64. c.G *= v
  65. c.B *= v
  66. return c
  67. }
  68. // FromColor sets this Color4 fields from Color and an alpha
  69. func (c *Color4) FromColor(other *Color, alpha float32) {
  70. c.R = other.R
  71. c.G = other.G
  72. c.B = other.B
  73. c.A = alpha
  74. }
  75. // ToColor returns a Color with this Color4 RGB components
  76. func (c *Color4) ToColor() Color {
  77. return Color{c.R, c.G, c.B}
  78. }