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