color.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 Color struct {
  7. R float32
  8. G float32
  9. B float32
  10. }
  11. var Black = Color{0, 0, 0}
  12. var White = Color{1, 1, 1}
  13. var Red = Color{1, 0, 0}
  14. var Green = Color{0, 1, 0}
  15. var Blue = Color{0, 0, 1}
  16. var Gray = Color{0.5, 0.5, 0.5}
  17. // NewColor creates and returns a pointer to a new color
  18. // with the specified RGB components
  19. func NewColor(r, g, b float32) *Color {
  20. return &Color{R: r, G: g, B: b}
  21. }
  22. // NewColorHex creates and returns a pointer to a new color
  23. // with its RGB components from the specified hex value
  24. func NewColorHex(color uint) *Color {
  25. return (&Color{}).SetHex(color)
  26. }
  27. // Set sets this color individual R,G,B components
  28. func (c *Color) Set(r, g, b float32) *Color {
  29. c.R = r
  30. c.G = g
  31. c.B = b
  32. return c
  33. }
  34. // SetHex sets the color RGB components from the
  35. // specified integer interpreted as a color hex number
  36. func (c *Color) SetHex(value uint) *Color {
  37. c.R = float32((value >> 16 & 255)) / 255
  38. c.G = float32((value >> 8 & 255)) / 255
  39. c.B = float32((value & 255)) / 255
  40. return c
  41. }
  42. // SetName sets the color RGB components from the
  43. // specified HTML color name
  44. func (c *Color) SetName(name string) *Color {
  45. return c.SetHex(colorKeywords[name])
  46. }
  47. func (c *Color) Add(other *Color) *Color {
  48. c.R += other.R
  49. c.G += other.G
  50. c.B += other.B
  51. return c
  52. }
  53. func (c *Color) AddColors(color1, color2 *Color) *Color {
  54. c.R = color1.R + color2.R
  55. c.G = color1.G + color2.G
  56. c.B = color1.B + color2.B
  57. return c
  58. }
  59. func (c *Color) AddScalar(s float32) *Color {
  60. c.R += s
  61. c.G += s
  62. c.B += s
  63. return c
  64. }
  65. func (c *Color) Multiply(other *Color) *Color {
  66. c.R *= other.R
  67. c.G *= other.G
  68. c.B *= other.B
  69. return c
  70. }
  71. func (c *Color) MultiplyScalar(v float32) *Color {
  72. c.R *= v
  73. c.G *= v
  74. c.B *= v
  75. return c
  76. }
  77. func (c *Color) Lerp(color *Color, alpha float32) *Color {
  78. c.R += (color.R - c.R) * alpha
  79. c.G += (color.G - c.G) * alpha
  80. c.B += (color.B - c.B) * alpha
  81. return c
  82. }
  83. func (c *Color) Equals(other *Color) bool {
  84. return (c.R == other.R) && (c.G == other.G) && (c.B == other.B)
  85. }
  86. func (c *Color) Clone() *Color {
  87. return NewColor(c.R, c.G, c.B)
  88. }
  89. var colorKeywords = map[string]uint{
  90. "aliceblue": 0xF0F8FF,
  91. "antiquewhite": 0xFAEBD7,
  92. "aqua": 0x00FFFF,
  93. "aquamarine": 0x7FFFD4,
  94. "azure": 0xF0FFFF,
  95. "beige": 0xF5F5DC,
  96. "bisque": 0xFFE4C4,
  97. "black": 0x000000,
  98. "blanchedalmond": 0xFFEBCD,
  99. "blue": 0x0000FF,
  100. "blueviolet": 0x8A2BE2,
  101. "brown": 0xA52A2A,
  102. "burlywood": 0xDEB887,
  103. "cadetblue": 0x5F9EA0,
  104. "chartreuse": 0x7FFF00,
  105. "chocolate": 0xD2691E,
  106. "coral": 0xFF7F50,
  107. "cornflowerblue": 0x6495ED,
  108. "cornsilk": 0xFFF8DC,
  109. "crimson": 0xDC143C,
  110. "cyan": 0x00FFFF,
  111. "darkblue": 0x00008B,
  112. "darkcyan": 0x008B8B,
  113. "darkgoldenrod": 0xB8860B,
  114. "darkgray": 0xA9A9A9,
  115. "darkgreen": 0x006400,
  116. "darkgrey": 0xA9A9A9,
  117. "darkkhaki": 0xBDB76B,
  118. "darkmagenta": 0x8B008B,
  119. "darkolivegreen": 0x556B2F,
  120. "darkorange": 0xFF8C00,
  121. "darkorchid": 0x9932CC,
  122. "darkred": 0x8B0000,
  123. "darksalmon": 0xE9967A,
  124. "darkseagreen": 0x8FBC8F,
  125. "darkslateblue": 0x483D8B,
  126. "darkslategray": 0x2F4F4F,
  127. "darkslategrey": 0x2F4F4F,
  128. "darkturquoise": 0x00CED1,
  129. "darkviolet": 0x9400D3,
  130. "deeppink": 0xFF1493,
  131. "deepskyblue": 0x00BFFF,
  132. "dimgray": 0x696969,
  133. "dimgrey": 0x696969,
  134. "dodgerblue": 0x1E90FF,
  135. "firebrick": 0xB22222,
  136. "floralwhite": 0xFFFAF0,
  137. "forestgreen": 0x228B22,
  138. "fuchsia": 0xFF00FF,
  139. "gainsboro": 0xDCDCDC,
  140. "ghostwhite": 0xF8F8FF,
  141. "gold": 0xFFD700,
  142. "goldenrod": 0xDAA520,
  143. "gray": 0x808080,
  144. "green": 0x008000,
  145. "greenyellow": 0xADFF2F,
  146. "grey": 0x808080,
  147. "honeydew": 0xF0FFF0,
  148. "hotpink": 0xFF69B4,
  149. "indianred": 0xCD5C5C,
  150. "indigo": 0x4B0082,
  151. "ivory": 0xFFFFF0,
  152. "khaki": 0xF0E68C,
  153. "lavender": 0xE6E6FA,
  154. "lavenderblush": 0xFFF0F5,
  155. "lawngreen": 0x7CFC00,
  156. "lemonchiffon": 0xFFFACD,
  157. "lightblue": 0xADD8E6,
  158. "lightcoral": 0xF08080,
  159. "lightcyan": 0xE0FFFF,
  160. "lightgoldenrodyellow": 0xFAFAD2,
  161. "lightgray": 0xD3D3D3,
  162. "lightgreen": 0x90EE90,
  163. "lightgrey": 0xD3D3D3,
  164. "lightpink": 0xFFB6C1,
  165. "lightsalmon": 0xFFA07A,
  166. "lightseagreen": 0x20B2AA,
  167. "lightskyblue": 0x87CEFA,
  168. "lightslategray": 0x778899,
  169. "lightslategrey": 0x778899,
  170. "lightsteelblue": 0xB0C4DE,
  171. "lightyellow": 0xFFFFE0,
  172. "lime": 0x00FF00,
  173. "limegreen": 0x32CD32,
  174. "linen": 0xFAF0E6,
  175. "magenta": 0xFF00FF,
  176. "maroon": 0x800000,
  177. "mediumaquamarine": 0x66CDAA,
  178. "mediumblue": 0x0000CD,
  179. "mediumorchid": 0xBA55D3,
  180. "mediumpurple": 0x9370DB,
  181. "mediumseagreen": 0x3CB371,
  182. "mediumslateblue": 0x7B68EE,
  183. "mediumspringgreen": 0x00FA9A,
  184. "mediumturquoise": 0x48D1CC,
  185. "mediumvioletred": 0xC71585,
  186. "midnightblue": 0x191970,
  187. "mintcream": 0xF5FFFA,
  188. "mistyrose": 0xFFE4E1,
  189. "moccasin": 0xFFE4B5,
  190. "navajowhite": 0xFFDEAD,
  191. "navy": 0x000080,
  192. "oldlace": 0xFDF5E6,
  193. "olive": 0x808000,
  194. "olivedrab": 0x6B8E23,
  195. "orange": 0xFFA500,
  196. "orangered": 0xFF4500,
  197. "orchid": 0xDA70D6,
  198. "palegoldenrod": 0xEEE8AA,
  199. "palegreen": 0x98FB98,
  200. "paleturquoise": 0xAFEEEE,
  201. "palevioletred": 0xDB7093,
  202. "papayawhip": 0xFFEFD5,
  203. "peachpuff": 0xFFDAB9,
  204. "peru": 0xCD853F,
  205. "pink": 0xFFC0CB,
  206. "plum": 0xDDA0DD,
  207. "powderblue": 0xB0E0E6,
  208. "purple": 0x800080,
  209. "red": 0xFF0000,
  210. "rosybrown": 0xBC8F8F,
  211. "royalblue": 0x4169E1,
  212. "saddlebrown": 0x8B4513,
  213. "salmon": 0xFA8072,
  214. "sandybrown": 0xF4A460,
  215. "seagreen": 0x2E8B57,
  216. "seashell": 0xFFF5EE,
  217. "sienna": 0xA0522D,
  218. "silver": 0xC0C0C0,
  219. "skyblue": 0x87CEEB,
  220. "slateblue": 0x6A5ACD,
  221. "slategrey": 0x708090,
  222. "snow": 0xFFFAFA,
  223. "springgreen": 0x00FF7F,
  224. "steelblue": 0x4682B4,
  225. "tan": 0xD2B48C,
  226. "teal": 0x008080,
  227. "thistle": 0xD8BFD8,
  228. "tomato": 0xFF6347,
  229. "turquoise": 0x40E0D0,
  230. "violet": 0xEE82EE,
  231. "wheat": 0xF5DEB3,
  232. "white": 0xFFFFFF,
  233. "whitesmoke": 0xF5F5F5,
  234. "yellow": 0xFFFF00,
  235. "yellowgreen": 0x9ACD32,
  236. }