Skip to content

Commit 541f090

Browse files
committed
Step 3: preset gradients and some tests
1 parent a3181b2 commit 541f090

File tree

7 files changed

+177
-218
lines changed

7 files changed

+177
-218
lines changed

builder_test.go

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,53 @@
1-
//go:build ignore
21
package colorgrad
32

43
import (
5-
"image/color"
64
"math"
75
"testing"
86
)
97

108
func TestBasic1(t *testing.T) {
119
// Single color
1210
grad, _ := NewGradient().
13-
Colors(color.RGBA{0, 255, 0, 255}).
11+
Colors(Color{R: 0, G: 1, B: 0, A: 1}).
1412
Build()
1513
dmin, dmax := grad.Domain()
1614
if dmin != 0 || dmax != 1 {
1715
t.Errorf("Domain got: (%v, %v), expected: (0, 1)", dmin, dmax)
1816
}
19-
testStr(t, grad.At(0).Hex(), "#00ff00")
20-
testStr(t, grad.At(1).Hex(), "#00ff00")
17+
testStr(t, grad.At(0).HexString(), "#00ff00")
18+
testStr(t, grad.At(1).HexString(), "#00ff00")
2119

2220
// Named colors
2321
grad, _ = NewGradient().
2422
HtmlColors("tomato", "skyblue", "gold", "springgreen").
2523
Build()
2624
colors := grad.ColorfulColors(4)
27-
testStr(t, colors[0].Hex(), "#ff6347")
28-
testStr(t, colors[1].Hex(), "#87ceeb")
29-
testStr(t, colors[2].Hex(), "#ffd700")
30-
testStr(t, colors[3].Hex(), "#00ff7f")
25+
testStr(t, colors[0].HexString(), "#ff6347")
26+
testStr(t, colors[1].HexString(), "#87ceeb")
27+
testStr(t, colors[2].HexString(), "#ffd700")
28+
testStr(t, colors[3].HexString(), "#00ff7f")
3129

3230
// Blend mode
3331
grad, _ = NewGradient().
3432
HtmlColors("#333", "#bbb").
35-
Mode(BlendHcl).
36-
Build()
37-
testStr(t, grad.At(0).Hex(), "#333333")
38-
testStr(t, grad.At(1).Hex(), "#bbbbbb")
39-
40-
grad, _ = NewGradient().
41-
HtmlColors("#333", "#bbb").
42-
Mode(BlendHsv).
43-
Build()
44-
testStr(t, grad.At(0).Hex(), "#333333")
45-
testStr(t, grad.At(1).Hex(), "#bbbbbb")
46-
47-
grad, _ = NewGradient().
48-
HtmlColors("#333", "#bbb").
49-
Mode(BlendLab).
33+
Mode(BlendRgb).
5034
Build()
51-
testStr(t, grad.At(0).Hex(), "#333333")
52-
testStr(t, grad.At(1).Hex(), "#bbbbbb")
35+
testStr(t, grad.At(0).HexString(), "#333333")
36+
testStr(t, grad.At(1).HexString(), "#bbbbbb")
5337

5438
grad, _ = NewGradient().
5539
HtmlColors("#333", "#bbb").
5640
Mode(BlendLinearRgb).
5741
Build()
58-
testStr(t, grad.At(0).Hex(), "#333333")
59-
testStr(t, grad.At(1).Hex(), "#bbbbbb")
60-
61-
grad, _ = NewGradient().
62-
HtmlColors("#333", "#bbb").
63-
Mode(BlendLuv).
64-
Build()
65-
testStr(t, grad.At(0).Hex(), "#333333")
66-
testStr(t, grad.At(1).Hex(), "#bbbbbb")
67-
68-
grad, _ = NewGradient().
69-
HtmlColors("#333", "#bbb").
70-
Mode(BlendRgb).
71-
Build()
72-
testStr(t, grad.At(0).Hex(), "#333333")
73-
testStr(t, grad.At(1).Hex(), "#bbbbbb")
42+
testStr(t, grad.At(0).HexString(), "#333333")
43+
testStr(t, grad.At(1).HexString(), "#bbbbbb")
7444

7545
grad, _ = NewGradient().
7646
HtmlColors("#333", "#bbb").
7747
Mode(BlendOklab).
7848
Build()
79-
testStr(t, grad.At(0).Hex(), "#333333")
80-
testStr(t, grad.At(1).Hex(), "#bbbbbb")
49+
testStr(t, grad.At(0).HexString(), "#333333")
50+
testStr(t, grad.At(1).HexString(), "#bbbbbb")
8151
}
8252

