color.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 standard web color name
  44. func (c *Color) SetName(name string) *Color {
  45. *c = colorNames[name]
  46. return c
  47. }
  48. func (c *Color) Add(other *Color) *Color {
  49. c.R += other.R
  50. c.G += other.G
  51. c.B += other.B
  52. return c
  53. }
  54. func (c *Color) AddColors(color1, color2 *Color) *Color {
  55. c.R = color1.R + color2.R
  56. c.G = color1.G + color2.G
  57. c.B = color1.B + color2.B
  58. return c
  59. }
  60. func (c *Color) AddScalar(s float32) *Color {
  61. c.R += s
  62. c.G += s
  63. c.B += s
  64. return c
  65. }
  66. func (c *Color) Multiply(other *Color) *Color {
  67. c.R *= other.R
  68. c.G *= other.G
  69. c.B *= other.B
  70. return c
  71. }
  72. func (c *Color) MultiplyScalar(v float32) *Color {
  73. c.R *= v
  74. c.G *= v
  75. c.B *= v
  76. return c
  77. }
  78. func (c *Color) Lerp(color *Color, alpha float32) *Color {
  79. c.R += (color.R - c.R) * alpha
  80. c.G += (color.G - c.G) * alpha
  81. c.B += (color.B - c.B) * alpha
  82. return c
  83. }
  84. func (c *Color) Equals(other *Color) bool {
  85. return (c.R == other.R) && (c.G == other.G) && (c.B == other.B)
  86. }
  87. // IsColor returns true if the specified name is a standard web color name
  88. func IsColor(name string) bool {
  89. _, ok := colorNames[name]
  90. return ok
  91. }
  92. // ColorName returns a Color with the specified standard web color name.
  93. func ColorName(name string) Color {
  94. return colorNames[name]
  95. }
  96. // Color4Name returns a Color4 with the specified standard web color name
  97. // and specified alpha channel.
  98. func Color4Name(name string, alpha float32) Color4 {
  99. c := colorNames[name]
  100. return Color4{c.R, c.G, c.B, alpha}
  101. }
  102. // colorNames maps standard web color names to a Vector3 with
  103. // the color's RGB component values
  104. var colorNames = map[string]Color{
  105. "aliceblue": Color{0.941, 0.973, 1.000},
  106. "antiquewhite": Color{0.980, 0.922, 0.843},
  107. "aqua": Color{0.000, 1.000, 1.000},
  108. "aquamarine": Color{0.498, 1.000, 0.831},
  109. "azure": Color{0.941, 1.000, 1.000},
  110. "beige": Color{0.961, 0.961, 0.863},
  111. "bisque": Color{1.000, 0.894, 0.769},
  112. "black": Color{0.000, 0.000, 0.000},
  113. "blanchedalmond": Color{1.000, 0.922, 0.804},
  114. "blue": Color{0.000, 0.000, 1.000},
  115. "blueviolet": Color{0.541, 0.169, 0.886},
  116. "brown": Color{0.647, 0.165, 0.165},
  117. "burlywood": Color{0.871, 0.722, 0.529},
  118. "cadetblue": Color{0.373, 0.620, 0.627},
  119. "chartreuse": Color{0.498, 1.000, 0.000},
  120. "chocolate": Color{0.824, 0.412, 0.118},
  121. "coral": Color{1.000, 0.498, 0.314},
  122. "cornflowerblue": Color{0.392, 0.584, 0.929},
  123. "cornsilk": Color{1.000, 0.973, 0.863},
  124. "crimson": Color{0.863, 0.078, 0.235},
  125. "cyan": Color{0.000, 1.000, 1.000},
  126. "darkblue": Color{0.000, 0.000, 0.545},
  127. "darkcyan": Color{0.000, 0.545, 0.545},
  128. "darkgoldenrod": Color{0.722, 0.525, 0.043},
  129. "darkgray": Color{0.663, 0.663, 0.663},
  130. "darkgreen": Color{0.000, 0.392, 0.000},
  131. "darkgrey": Color{0.663, 0.663, 0.663},
  132. "darkkhaki": Color{0.741, 0.718, 0.420},
  133. "darkmagenta": Color{0.545, 0.000, 0.545},
  134. "darkolivegreen": Color{0.333, 0.420, 0.184},
  135. "darkorange": Color{1.000, 0.549, 0.000},
  136. "darkorchid": Color{0.600, 0.196, 0.800},
  137. "darkred": Color{0.545, 0.000, 0.000},
  138. "darksalmon": Color{0.914, 0.588, 0.478},
  139. "darkseagreen": Color{0.561, 0.737, 0.561},
  140. "darkslateblue": Color{0.282, 0.239, 0.545},
  141. "darkslategray": Color{0.184, 0.310, 0.310},
  142. "darkslategrey": Color{0.184, 0.310, 0.310},
  143. "darkturquoise": Color{0.000, 0.808, 0.820},
  144. "darkviolet": Color{0.580, 0.000, 0.827},
  145. "deeppink": Color{1.000, 0.078, 0.576},
  146. "deepskyblue": Color{0.000, 0.749, 1.000},
  147. "dimgray": Color{0.412, 0.412, 0.412},
  148. "dimgrey": Color{0.412, 0.412, 0.412},
  149. "dodgerblue": Color{0.118, 0.565, 1.000},
  150. "firebrick": Color{0.698, 0.133, 0.133},
  151. "floralwhite": Color{1.000, 0.980, 0.941},
  152. "forestgreen": Color{0.133, 0.545, 0.133},
  153. "fuchsia": Color{1.000, 0.000, 1.000},
  154. "gainsboro": Color{0.863, 0.863, 0.863},
  155. "ghostwhite": Color{0.973, 0.973, 1.000},
  156. "gold": Color{1.000, 0.843, 0.000},
  157. "goldenrod": Color{0.855, 0.647, 0.125},
  158. "gray": Color{0.502, 0.502, 0.502},
  159. "green": Color{0.000, 0.502, 0.000},
  160. "greenyellow": Color{0.678, 1.000, 0.184},
  161. "grey": Color{0.502, 0.502, 0.502},
  162. "honeydew": Color{0.941, 1.000, 0.941},
  163. "hotpink": Color{1.000, 0.412, 0.706},
  164. "indianred": Color{0.804, 0.361, 0.361},
  165. "indigo": Color{0.294, 0.000, 0.510},
  166. "ivory": Color{1.000, 1.000, 0.941},
  167. "khaki": Color{0.941, 0.902, 0.549},
  168. "lavender": Color{0.902, 0.902, 0.980},
  169. "lavenderblush": Color{1.000, 0.941, 0.961},
  170. "lawngreen": Color{0.486, 0.988, 0.000},
  171. "lemonchiffon": Color{1.000, 0.980, 0.804},
  172. "lightblue": Color{0.678, 0.847, 0.902},
  173. "lightcoral": Color{0.941, 0.502, 0.502},
  174. "lightcyan": Color{0.878, 1.000, 1.000},
  175. "lightgoldenrodyellow": Color{0.980, 0.980, 0.824},
  176. "lightgray": Color{0.827, 0.827, 0.827},
  177. "lightgreen": Color{0.565, 0.933, 0.565},
  178. "lightgrey": Color{0.827, 0.827, 0.827},
  179. "lightpink": Color{1.000, 0.714, 0.757},
  180. "lightsalmon": Color{1.000, 0.627, 0.478},
  181. "lightseagreen": Color{0.125, 0.698, 0.667},
  182. "lightskyblue": Color{0.529, 0.808, 0.980},
  183. "lightslategray": Color{0.467, 0.533, 0.600},
  184. "lightslategrey": Color{0.467, 0.533, 0.600},
  185. "lightsteelblue": Color{0.690, 0.769, 0.871},
  186. "lightyellow": Color{1.000, 1.000, 0.878},
  187. "lime": Color{0.000, 1.000, 0.000},
  188. "limegreen": Color{0.196, 0.804, 0.196},
  189. "linen": Color{0.980, 0.941, 0.902},
  190. "magenta": Color{1.000, 0.000, 1.000},
  191. "maroon": Color{0.502, 0.000, 0.000},
  192. "mediumaquamarine": Color{0.400, 0.804, 0.667},
  193. "mediumblue": Color{0.000, 0.000, 0.804},
  194. "mediumorchid": Color{0.729, 0.333, 0.827},
  195. "mediumpurple": Color{0.576, 0.439, 0.859},
  196. "mediumseagreen": Color{0.235, 0.702, 0.443},
  197. "mediumslateblue": Color{0.482, 0.408, 0.933},
  198. "mediumspringgreen": Color{0.000, 0.980, 0.604},
  199. "mediumturquoise": Color{0.282, 0.820, 0.800},
  200. "mediumvioletred": Color{0.780, 0.082, 0.522},
  201. "midnightblue": Color{0.098, 0.098, 0.439},
  202. "mintcream": Color{0.961, 1.000, 0.980},
  203. "mistyrose": Color{1.000, 0.894, 0.882},
  204. "moccasin": Color{1.000, 0.894, 0.710},
  205. "navajowhite": Color{1.000, 0.871, 0.678},
  206. "navy": Color{0.000, 0.000, 0.502},
  207. "oldlace": Color{0.992, 0.961, 0.902},
  208. "olive": Color{0.502, 0.502, 0.000},
  209. "olivedrab": Color{0.420, 0.557, 0.137},
  210. "orange": Color{1.000, 0.647, 0.000},
  211. "orangered": Color{1.000, 0.271, 0.000},
  212. "orchid": Color{0.855, 0.439, 0.839},
  213. "palegoldenrod": Color{0.933, 0.910, 0.667},
  214. "palegreen": Color{0.596, 0.984, 0.596},
  215. "paleturquoise": Color{0.686, 0.933, 0.933},
  216. "palevioletred": Color{0.859, 0.439, 0.576},
  217. "papayawhip": Color{1.000, 0.937, 0.835},
  218. "peachpuff": Color{1.000, 0.855, 0.725},
  219. "peru": Color{0.804, 0.522, 0.247},
  220. "pink": Color{1.000, 0.753, 0.796},
  221. "plum": Color{0.867, 0.627, 0.867},
  222. "powderblue": Color{0.690, 0.878, 0.902},
  223. "purple": Color{0.502, 0.000, 0.502},
  224. "red": Color{1.000, 0.000, 0.000},
  225. "rosybrown": Color{0.737, 0.561, 0.561},
  226. "royalblue": Color{0.255, 0.412, 0.882},
  227. "saddlebrown": Color{0.545, 0.271, 0.075},
  228. "salmon": Color{0.980, 0.502, 0.447},
  229. "sandybrown": Color{0.957, 0.643, 0.376},
  230. "seagreen": Color{0.180, 0.545, 0.341},
  231. "seashell": Color{1.000, 0.961, 0.933},
  232. "sienna": Color{0.627, 0.322, 0.176},
  233. "silver": Color{0.753, 0.753, 0.753},
  234. "skyblue": Color{0.529, 0.808, 0.922},
  235. "slateblue": Color{0.416, 0.353, 0.804},
  236. "slategray": Color{0.439, 0.502, 0.565},
  237. "slategrey": Color{0.439, 0.502, 0.565},
  238. "snow": Color{1.000, 0.980, 0.980},
  239. "springgreen": Color{0.000, 1.000, 0.498},
  240. "steelblue": Color{0.275, 0.510, 0.706},
  241. "tan": Color{0.824, 0.706, 0.549},
  242. "teal": Color{0.000, 0.502, 0.502},
  243. "thistle": Color{0.847, 0.749, 0.847},
  244. "tomato": Color{1.000, 0.388, 0.278},
  245. "turquoise": Color{0.251, 0.878, 0.816},
  246. "violet": Color{0.933, 0.510, 0.933},
  247. "wheat": Color{0.961, 0.871, 0.702},
  248. "white": Color{1.000, 1.000, 1.000},
  249. "whitesmoke": Color{0.961, 0.961, 0.961},
  250. "yellow": Color{1.000, 1.000, 0.000},
  251. "yellowgreen": Color{0.604, 0.804, 0.196},
  252. }