-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
builder.go
166 lines (141 loc) · 3.55 KB
/
builder.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
package colorgrad
import (
"fmt"
"github.com/mazznoer/csscolorparser"
)
type GradientBuilder struct {
colors []Color
positions []float64
mode BlendMode
interpolation Interpolation
invalidHtmlColors []string
clean bool
}
func NewGradient() *GradientBuilder {
return &GradientBuilder{
mode: BlendRgb,
interpolation: InterpolationLinear,
clean: false,
}
}
func (gb *GradientBuilder) Colors(colors ...Color) *GradientBuilder {
for _, col := range colors {
gb.colors = append(gb.colors, col)
}
gb.clean = false
return gb
}
func (gb *GradientBuilder) HtmlColors(htmlColors ...string) *GradientBuilder {
for _, s := range htmlColors {
c, err := csscolorparser.Parse(s)
if err != nil {
gb.invalidHtmlColors = append(gb.invalidHtmlColors, s)
continue
}
gb.colors = append(gb.colors, c)
}
gb.clean = false
return gb
}
func (gb *GradientBuilder) Domain(positions ...float64) *GradientBuilder {
gb.positions = positions
gb.clean = false
return gb
}
func (gb *GradientBuilder) Mode(mode BlendMode) *GradientBuilder {
gb.mode = mode
return gb
}
func (gb *GradientBuilder) Interpolation(mode Interpolation) *GradientBuilder {
gb.interpolation = mode
return gb
}
func (gb *GradientBuilder) prepareBuild() error {
if gb.clean {
return nil
}
if gb.invalidHtmlColors != nil {
return fmt.Errorf("invalid HTML colors: %q", gb.invalidHtmlColors)
}
var colors []Color
var positions []float64
if len(gb.colors) == 0 {
// Default colors
colors = []Color{
{R: 0, G: 0, B: 0, A: 1}, // black
{R: 1, G: 1, B: 1, A: 1}, // white
}
} else if len(gb.colors) == 1 {
colors = []Color{gb.colors[0], gb.colors[0]}
} else {
colors = make([]Color, len(gb.colors))
copy(colors, gb.colors)
}
if len(gb.positions) == 0 {
positions = linspace(0, 1, uint(len(colors)))
} else if len(gb.positions) == len(colors) {
for i := 0; i < len(gb.positions)-1; i++ {
if gb.positions[i] > gb.positions[i+1] {
return fmt.Errorf("invalid domain")
}
}
positions = make([]float64, len(gb.positions))
copy(positions, gb.positions)
} else if len(gb.positions) == 2 {
if gb.positions[0] >= gb.positions[1] {
return fmt.Errorf("invalid domain")
}
positions = linspace(gb.positions[0], gb.positions[1], uint(len(colors)))
} else {
return fmt.Errorf("invalid domain")
}
gb.colors = nil
gb.positions = nil
prev := positions[0]
lastIdx := len(positions) - 1
for i, col := range colors {
pos := positions[i]
var next float64
if i == lastIdx {
next = positions[lastIdx]
} else {
next = positions[i+1]
}
if (pos-prev)+(next-pos) < epsilon {
// skip
} else {
gb.colors = append(gb.colors, col)
gb.positions = append(gb.positions, pos)
}
prev = pos
}
if len(gb.colors) != len(gb.positions) || len(gb.colors) < 2 {
return fmt.Errorf("invalid stops")
}
gb.clean = true
return nil
}
func (gb *GradientBuilder) Build() (Gradient, error) {
if err := gb.prepareBuild(); err != nil {
return Gradient{
grad: zeroGradient{},
dmin: 0,
dmax: 1,
}, err
}
if gb.interpolation == InterpolationLinear {
return newLinearGradient(gb.colors, gb.positions, gb.mode), nil
}
if gb.interpolation == InterpolationBasis {
return newBasisGradient(gb.colors, gb.positions, gb.mode), nil
}
return newCatmullRomGradient(gb.colors, gb.positions, gb.mode), nil
}
// For testing purposes
func (gb *GradientBuilder) GetColors() *[]Color {
return &gb.colors
}
// For testing purposes
func (gb *GradientBuilder) GetPositions() *[]float64 {
return &gb.positions
}