8353
func TestBasic2(t *testing.T) {
@@ -88,13 +58,13 @@ func TestBasic2(t *testing.T) {
8858
if len(colors) != 2 {
8959
t.Errorf("Expected 2, got %v", len(colors))
9060
}
91-
testStr(t, colors[0].Hex(), "#000000")
92-
testStr(t, colors[1].Hex(), "#ffffff")
61+
testStr(t, colors[0].HexString(), "#000000")
62+
testStr(t, colors[1].HexString(), "#ffffff")
9363

94-
testStr(t, grad.At(math.NaN()).Hex(), "#000000")
64+
testStr(t, grad.At(math.NaN()).HexString(), "#00000000")
9565

9666
// Custom colors
97-
grad, _ = NewGradient().
67+
/*grad, _ = NewGradient().
9868
Colors(
9969
color.RGBA{255, 0, 0, 255},
10070
color.RGBA{255, 255, 0, 255},
@@ -106,11 +76,11 @@ func TestBasic2(t *testing.T) {
10676
if len(colors) != 3 {
10777
t.Errorf("Expected 3, got %v", len(colors))
10878
}
109-
testStr(t, colors[0].Hex(), "#ff0000")
110-
testStr(t, colors[1].Hex(), "#ffff00")
111-
testStr(t, colors[2].Hex(), "#0000ff")
79+
testStr(t, colors[0].HexString(), "#ff0000")
80+
testStr(t, colors[1].HexString(), "#ffff00")
81+
testStr(t, colors[2].HexString(), "#0000ff")
11282
113-
testStr(t, grad.At(math.NaN()).Hex(), "#000000")
83+
testStr(t, grad.At(math.NaN()).HexString(), "#00000000")
11484
11585
// Custom colors #2
11686
grad, _ = NewGradient().
@@ -119,10 +89,10 @@ func TestBasic2(t *testing.T) {
11989
HtmlColors("lime").
12090
Build()
12191
colors = grad.ColorfulColors(4)
122-
testStr(t, colors[0].Hex(), "#0000ff")
123-
testStr(t, colors[1].Hex(), "#00ffff")
124-
testStr(t, colors[2].Hex(), "#ffff00")
125-
testStr(t, colors[3].Hex(), "#00ff00")
92+
testStr(t, colors[0].HexString(), "#0000ff")
93+
testStr(t, colors[1].HexString(), "#00ffff")
94+
testStr(t, colors[2].HexString(), "#ffff00")
95+
testStr(t, colors[3].HexString(), "#00ff00")*/
12696
}
12797

12898
func TestError(t *testing.T) {

gradient_test.go

Lines changed: 82 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
//go:build ignore
21
package colorgrad
32

43
import (
54
"image/color"
65
"testing"
7-
8-
"github.com/lucasb-eyer/go-colorful"
96
)
107

118
func TestDomain(t *testing.T) {
@@ -14,49 +11,49 @@ func TestDomain(t *testing.T) {
1411
Domain(0, 100).
1512
Build()
1613

17-
testStr(t, grad.At(0).Hex(), "#ffff00")
18-
testStr(t, grad.At(50).Hex(), "#0000ff")
19-
testStr(t, grad.At(100).Hex(), "#00ff00")
14+
testStr(t, grad.At(0).HexString(), "#ffff00")
15+
testStr(t, grad.At(50).HexString(), "#0000ff")
16+
testStr(t, grad.At(100).HexString(), "#00ff00")
2017
// outside domain
21-
testStr(t, grad.At(-10).Hex(), "#ffff00")
22-
testStr(t, grad.At(150).Hex(), "#00ff00")
18+
testStr(t, grad.At(-10).HexString(), "#ffff00")
19+
testStr(t, grad.At(150).HexString(), "#00ff00")
2320

2421
grad, _ = NewGradient().
2522
HtmlColors("yellow", "blue", "lime").
2623
Domain(-1, 1).
2724
Build()
2825

29-
testStr(t, grad.At(-1).Hex(), "#ffff00")
30-
testStr(t, grad.At(0).Hex(), "#0000ff")
31-
testStr(t, grad.At(1).Hex(), "#00ff00")
26+
testStr(t, grad.At(-1).HexString(), "#ffff00")
27+
testStr(t, grad.At(0).HexString(), "#0000ff")
28+
testStr(t, grad.At(1).HexString(), "#00ff00")
3229
// outside domain
33-
testStr(t, grad.At(-1.5).Hex(), "#ffff00")
34-
testStr(t, grad.At(1.5).Hex(), "#00ff00")
30+
testStr(t, grad.At(-1.5).HexString(), "#ffff00")
31+
testStr(t, grad.At(1.5).HexString(), "#00ff00")
3532

3633
grad, _ = NewGradient().
3734
HtmlColors("#00ff00", "#ff0000", "#ffff00").
3835
Domain(0, 0.75, 1).
3936
Build()
4037

41-
testStr(t, grad.At(0).Hex(), "#00ff00")
42-
testStr(t, grad.At(0.75).Hex(), "#ff0000")
43-
testStr(t, grad.At(1).Hex(), "#ffff00")
38+
testStr(t, grad.At(0).HexString(), "#00ff00")
39+
testStr(t, grad.At(0.75).HexString(), "#ff0000")
40+
testStr(t, grad.At(1).HexString(), "#ffff00")
4441
// outside domain
45-
testStr(t, grad.At(-1).Hex(), "#00ff00")
46-
testStr(t, grad.At(1.5).Hex(), "#ffff00")
42+
testStr(t, grad.At(-1).HexString(), "#00ff00")
43+
testStr(t, grad.At(1.5).HexString(), "#ffff00")
4744

4845
grad, _ = NewGradient().
4946
HtmlColors("#00ff00", "#ff0000", "#0000ff", "#ffff00").
5047
Domain(15, 25, 29, 63).
5148
Build()
5249

53-
testStr(t, grad.At(15).Hex(), "#00ff00")
54-
testStr(t, grad.At(25).Hex(), "#ff0000")
55-
testStr(t, grad.At(29).Hex(), "#0000ff")
56-
testStr(t, grad.At(63).Hex(), "#ffff00")
50+
testStr(t, grad.At(15).HexString(), "#00ff00")
51+
testStr(t, grad.At(25).HexString(), "#ff0000")
52+
testStr(t, grad.At(29).HexString(), "#0000ff")
53+
testStr(t, grad.At(63).HexString(), "#ffff00")
5754
// outside domain
58-
testStr(t, grad.At(10).Hex(), "#00ff00")
59-
testStr(t, grad.At(67).Hex(), "#ffff00")
55+
testStr(t, grad.At(10).HexString(), "#00ff00")
56+
testStr(t, grad.At(67).HexString(), "#ffff00")
6057
}
6158

6259
func TestGetColors(t *testing.T) {
@@ -75,91 +72,91 @@ func TestGetColors(t *testing.T) {
7572
if len(colors0) != 0 {
7673
t.Errorf("Error.")
7774
}
78-
colors1 := grad.ColorfulColors(1)
79-
testStr(t, colors1[0].Hex(), "#000000")
75+
//colors1 := grad.ColorfulColors(1)
76+
//testStr(t, colors1[0].HexString(), "#000000")
8077

8178
colors2 := grad.ColorfulColors(2)
82-
testStr(t, colors2[0].Hex(), "#000000")
83-
testStr(t, colors2[1].Hex(), "#ffffff")
79+
testStr(t, colors2[0].HexString(), "#000000")
80+
testStr(t, colors2[1].HexString(), "#ffffff")
8481

8582
colors3 := grad.ColorfulColors(3)
86-
testStr(t, colors3[0].Hex(), "#000000")
87-
testStr(t, colors3[1].Hex(), "#808080")
88-
testStr(t, colors3[2].Hex(), "#ffffff")
83+
testStr(t, colors3[0].HexString(), "#000000")
84+
testStr(t, colors3[1].HexString(), "#808080")
85+
testStr(t, colors3[2].HexString(), "#ffffff")
8986

9087
grad, _ = NewGradient().
9188
HtmlColors("#f00", "#0f0", "#00f").
9289
Domain(-1, 1).
9390
Build()
9491

9592
colors5 := grad.ColorfulColors(5)
96-
testStr(t, colors5[0].Hex(), "#ff0000")
97-
testStr(t, colors5[1].Hex(), "#808000")
98-
testStr(t, colors5[2].Hex(), "#00ff00")
99-
testStr(t, colors5[3].Hex(), "#008080")
100-
testStr(t, colors5[4].Hex(), "#0000ff")
93+
testStr(t, colors5[0].HexString(), "#ff0000")
94+
testStr(t, colors5[1].HexString(), "#808000")
95+
testStr(t, colors5[2].HexString(), "#00ff00")
96+
testStr(t, colors5[3].HexString(), "#008080")
97+
testStr(t, colors5[4].HexString(), "#0000ff")
10198
}
10299

103100
func TestSpreadRepeat(t *testing.T) {
104101
grad, _ := NewGradient().
105102
HtmlColors("#000", "#fff").
106103
Build()
107104

108-
testStr(t, grad.RepeatAt(-2.0).Hex(), "#000000")
109-
testStr(t, grad.RepeatAt(-1.9).Hex(), "#1a1a1a")
110-
testStr(t, grad.RepeatAt(-1.5).Hex(), "#808080")
111-
testStr(t, grad.RepeatAt(-1.1).Hex(), "#e5e5e5")
112-
113-
testStr(t, grad.RepeatAt(-1.0).Hex(), "#000000")
114-
testStr(t, grad.RepeatAt(-0.9).Hex(), "#191919")
115-
testStr(t, grad.RepeatAt(-0.5).Hex(), "#808080")
116-
testStr(t, grad.RepeatAt(-0.1).Hex(), "#e6e6e6")
117-
118-
testStr(t, grad.RepeatAt(0.0).Hex(), "#000000")
119-
testStr(t, grad.RepeatAt(0.1).Hex(), "#1a1a1a")
120-
testStr(t, grad.RepeatAt(0.5).Hex(), "#808080")
121-
testStr(t, grad.RepeatAt(0.9).Hex(), "#e5e5e5")
122-
123-
testStr(t, grad.RepeatAt(1.0).Hex(), "#000000")
124-
testStr(t, grad.RepeatAt(1.1).Hex(), "#1a1a1a")
125-
testStr(t, grad.RepeatAt(1.5).Hex(), "#808080")
126-
testStr(t, grad.RepeatAt(1.9).Hex(), "#e5e5e5")
127-
128-
testStr(t, grad.RepeatAt(2.0).Hex(), "#000000")
129-
testStr(t, grad.RepeatAt(2.1).Hex(), "#1a1a1a")
130-
testStr(t, grad.RepeatAt(2.5).Hex(), "#808080")
131-
testStr(t, grad.RepeatAt(2.9).Hex(), "#e5e5e5")
105+
testStr(t, grad.RepeatAt(-2.0).HexString(), "#000000")
106+
testStr(t, grad.RepeatAt(-1.9).HexString(), "#1a1a1a")
107+
testStr(t, grad.RepeatAt(-1.5).HexString(), "#808080")
108+
testStr(t, grad.RepeatAt(-1.1).HexString(), "#e5e5e5")
109+
110+
testStr(t, grad.RepeatAt(-1.0).HexString(), "#000000")
111+
testStr(t, grad.RepeatAt(-0.9).HexString(), "#191919")
112+
testStr(t, grad.RepeatAt(-0.5).HexString(), "#808080")
113+
testStr(t, grad.RepeatAt(-0.1).HexString(), "#e6e6e6")
114+
115+
testStr(t, grad.RepeatAt(0.0).HexString(), "#000000")
116+
testStr(t, grad.RepeatAt(0.1).HexString(), "#1a1a1a")
117+
testStr(t, grad.RepeatAt(0.5).HexString(), "#808080")
118+
testStr(t, grad.RepeatAt(0.9).HexString(), "#e5e5e5")
119+
120+
testStr(t, grad.RepeatAt(1.0).HexString(), "#000000")
121+
testStr(t, grad.RepeatAt(1.1).HexString(), "#1a1a1a")
122+
testStr(t, grad.RepeatAt(1.5).HexString(), "#808080")
123+
testStr(t, grad.RepeatAt(1.9).HexString(), "#e5e5e5")
124+
125+
testStr(t, grad.RepeatAt(2.0).HexString(), "#000000")
126+
testStr(t, grad.RepeatAt(2.1).HexString(), "#1a1a1a")
127+
testStr(t, grad.RepeatAt(2.5).HexString(), "#808080")
128+
testStr(t, grad.RepeatAt(2.9).HexString(), "#e5e5e5")
132129
}
133130

134131
func TestSpreadReflect(t *testing.T) {
135132
grad, _ := NewGradient().
136133
HtmlColors("#000", "#fff").
137134
Build()
138135

139-
testStr(t, grad.ReflectAt(-2.0).Hex(), "#000000")
140-
testStr(t, grad.ReflectAt(-1.9).Hex(), "#1a1a1a")
141-
testStr(t, grad.ReflectAt(-1.5).Hex(), "#808080")
142-
testStr(t, grad.ReflectAt(-1.1).Hex(), "#e5e5e5")
143-
144-
testStr(t, grad.ReflectAt(-1.0).Hex(), "#ffffff")
145-
testStr(t, grad.ReflectAt(-0.9).Hex(), "#e5e5e5")
146-
testStr(t, grad.ReflectAt(-0.5).Hex(), "#808080")
147-
testStr(t, grad.ReflectAt(-0.1).Hex(), "#1a1a1a")
148-
149-
testStr(t, grad.ReflectAt(0.0).Hex(), "#000000")
150-
testStr(t, grad.ReflectAt(0.1).Hex(), "#1a1a1a")
151-
testStr(t, grad.ReflectAt(0.5).Hex(), "#808080")
152-
testStr(t, grad.ReflectAt(0.9).Hex(), "#e5e5e5")
153-
154-
testStr(t, grad.ReflectAt(1.0).Hex(), "#ffffff")
155-
testStr(t, grad.ReflectAt(1.1).Hex(), "#e5e5e5")
156-
testStr(t, grad.ReflectAt(1.5).Hex(), "#808080")
157-
testStr(t, grad.ReflectAt(1.9).Hex(), "#1a1a1a")
158-
159-
testStr(t, grad.ReflectAt(2.0).Hex(), "#000000")
160-
testStr(t, grad.ReflectAt(2.1).Hex(), "#1a1a1a")
161-
testStr(t, grad.ReflectAt(2.5).Hex(), "#808080")
162-
testStr(t, grad.ReflectAt(2.9).Hex(), "#e5e5e5")
136+
testStr(t, grad.ReflectAt(-2.0).HexString(), "#000000")
137+
testStr(t, grad.ReflectAt(-1.9).HexString(), "#1a1a1a")
138+
testStr(t, grad.ReflectAt(-1.5).HexString(), "#808080")
139+
testStr(t, grad.ReflectAt(-1.1).HexString(), "#e5e5e5")
140+
141+
testStr(t, grad.ReflectAt(-1.0).HexString(), "#ffffff")
142+
testStr(t, grad.ReflectAt(-0.9).HexString(), "#e5e5e5")
143+
testStr(t, grad.ReflectAt(-0.5).HexString(), "#808080")
144+
testStr(t, grad.ReflectAt(-0.1).HexString(), "#1a1a1a")
145+
146+
testStr(t, grad.ReflectAt(0.0).HexString(), "#000000")
147+
testStr(t, grad.ReflectAt(0.1).HexString(), "#1a1a1a")
148+
testStr(t, grad.ReflectAt(0.5).HexString(), "#808080")
149+
testStr(t, grad.ReflectAt(0.9).HexString(), "#e5e5e5")
150+
151+
testStr(t, grad.ReflectAt(1.0).HexString(), "#ffffff")
152+
testStr(t, grad.ReflectAt(1.1).HexString(), "#e5e5e5")
153+
testStr(t, grad.ReflectAt(1.5).HexString(), "#808080")
154+
testStr(t, grad.ReflectAt(1.9).HexString(), "#1a1a1a")
155+
156+
testStr(t, grad.ReflectAt(2.0).HexString(), "#000000")
157+
testStr(t, grad.ReflectAt(2.1).HexString(), "#1a1a1a")
158+
testStr(t, grad.ReflectAt(2.5).HexString(), "#808080")
159+
testStr(t, grad.ReflectAt(2.9).HexString(), "#e5e5e5")
163160
}
164161

165162
func testStr(t *testing.T, a, b string) {
@@ -175,7 +172,7 @@ func isZeroGradient(grad Gradient) bool {
175172
return false
176173
}
177174
colors := grad.ColorfulColors(13)
178-
black := colorful.Color{R: 0, G: 0, B: 0}
175+
black := Color{R: 0, G: 0, B: 0, A: 0}
179176
for _, col := range colors {
180177
if col != black {
181178
return false

0 commit comments

Comments
 (0)