color.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 color describes an RGB color
  9. type Color struct {
  10. R float32
  11. G float32
  12. B float32
  13. }
  14. // New 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. // Name returns a Color with the specified standard web color
  25. // name (case insensitive).
  26. // Returns black color if the specified color name not found
  27. func ColorName(name string) Color {
  28. return mapColorNames[strings.ToLower(name)]
  29. }
  30. // NewColorHex creates and returns a pointer to a new color
  31. // with its RGB components from the specified hex value
  32. func NewColorHex(color uint) *Color {
  33. return (&Color{}).SetHex(color)
  34. }
  35. // Set sets this color individual R,G,B components
  36. func (c *Color) Set(r, g, b float32) *Color {
  37. c.R = r
  38. c.G = g
  39. c.B = b
  40. return c
  41. }
  42. // SetHex sets the color RGB components from the
  43. // specified integer interpreted as a color hex number
  44. func (c *Color) SetHex(value uint) *Color {
  45. c.R = float32((value >> 16 & 255)) / 255
  46. c.G = float32((value >> 8 & 255)) / 255
  47. c.B = float32((value & 255)) / 255
  48. return c
  49. }
  50. // SetName sets the color RGB components from the
  51. // specified standard web color name
  52. func (c *Color) SetName(name string) *Color {
  53. color, ok := mapColorNames[strings.ToLower(name)]
  54. if ok {
  55. *c = color
  56. }
  57. return c
  58. }
  59. func (c *Color) Add(other *Color) *Color {
  60. c.R += other.R
  61. c.G += other.G
  62. c.B += other.B
  63. return c
  64. }
  65. func (c *Color) AddColors(color1, color2 *Color) *Color {
  66. c.R = color1.R + color2.R
  67. c.G = color1.G + color2.G
  68. c.B = color1.B + color2.B
  69. return c
  70. }
  71. func (c *Color) AddScalar(s float32) *Color {
  72. c.R += s
  73. c.G += s
  74. c.B += s
  75. return c
  76. }
  77. func (c *Color) Multiply(other *Color) *Color {
  78. c.R *= other.R
  79. c.G *= other.G
  80. c.B *= other.B
  81. return c
  82. }
  83. func (c *Color) MultiplyScalar(v float32) *Color {
  84. c.R *= v
  85. c.G *= v
  86. c.B *= v
  87. return c
  88. }
  89. func (c *Color) Lerp(color *Color, alpha float32) *Color {
  90. c.R += (color.R - c.R) * alpha
  91. c.G += (color.G - c.G) * alpha
  92. c.B += (color.B - c.B) * alpha
  93. return c
  94. }
  95. func (c *Color) Equals(other *Color) bool {
  96. return (c.R == other.R) && (c.G == other.G) && (c.B == other.B)
  97. }
  98. func IsColorName(name string) (Color, bool) {
  99. c, ok := mapColorNames[strings.ToLower(name)]
  100. return c, ok
  101. }
  102. // mapColorNames maps standard web color names to a Color with
  103. // the standard web color's RGB component values
  104. var mapColorNames = 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. }