Skip to content

Commit 571978c

Browse files
committed
Step 6: some cleanups
1 parent a7a9f1b commit 571978c

File tree

10 files changed

+37
-53
lines changed

10 files changed

+37
-53
lines changed

basis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (lg basisGradient) At(t float64) Color {
2626
}
2727

2828
if math.IsNaN(t) {
29-
return Color{R: 0, G: 0, B: 0, A: 0}
29+
return Color{R: 0, G: 0, B: 0, A: 1}
3030
}
3131

3232
low := 0
@@ -78,7 +78,7 @@ func (lg basisGradient) At(t float64) Color {
7878
case BlendLinearRgb:
7979
return LinearRgb(a, b, c, d)
8080
case BlendOklab:
81-
return Oklab(a, b, c, d) //.Clamped()
81+
return Oklab(a, b, c, d).Clamp()
8282
}
8383

8484
return Color{R: 0, G: 0, B: 0, A: 0}

builder_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestBasic1(t *testing.T) {
2121
grad, _ = NewGradient().
2222
HtmlColors("tomato", "skyblue", "gold", "springgreen").
2323
Build()
24-
colors := grad.ColorfulColors(4)
24+
colors := grad.Colors(4)
2525
testStr(t, colors[0].HexString(), "#ff6347")
2626
testStr(t, colors[1].HexString(), "#87ceeb")
2727
testStr(t, colors[2].HexString(), "#ffd700")
@@ -53,25 +53,25 @@ func TestBasic1(t *testing.T) {
5353
func TestBasic2(t *testing.T) {
5454
// Custom gradient default
5555
grad, _ := NewGradient().Build()
56-
colors := grad.ColorfulColors(2)
56+
colors := grad.Colors(2)
5757

5858
if len(colors) != 2 {
5959
t.Errorf("Expected 2, got %v", len(colors))
6060
}
6161
testStr(t, colors[0].HexString(), "#000000")
6262
testStr(t, colors[1].HexString(), "#ffffff")
6363

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

6666
// Custom colors
67-
/*grad, _ = NewGradient().
67+
grad, _ = NewGradient().
6868
Colors(
69-
color.RGBA{255, 0, 0, 255},
70-
color.RGBA{255, 255, 0, 255},
71-
color.RGBA{0, 0, 255, 255},
69+
Rgb8(255, 0, 0, 255),
70+
Rgb8(255, 255, 0, 255),
71+
Rgb8(0, 0, 255, 255),
7272
).
7373
Build()
74-
colors = grad.ColorfulColors(3)
74+
colors = grad.Colors(3)
7575

7676
if len(colors) != 3 {
7777
t.Errorf("Expected 3, got %v", len(colors))
@@ -80,19 +80,19 @@ func TestBasic2(t *testing.T) {
8080
testStr(t, colors[1].HexString(), "#ffff00")
8181
testStr(t, colors[2].HexString(), "#0000ff")
8282

83-
testStr(t, grad.At(math.NaN()).HexString(), "#00000000")
83+
testStr(t, grad.At(math.NaN()).HexString(), "#000000")
8484

8585
// Custom colors #2
8686
grad, _ = NewGradient().
8787
HtmlColors("#00f", "#00ffff").
88-
Colors(color.RGBA{255, 255, 0, 255}).
88+
Colors(Rgb8(255, 255, 0, 255)).
8989
HtmlColors("lime").
9090
Build()
91-
colors = grad.ColorfulColors(4)
91+
colors = grad.Colors(4)
9292
testStr(t, colors[0].HexString(), "#0000ff")
9393
testStr(t, colors[1].HexString(), "#00ffff")
9494
testStr(t, colors[2].HexString(), "#ffff00")
95-
testStr(t, colors[3].HexString(), "#00ff00")*/
95+
testStr(t, colors[3].HexString(), "#00ff00")
9696
}
9797

9898
func TestError(t *testing.T) {

catmull_rom.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ type catmullRomGradient struct {
9494

9595
func (s catmullRomGradient) At(t float64) Color {
9696
if math.IsNaN(t) {
97-
return Color{R: 0, G: 0, B: 0, A: 0}
97+
return Color{R: 0, G: 0, B: 0, A: 1}
9898
}
9999
t = math.Max(s.dmin, math.Min(s.dmax, t))
100100
switch s.mode {
101101
case BlendLinearRgb:
102102
return LinearRgb(s.a.at(t), s.b.at(t), s.c.at(t), 1)
103103
case BlendOklab:
104-
return Oklab(s.a.at(t), s.b.at(t), s.c.at(t), 1) //.Clamped
104+
return Oklab(s.a.at(t), s.b.at(t), s.c.at(t), 1).Clamp()
105105
default:
106106
return Color{R: s.a.at(t), G: s.b.at(t), B: s.c.at(t), A: 1}
107107
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.17
44

55
require (
66
github.com/lucasb-eyer/go-colorful v1.2.0
7-
github.com/mazznoer/csscolorparser v0.1.4
7+
github.com/mazznoer/csscolorparser v0.1.5-0.20240719031628-b1c13f071417
88
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
22
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
33
github.com/mazznoer/csscolorparser v0.1.4 h1:1bAWPWdoMc5qdAXVlixS9rpZLw/vaq5InX/lj3tfRQQ=
44
github.com/mazznoer/csscolorparser v0.1.4/go.mod h1:OQRVvgCyHDCAquR1YWfSwwaDcM0LhnSffGnlbOew/3I=
5+
github.com/mazznoer/csscolorparser v0.1.5-0.20240717073837-ced1c0227ecd h1:QD8NhwDjRqslqCti93tzxSjojvNUihcaYqQjGwdQAcM=
6+
github.com/mazznoer/csscolorparser v0.1.5-0.20240717073837-ced1c0227ecd/go.mod h1:OQRVvgCyHDCAquR1YWfSwwaDcM0LhnSffGnlbOew/3I=
7+
github.com/mazznoer/csscolorparser v0.1.5-0.20240719031628-b1c13f071417 h1:IdwaaP9b9TX6x7pMASy55IiksNPm73Z64ru71Nx22bw=
8+
github.com/mazznoer/csscolorparser v0.1.5-0.20240719031628-b1c13f071417/go.mod h1:OQRVvgCyHDCAquR1YWfSwwaDcM0LhnSffGnlbOew/3I=

gradient.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package colorgrad
22

33
import (
4-
"image/color"
54
"math"
65

76
"github.com/mazznoer/csscolorparser"
@@ -93,21 +92,12 @@ func (g Gradient) ReflectAt(t float64) Color {
9392
}
9493

9594
// Get n colors evenly spaced across gradient
96-
func (g Gradient) ColorfulColors(count uint) []Color {
95+
func (g Gradient) Colors(count uint) []Color {
9796
d := g.dmax - g.dmin
9897
l := float64(count) - 1
9998
colors := make([]Color, count)
10099
for i := range colors {
101-
colors[i] = g.grad.At(g.dmin + (float64(i)*d)/l) //.Clamped()
102-
}
103-
return colors
104-
}
105-
106-
// Get n colors evenly spaced across gradient
107-
func (g Gradient) Colors(count uint) []color.Color {
108-
colors := make([]color.Color, count)
109-
for i, col := range g.ColorfulColors(count) {
110-
colors[i] = col
100+
colors[i] = g.grad.At(g.dmin + (float64(i)*d)/l).Clamp()
111101
}
112102
return colors
113103
}
@@ -121,7 +111,7 @@ func (g Gradient) Domain() (float64, float64) {
121111
func (g Gradient) Sharp(segment uint, smoothness float64) Gradient {
122112
colors := []Color{}
123113
if segment >= 2 {
124-
colors = g.ColorfulColors(segment)
114+
colors = g.Colors(segment)
125115
} else {
126116
colors = append(colors, g.At(g.dmin))
127117
colors = append(colors, g.At(g.dmin))

gradient_test.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package colorgrad
22

33
import (
4-
"image/color"
54
"testing"
65
)
76

@@ -58,28 +57,19 @@ func TestDomain(t *testing.T) {
5857

5958
func TestGetColors(t *testing.T) {
6059
grad, _ := NewGradient().Build()
61-
colorsA := grad.ColorfulColors(5) // []colorful.Color
62-
colorsB := grad.Colors(5) // []color.Color
6360

64-
for i, c2 := range colorsB {
65-
var c1 color.Color = colorsA[i]
66-
if c1 != c2 {
67-
t.Errorf("%v != %v", c1, c2)
68-
}
69-
}
70-
71-
colors0 := grad.ColorfulColors(0)
61+
colors0 := grad.Colors(0)
7262
if len(colors0) != 0 {
7363
t.Errorf("Error.")
7464
}
75-
//colors1 := grad.ColorfulColors(1)
65+
//colors1 := grad.Colors(1)
7666
//testStr(t, colors1[0].HexString(), "#000000")
7767

78-
colors2 := grad.ColorfulColors(2)
68+
colors2 := grad.Colors(2)
7969
testStr(t, colors2[0].HexString(), "#000000")
8070
testStr(t, colors2[1].HexString(), "#ffffff")
8171

82-
colors3 := grad.ColorfulColors(3)
72+
colors3 := grad.Colors(3)
8373
testStr(t, colors3[0].HexString(), "#000000")
8474
testStr(t, colors3[1].HexString(), "#808080")
8575
testStr(t, colors3[2].HexString(), "#ffffff")
@@ -89,7 +79,7 @@ func TestGetColors(t *testing.T) {
8979
Domain(-1, 1).
9080
Build()
9181

92-
colors5 := grad.ColorfulColors(5)
82+
colors5 := grad.Colors(5)
9383
testStr(t, colors5[0].HexString(), "#ff0000")
9484
testStr(t, colors5[1].HexString(), "#808000")
9585
testStr(t, colors5[2].HexString(), "#00ff00")
@@ -171,7 +161,7 @@ func isZeroGradient(grad Gradient) bool {
171161
if dmin != 0 || dmax != 1 {
172162
return false
173163
}
174-
colors := grad.ColorfulColors(13)
164+
colors := grad.Colors(13)
175165
black := Color{R: 0, G: 0, B: 0, A: 0}
176166
for _, col := range colors {
177167
if col != black {

linear.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (lg linearGradient) At(t float64) Color {
2424
}
2525

2626
if math.IsNaN(t) {
27-
return Color{R: 0, G: 0, B: 0, A: 0}
27+
return Color{R: 0, G: 0, B: 0, A: 1}
2828
}
2929

3030
low := 0
@@ -54,7 +54,7 @@ func (lg linearGradient) At(t float64) Color {
5454
case BlendLinearRgb:
5555
return LinearRgb(a, b, c, d)
5656
case BlendOklab:
57-
return Oklab(a, b, c, d) //.Clamped()
57+
return Oklab(a, b, c, d).Clamp()
5858
}
5959

6060
return Color{R: 0, G: 0, B: 0, A: 0}

preset_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ func testGrad(t *testing.T, grad Gradient, start, end string) {
5454
testStr(t, grad.At(1.2).HexString(), end)
5555
testStr(t, grad.At(1.5).HexString(), end)
5656
testStr(t, grad.At(1.8).HexString(), end)
57-
testStr(t, grad.At(math.NaN()).HexString(), "#00000000")
57+
testStr(t, grad.At(math.NaN()).HexString(), "#000000")
5858
}

spline_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestSplineGradient(t *testing.T) {
1414
testStr(t, grad.At(0).HexString(), "#ff0000")
1515
testStr(t, grad.At(0.5).HexString(), "#00ff00")
1616
testStr(t, grad.At(1).HexString(), "#0000ff")
17-
testStr(t, grad.At(math.NaN()).HexString(), "#00000000")
17+
testStr(t, grad.At(math.NaN()).HexString(), "#000000")
1818

1919
grad, _ = NewGradient().
2020
HtmlColors("#f00", "#0f0", "#00f").
@@ -24,7 +24,7 @@ func TestSplineGradient(t *testing.T) {
2424
testStr(t, grad.At(0).HexString(), "#ff0000")
2525
testStr(t, grad.At(0.5).HexString(), "#00ff00")
2626
testStr(t, grad.At(1).HexString(), "#0000ff")
27-
testStr(t, grad.At(math.NaN()).HexString(), "#00000000")
27+
testStr(t, grad.At(math.NaN()).HexString(), "#000000")
2828

2929
grad, _ = NewGradient().
3030
HtmlColors("#f00", "#0f0", "#00f").
@@ -34,13 +34,13 @@ func TestSplineGradient(t *testing.T) {
3434
testStr(t, grad.At(0).HexString(), "#ff0000")
3535
//testStr(t, grad.At(0.5).HexString(), "#00ff00")
3636
testStr(t, grad.At(1).HexString(), "#0000ff")
37-
testStr(t, grad.At(math.NaN()).HexString(), "#00000000")
37+
testStr(t, grad.At(math.NaN()).HexString(), "#000000")
3838

3939
grad, _ = NewGradient().
4040
HtmlColors("#f00", "#0f0", "#00f").
4141
Interpolation(InterpolationBasis).
4242
Build()
4343
testStr(t, grad.At(0).HexString(), "#ff0000")
4444
testStr(t, grad.At(1).HexString(), "#0000ff")
45-
testStr(t, grad.At(math.NaN()).HexString(), "#00000000")
45+
testStr(t, grad.At(math.NaN()).HexString(), "#000000")
4646
}

0 commit comments

Comments
 (0)