-
Notifications
You must be signed in to change notification settings - Fork 18
/
color.go
601 lines (487 loc) · 15.3 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package tetra3d
import (
"fmt"
"image/color"
"math"
"math/rand"
"sort"
"strconv"
"strings"
"github.com/tanema/gween/ease"
)
// Color represents a color, containing R, G, B, and A components, each expected to range from 0 to 1.
type Color struct {
R, G, B, A float32
}
// NewColor returns a new Color, with the provided R, G, B, and A components expected to range from 0 to 1.
func NewColor(r, g, b, a float32) Color {
return Color{r, g, b, a}
}
// NewColorFromColor returns a new Color based on a provided color.Color.
func NewColorFromColor(color color.Color) Color {
r, g, b, a := color.RGBA()
return NewColor(
float32(r)/65535,
float32(g)/65535,
float32(b)/65535,
float32(a)/65535,
)
}
// NewColorRandom creates a randomized color, with each component lying between the minimum and maximum values.
func NewColorRandom(min, max float32, grayscale bool) Color {
color := NewColor(1, 1, 1, 1)
diff := max - min
if grayscale {
r := min + (diff * rand.Float32())
color.R = r
color.G = r
color.B = r
} else {
color.R = min + (diff * rand.Float32())
color.G = min + (diff * rand.Float32())
color.B = min + (diff * rand.Float32())
}
return color
}
// AddRGBA adds the provided R, G, B, and A values to the color as provided. The components are expected to range from 0 to 1.
func (color Color) AddRGBA(r, g, b, a float32) Color {
color.R += r
color.G += g
color.B += b
color.A += a
return color
}
// Add adds the provided Color to the existing Color.
func (color Color) Add(other Color) Color {
return color.AddRGBA(other.ToFloat32s())
}
// MultiplyRGBA multiplies the color's RGBA channels by the provided R, G, B, and A scalar values.
func (color Color) MultiplyRGBA(scalarR, scalarG, scalarB, scalarA float32) Color {
color.R *= scalarR
color.G *= scalarG
color.B *= scalarB
color.A *= scalarA
return color
}
func (color Color) MultiplyScalarRGB(scalar float32) Color {
return color.MultiplyRGBA(scalar, scalar, scalar, 1)
}
func (color Color) MultiplyScalarRGBA(scalar float32) Color {
return color.MultiplyRGBA(scalar, scalar, scalar, scalar)
}
// Multiply multiplies the existing Color by the provided Color.
func (color Color) Multiply(other Color) Color {
return color.MultiplyRGBA(other.ToFloat32s())
}
// Sub subtracts the other Color from the calling Color instance.
func (color Color) SubRGBA(r, g, b, a float32) Color {
color.R -= r
color.G -= g
color.B -= b
color.A -= a
return color
}
// Sub subtracts the other Color from the calling Color instance.
func (color Color) Sub(other Color) Color {
return color.SubRGBA(other.ToFloat32s())
}
// Mix mixes the calling Color with the other Color, mixed to the percentage given (ranging from 0 - 1).
func (color Color) Mix(other Color, percentage float32) Color {
p := clamp(float64(percentage), 0, 1)
percentage = float32(p)
color.R += (other.R - color.R) * percentage
color.G += (other.G - color.G) * percentage
color.B += (other.B - color.B) * percentage
color.A += (other.A - color.A) * percentage
return color
}
// AddAlpha adds the provided alpha amount to the Color
func (c Color) AddAlpha(alpha float32) Color {
c.A += alpha
return c
}
// SubAlpha adds the provided alpha amount to the Color
func (c Color) SubAlpha(alpha float32) Color {
c.A -= alpha
return c
}
// SetAlpha returns a copy of the the Color with the alpha set to the provided alpha value.
func (color Color) SetAlpha(alpha float32) Color {
color.A = alpha
return color
}
// ToFloat32s returns the Color as four float32 in the order R, G, B, and A.
func (color Color) ToFloat32s() (float32, float32, float32, float32) {
return color.R, color.G, color.B, color.A
}
// ToFloat64s returns four float64 values for each channel in the Color in the order R, G, B, and A.
func (color Color) ToFloat64s() (float64, float64, float64, float64) {
return float64(color.R), float64(color.G), float64(color.B), float64(color.A)
}
// ToFloat32Array returns a [4]float32 array for each channel in the Color in the order of R, G, B, and A.
func (color Color) ToFloat32Array() [4]float32 {
return [4]float32{float32(color.R), float32(color.G), float32(color.B), float32(color.A)}
}
// ToRGBA64 converts a color to a color.RGBA64 instance.
func (c Color) ToRGBA64() color.RGBA64 {
return color.RGBA64{
c.capRGBA64(c.R),
c.capRGBA64(c.G),
c.capRGBA64(c.B),
c.capRGBA64(c.A),
}
}
// ToNRGBA64 converts a color to a color.NRGBA64 (non-alpha color multiplied) color instance.
func (c Color) ToNRGBA64() color.NRGBA64 {
return color.NRGBA64{
c.capRGBA64(c.R),
c.capRGBA64(c.G),
c.capRGBA64(c.B),
c.capRGBA64(c.A),
}
}
func (color Color) capRGBA64(value float32) uint16 {
if value > 1 {
value = 1
} else if value < 0 {
value = 0
}
return uint16(value * math.MaxUint16)
}
// ConvertTosRGB() converts the color's R, G, and B components to the sRGB color space. This is used to convert
// colors from their values in GLTF to how they should appear on the screen. See: https://en.wikipedia.org/wiki/SRGB
func (color Color) ConvertTosRGB() Color {
if color.R <= 0.0031308 {
color.R *= 12.92
} else {
color.R = float32(1.055*math.Pow(float64(color.R), 1/2.4) - 0.055)
}
if color.G <= 0.0031308 {
color.G *= 12.92
} else {
color.G = float32(1.055*math.Pow(float64(color.G), 1/2.4) - 0.055)
}
if color.B <= 0.0031308 {
color.B *= 12.92
} else {
color.B = float32(1.055*math.Pow(float64(color.B), 1/2.4) - 0.055)
}
return color
}
// Lerp linearly interpolates the color from the starting color to the target by the percentage given.
func (c Color) Lerp(other Color, percentage float64) Color {
percentage = clamp(percentage, 0, 1)
c.R += (other.R - c.R) * float32(percentage)
c.G += (other.G - c.G) * float32(percentage)
c.B += (other.B - c.B) * float32(percentage)
c.A += (other.A - c.A) * float32(percentage)
return c
}
// Lerp linearly interpolates the color from the starting color to the target by the percentage given.
func (c Color) LerpRGBA(r, g, b, a, percentage float64) Color {
percentage = clamp(percentage, 0, 1)
c.R += (float32(r) - c.R) * float32(percentage)
c.G += (float32(g) - c.G) * float32(percentage)
c.B += (float32(b) - c.B) * float32(percentage)
c.A += (float32(a) - c.A) * float32(percentage)
return c
}
func (color Color) String() string {
return fmt.Sprintf("<%0.2f, %0.2f, %0.2f, %0.2f>", color.R, color.G, color.B, color.A)
}
// NewColorFromHSV returns a new color, using hue, saturation, and value numbers, each ranging from 0 to 1. A hue of
// 0 is red, while 1 is also red, but on the other end of the spectrum.
// Cribbed from: https://github.com/lucasb-eyer/go-colorful/blob/master/colors.go
func NewColorFromHSV(h, s, v float64) Color {
for h > 1 {
h--
}
for h < 0 {
h++
}
h *= 360
if s > 1 {
s = 1
} else if s < 0 {
s = 0
}
if v > 1 {
v = 1
} else if v < 0 {
v = 0
}
Hp := h / 60.0
C := v * s
X := C * (1.0 - math.Abs(math.Mod(Hp, 2.0)-1.0))
m := v - C
r, g, b := 0.0, 0.0, 0.0
switch {
case 0.0 <= Hp && Hp < 1.0:
r = C
g = X
case 1.0 <= Hp && Hp < 2.0:
r = X
g = C
case 2.0 <= Hp && Hp < 3.0:
g = C
b = X
case 3.0 <= Hp && Hp < 4.0:
g = X
b = C
case 4.0 <= Hp && Hp < 5.0:
r = X
b = C
case 5.0 <= Hp && Hp < 6.0:
r = C
b = X
}
return Color{float32(m + r), float32(m + g), float32(m + b), 1}
}
func NewColorFromHexString(hex string) Color {
c := NewColor(0, 0, 0, 1)
hex = strings.TrimPrefix(hex, "#")
if len(hex) >= 2 {
v, _ := strconv.ParseInt(hex[:2], 16, 32)
c.R = float32(v) / 256.0
if len(hex) >= 4 {
v, _ := strconv.ParseInt(hex[2:4], 16, 32)
c.G = float32(v) / 256.0
} else {
c.G = 1
}
if len(hex) >= 6 {
v, _ := strconv.ParseInt(hex[4:6], 16, 32)
c.B = float32(v) / 256.0
} else {
c.B = 1
}
if len(hex) >= 8 {
v, _ := strconv.ParseInt(hex[6:8], 16, 32)
c.A = float32(v) / 256.0
} else {
c.A = 1
}
}
return c
}
// Hue returns the hue of the color as a value ranging from 0 to 1.
func (color Color) Hue() float64 {
// Function cribbed from: https://github.com/lucasb-eyer/go-colorful/blob/master/colors.go
r := float64(color.R)
g := float64(color.G)
b := float64(color.B)
min := math.Min(math.Min(r, g), b)
v := math.Max(math.Max(r, g), b)
C := v - min
h := 0.0
if min != v {
if v == r {
h = math.Mod((g-b)/C, 6.0)
}
if v == g {
h = (b-r)/C + 2.0
}
if v == b {
h = (r-g)/C + 4.0
}
h *= 60.0
if h < 0.0 {
h += 360.0
}
}
return h / 360
}
// Saturation returns the saturation of the color as a value ranging from 0 to 1.
func (color Color) Saturation() float64 {
r := float64(color.R)
g := float64(color.G)
b := float64(color.B)
min := math.Min(math.Min(r, g), b)
v := math.Max(math.Max(r, g), b)
C := v - min
s := 0.0
if v != 0.0 {
s = C / v
}
return s
}
// Value returns the value of the color as a value, ranging from 0 to 1.
func (color Color) Value() float64 {
r := float64(color.R)
g := float64(color.G)
b := float64(color.B)
return math.Max(math.Max(r, g), b)
}
// func (color Color) HSV() (float64, float64, float64) {
// r := float64(color.R)
// g := float64(color.G)
// b := float64(color.B)
// min := math.Min(math.Min(r, g), b)
// v := math.Max(math.Max(r, g), b)
// C := v - min
// s := 0.0
// if v != 0.0 {
// s = C / v
// }
// h := 0.0
// if min != v {
// if v == r {
// h = math.Mod((g-b)/C, 6.0)
// }
// if v == g {
// h = (b-r)/C + 2.0
// }
// if v == b {
// h = (r-g)/C + 4.0
// }
// h *= 60.0
// if h < 0.0 {
// h += 360.0
// }
// }
// return h / 360, s, v
// }
// SetHue returns a copy of the color with the hue set to the specified value.
// Hue goes through the rainbow, starting with red, and ranges from 0 to 1.
func (color Color) SetHue(h float64) Color {
return NewColorFromHSV(h, color.Saturation(), color.Value())
}
// SetSaturation returns a copy of the color with the saturation of the color set to the specified value.
// Saturation ranges from 0 to 1.
func (color Color) SetSaturation(s float64) Color {
return NewColorFromHSV(color.Hue(), s, color.Value())
}
// SetValue returns a copy of the color with the value of the color set to the specified value.
// Value ranges from 0 to 1.
func (color Color) SetValue(v float64) Color {
return NewColorFromHSV(color.Hue(), color.Saturation(), v)
}
func (c Color) IsZero() bool {
return c.R == 0 && c.G == 0 && c.B == 0 && c.A == 0
}
// ColorCurvePoint indicates an individual color point in a color curve.
type ColorCurvePoint struct {
Color Color
Percentage float64
}
// ColorCurve represents a range of colors that a value of 0 to 1 can interpolate between.
type ColorCurve struct {
Points []ColorCurvePoint
EasingFunction ease.TweenFunc
}
// NewColorCurve creats a new ColorCurve composed of the colors given, evenly spaced throughout the curve.
// If no colors are given, the ColorCurve is still valid - it's just empty.
func NewColorCurve(colors ...Color) ColorCurve {
cc := ColorCurve{
Points: []ColorCurvePoint{},
EasingFunction: ease.Linear,
}
if len(colors) == 1 {
cc.Add(colors[0], 0)
} else if len(colors) > 1 {
for i, color := range colors {
cc.Add(color, float64(i)/float64(len(colors)-1))
}
}
return cc
}
// Clone creates a duplicate ColorCurve.
func (cc ColorCurve) Clone() ColorCurve {
ncc := NewColorCurve()
ncc.Points = append(ncc.Points, cc.Points...)
return ncc
}
// Add adds a color point to the ColorCurve with the color and percentage provided (from 0-1).
func (cc *ColorCurve) Add(color Color, percentage float64) {
cc.AddRGBA(color.R, color.G, color.B, color.A, percentage)
}
// AddRGBA adds a point to the ColorCurve, with r, g, b, and a being the color and the percentage being a number between 0 and 1 indicating the .
func (cc *ColorCurve) AddRGBA(r, g, b, a float32, percentage float64) {
if percentage > 1 {
percentage = 1
} else if percentage < 0 {
percentage = 0
}
cc.Points = append(cc.Points, ColorCurvePoint{
Color: NewColor(r, g, b, a),
Percentage: percentage,
})
sort.Slice(cc.Points, func(i, j int) bool { return cc.Points[i].Percentage < cc.Points[j].Percentage })
}
// Color returns the Color for the given percentage in the color curve. For example, if you have a curve composed of
// the colors {0, 0, 0, 0} at 0 and {1, 1, 1, 1} at 1, then calling Curve.Color(0.5) would return {0.5, 0.5, 0.5, 0.5}.
// If the curve doesn't have any color curve points, Color will return transparent.
func (cc ColorCurve) Color(perc float64) Color {
if perc > 1 {
perc = 1
} else if perc < 0 {
perc = 0
}
var c Color
for i := 0; i < len(cc.Points); i++ {
c = cc.Points[i].Color
if i >= len(cc.Points)-1 || cc.Points[i].Percentage >= perc {
break
}
if cc.Points[i].Percentage <= perc && cc.Points[i+1].Percentage >= perc {
// c = cc.Points[i].Color.Mix(cc.Points[i+1].Color, float32((perc-cc.Points[i].Percentage)/(cc.Points[i+1].Percentage-cc.Points[i].Percentage)))
pp := (perc - cc.Points[i].Percentage)
c = cc.Points[i].Color.Mix(cc.Points[i+1].Color, cc.EasingFunction(float32(pp), 0, 1, float32(cc.Points[i+1].Percentage-cc.Points[i].Percentage)))
break
}
}
return c
}
// ColorTween represents an object that tweens across a ColorCurve.
type ColorTween struct {
ColorCurve ColorCurve // ColorCurve is the curve to use while tweening
Percent float64 // Percent is the percentage through the tween the ColorTween is
Speed float64 // Speed is the speed of the tween; if you want a
Duration float64 // Duration is the duration of the tween in seconds
playing bool
}
// ColorTween creates a new ColorTween object with the specified duration and curve.
func NewColorTween(duration float64, curve ColorCurve) ColorTween {
ct := ColorTween{
ColorCurve: curve,
Speed: 1,
Duration: duration,
}
return ct
}
// Update updates the ColorTween using the delta value provided in seconds and returns if the tween is finished or not.
func (c *ColorTween) Update(dt float64) bool {
if c.playing {
c.Percent += dt / c.Duration * c.Speed
if (c.Percent >= 1 && c.Speed > 0) || (c.Percent <= 0 && c.Speed < 0) {
c.playing = false
c.Percent = clamp(c.Percent, 0, 1)
return true
}
}
return false
}
// Play starts playing the ColorTween back.
func (c *ColorTween) Play() {
c.playing = true
}
// Stop stops the ColorTween.
func (c *ColorTween) Stop() {
c.playing = false
}
// IsPlaying returns if the ColorTween is playing.
func (c *ColorTween) IsPlaying() bool {
return c.playing
}
// Reset resets the ColorTween.
func (c *ColorTween) Reset() {
c.Percent = 0
}
// Color returns the current color from the internal ColorCurve.
func (c *ColorTween) Color() Color {
return c.ColorCurve.Color(c.Percent)
}
// SetEasingFunction sets the easing function to be used by the tween and is present on the ColorCurve.
func (c *ColorTween) SetEasingFunction(easingFunction ease.TweenFunc) {
c.ColorCurve.EasingFunction = easingFunction
}