color.go 11 KB

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