-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
gradient.go
142 lines (119 loc) · 2.89 KB
/
gradient.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
package colorgrad
import (
"image/color"
"math"
"github.com/mazznoer/csscolorparser"
)
type BlendMode int
const (
BlendRgb BlendMode = iota
BlendLinearRgb
BlendOklab
)
func (b BlendMode) String() string {
switch b {
case BlendRgb:
return "BlendRgb"
case BlendLinearRgb:
return "BlendLinearRgb"
case BlendOklab:
return "BlendOklab"
}
return ""
}
type Interpolation int
const (
InterpolationLinear Interpolation = iota
InterpolationCatmullRom
InterpolationBasis
)
func (i Interpolation) String() string {
switch i {
case InterpolationLinear:
return "InterpolationLinear"
case InterpolationCatmullRom:
return "InterpolationCatmullRom"
case InterpolationBasis:
return "InterpolationBasis"
}
return ""
}
type Color = csscolorparser.Color
var Hwb = csscolorparser.FromHwb
var Hsv = csscolorparser.FromHsv
var Hsl = csscolorparser.FromHsl
var LinearRgb = csscolorparser.FromLinearRGB
var Oklab = csscolorparser.FromOklab
var Oklch = csscolorparser.FromOklch
func Rgb(r, g, b, a float64) Color {
return Color{R: r, G: g, B: b, A: a}
}
func Rgb8(r, g, b, a uint8) Color {
return Color{R: float64(r) / 255, G: float64(g) / 255, B: float64(b) / 255, A: float64(a) / 255}
}
func GoColor(col color.Color) Color {
r, g, b, a := col.RGBA()
if a == 0 {
return csscolorparser.Color{}
}
r *= 0xffff
r /= a
g *= 0xffff
g /= a
b *= 0xffff
b /= a
return csscolorparser.Color{R: float64(r) / 65535.0, G: float64(g) / 65535.0, B: float64(b) / 65535.0, A: float64(a) / 65535.0}
}
type gradientBase interface {
// Get color at certain position
At(float64) Color
}
type Gradient struct {
grad gradientBase
dmin float64
dmax float64
}
// Get color at certain position
func (g Gradient) At(t float64) Color {
return g.grad.At(t)
}
// Get color at certain position
func (g Gradient) RepeatAt(t float64) Color {
t = norm(t, g.dmin, g.dmax)
return g.grad.At(g.dmin + modulo(t, 1)*(g.dmax-g.dmin))
}
// Get color at certain position
func (g Gradient) ReflectAt(t float64) Color {
t = norm(t, g.dmin, g.dmax)
return g.grad.At(g.dmin + math.Abs(modulo(1+t, 2)-1)*(g.dmax-g.dmin))
}
// Get n colors evenly spaced across gradient
func (g Gradient) Colors(count uint) []Color {
d := g.dmax - g.dmin
l := float64(count) - 1
colors := make([]Color, count)
for i := range colors {
colors[i] = g.grad.At(g.dmin + (float64(i)*d)/l).Clamp()
}
return colors
}
// Get the gradient domain min and max
func (g Gradient) Domain() (float64, float64) {
return g.dmin, g.dmax
}
// Return a new hard-edge gradient
func (g Gradient) Sharp(segment uint, smoothness float64) Gradient {
colors := []Color{}
if segment >= 2 {
colors = g.Colors(segment)
} else {
colors = append(colors, g.At(g.dmin))
colors = append(colors, g.At(g.dmin))
}
return newSharpGradient(colors, g.dmin, g.dmax, smoothness)
}
type zeroGradient struct {
}
func (zg zeroGradient) At(t float64) Color {
return Color{R: 0, G: 0, B: 0, A: 0}
}