-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgradient.go
More file actions
150 lines (127 loc) · 3.07 KB
/
gradient.go
File metadata and controls
150 lines (127 loc) · 3.07 KB
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
package colorgrad
import (
"image/color"
"math"
"github.com/mazznoer/csscolorparser"
)
type BlendMode int
const (
BlendRgb BlendMode = iota
BlendLinearRgb
BlendLab
BlendOklab
)
func (b BlendMode) String() string {
switch b {
case BlendRgb:
return "BlendRgb"
case BlendLinearRgb:
return "BlendLinearRgb"
case BlendLab:
return "BlendLab"
case BlendOklab:
return "BlendOklab"
}
return ""
}
type Interpolation int
const (
InterpolationLinear Interpolation = iota
InterpolationSmoothstep
InterpolationCatmullRom
InterpolationBasis
)
func (i Interpolation) String() string {
switch i {
case InterpolationLinear:
return "InterpolationLinear"
case InterpolationSmoothstep:
return "InterpolationSmoothstep"
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 Lab = csscolorparser.FromLab
var Lch = csscolorparser.FromLch
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 GradientCore interface {
// Get color at certain position
At(float64) Color
}
type Gradient struct {
Core GradientCore
Min float64
Max float64
}
// Get color at certain position
func (g Gradient) At(t float64) Color {
return g.Core.At(t)
}
// Get color at certain position
func (g Gradient) RepeatAt(t float64) Color {
t = norm(t, g.Min, g.Max)
return g.Core.At(g.Min + modulo(t, 1)*(g.Max-g.Min))
}
// Get color at certain position
func (g Gradient) ReflectAt(t float64) Color {
t = norm(t, g.Min, g.Max)
return g.Core.At(g.Min + math.Abs(modulo(1+t, 2)-1)*(g.Max-g.Min))
}
// Get n colors evenly spaced across gradient
func (g Gradient) Colors(count uint) []Color {
d := g.Max - g.Min
l := float64(count) - 1
colors := make([]Color, count)
for i := range colors {
colors[i] = g.Core.At(g.Min + (float64(i)*d)/l).Clamp()
}
return colors
}
// Get the gradient domain min and max
func (g Gradient) Domain() (float64, float64) {
return g.Min, g.Max
}
// 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.Min))
colors = append(colors, g.At(g.Min))
}
return newSharpGradient(colors, g.Min, g.Max, smoothness)
}
type zeroGradient struct {
}
func (zg zeroGradient) At(t float64) Color {
return Color{R: 0, G: 0, B: 0, A: 0}
}