-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutil.go
134 lines (115 loc) · 2.72 KB
/
util.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
package colorgrad
import (
"math"
"strconv"
"strings"
)
func linspace(min, max float64, n uint) []float64 {
if n == 1 {
return []float64{min}
}
d := max - min
l := float64(n) - 1
res := make([]float64, n)
for i := range res {
res[i] = (min + (float64(i)*d)/l)
}
return res
}
// Map t from range [a, b] to range [0, 1]
func norm(t, a, b float64) float64 {
return (t - a) * (1 / (b - a))
}
func modulo(x, y float64) float64 {
return math.Mod(math.Mod(x, y)+y, y)
}
func clamp01(t float64) float64 {
return math.Max(0, math.Min(1, t))
}
func parseFloat(s string) (float64, bool) {
f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
return f, err == nil
}
func toLinear(x float64) float64 {
if x >= 0.04045 {
return math.Pow((x+0.055)/1.055, 2.4)
}
return x / 12.92
}
func col2linearRgb(col Color) [4]float64 {
return [4]float64{
toLinear(col.R),
toLinear(col.G),
toLinear(col.B),
col.A,
}
}
func col2oklab(col Color) [4]float64 {
arr := col2linearRgb(col)
l := math.Cbrt(0.4121656120*arr[0] + 0.5362752080*arr[1] + 0.0514575653*arr[2])
m := math.Cbrt(0.2118591070*arr[0] + 0.6807189584*arr[1] + 0.1074065790*arr[2])
s := math.Cbrt(0.0883097947*arr[0] + 0.2818474174*arr[1] + 0.6302613616*arr[2])
return [4]float64{
0.2104542553*l + 0.7936177850*m - 0.0040720468*s,
1.9779984951*l - 2.4285922050*m + 0.4505937099*s,
0.0259040371*l + 0.7827717662*m - 0.8086757660*s,
col.A,
}
}
func col2hsv(col Color) [4]float64 {
v := math.Max(col.R, math.Max(col.G, col.B))
d := v - math.Min(col.R, math.Min(col.G, col.B))
if math.Abs(d) < epsilon {
return [4]float64{0, 0, v, col.A}
}
s := d / v
dr := (v - col.R) / d
dg := (v - col.G) / d
db := (v - col.B) / d
var h float64
if math.Abs(col.R-v) < epsilon {
h = db - dg
} else if math.Abs(col.G-v) < epsilon {
h = 2.0 + dr - db
} else {
h = 4.0 + dg - dr
}
h = math.Mod(h*60.0, 360.0)
return [4]float64{normalizeAngle(h), s, v, col.A}
}
func normalizeAngle(t float64) float64 {
t = math.Mod(t, 360.0)
if t < 0.0 {
t += 360.0
}
return t
}
func convertColors(colorsIn []Color, mode BlendMode) [][4]float64 {
colors := make([][4]float64, len(colorsIn))
for i, col := range colorsIn {
switch mode {
case BlendRgb:
colors[i] = [4]float64{col.R, col.G, col.B, col.A}
case BlendLinearRgb:
colors[i] = col2linearRgb(col)
case BlendOklab:
colors[i] = col2oklab(col)
}
}
return colors
}
func linearInterpolate(a, b [4]float64, t float64) (i, j, k, l float64) {
i = a[0] + t*(b[0]-a[0])
j = a[1] + t*(b[1]-a[1])
k = a[2] + t*(b[2]-a[2])
l = a[3] + t*(b[3]-a[3])
return
}
func blendRgb(a, b Color, t float64) Color {
return Color{
R: a.R + t*(b.R-a.R),
G: a.G + t*(b.G-a.G),
B: a.B + t*(b.B-a.B),
A: a.A + t*(b.A-a.A),
}
}