Skip to content

Commit 6a04687

Browse files
committed
some improvements
1 parent 7e28c1c commit 6a04687

File tree

7 files changed

+116
-111
lines changed

7 files changed

+116
-111
lines changed

basis.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,35 @@ import (
77
// https://github.com/d3/d3-interpolate/blob/master/src/basis.js
88

99
type basisGradient struct {
10-
colors [][4]float64
11-
pos []float64
12-
dmin float64
13-
dmax float64
14-
mode BlendMode
15-
firstColor Color
16-
lastColor Color
10+
colors [][4]float64
11+
positions []float64
12+
min float64
13+
max float64
14+
mode BlendMode
15+
first Color
16+
last Color
1717
}
1818

1919
func (lg basisGradient) At(t float64) Color {
20-
if t <= lg.dmin {
21-
return lg.firstColor
20+
if t <= lg.min {
21+
return lg.first
2222
}
2323

24-
if t >= lg.dmax {
25-
return lg.lastColor
24+
if t >= lg.max {
25+
return lg.last
2626
}
2727

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

3232
low := 0
33-
high := len(lg.pos)
33+
high := len(lg.positions)
3434
n := high - 1
3535

3636
for low < high {
3737
mid := (low + high) / 2
38-
if lg.pos[mid] < t {
38+
if lg.positions[mid] < t {
3939
low = mid + 1
4040
} else {
4141
high = mid
@@ -46,8 +46,8 @@ func (lg basisGradient) At(t float64) Color {
4646
low = 1
4747
}
4848

49-
p1 := lg.pos[low-1]
50-
p2 := lg.pos[low]
49+
p1 := lg.positions[low-1]
50+
p2 := lg.positions[low]
5151
val0 := lg.colors[low-1]
5252
val1 := lg.colors[low]
5353
i := low - 1
@@ -81,24 +81,24 @@ func (lg basisGradient) At(t float64) Color {
8181
return Oklab(a, b, c, d).Clamp()
8282
}
8383

84-
return Color{R: 0, G: 0, B: 0, A: 0}
84+
return Color{}
8585
}
8686

87-
func newBasisGradient(colors []Color, pos []float64, mode BlendMode) Gradient {
87+
func newBasisGradient(colors []Color, positions []float64, mode BlendMode) Gradient {
8888
gradbase := basisGradient{
89-
colors: convertColors(colors, mode),
90-
pos: pos,
91-
dmin: pos[0],
92-
dmax: pos[len(pos)-1],
93-
mode: mode,
94-
firstColor: colors[0],
95-
lastColor: colors[len(colors)-1],
89+
colors: convertColors(colors, mode),
90+
positions: positions,
91+
min: positions[0],
92+
max: positions[len(positions)-1],
93+
mode: mode,
94+
first: colors[0],
95+
last: colors[len(colors)-1],
9696
}
9797

9898
return Gradient{
9999
grad: gradbase,
100-
dmin: pos[0],
101-
dmax: pos[len(pos)-1],
100+
dmin: positions[0],
101+
dmax: positions[len(positions)-1],
102102
}
103103
}
104104

catmull_rom.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ func toCatmullRomSegments(values []float64) [][4]float64 {
4747
}
4848

4949
type catmullRomGradient struct {
50-
segments [][4][4]float64
51-
positions []float64
52-
dmin float64
53-
dmax float64
54-
mode BlendMode
55-
firstColor Color
56-
lastColor Color
50+
segments [][4][4]float64
51+
positions []float64
52+
min float64
53+
max float64
54+
mode BlendMode
55+
first Color
56+
last Color
5757
}
5858

59-
func newCatmullRomGradient(colors []Color, pos []float64, space BlendMode) Gradient {
59+
func newCatmullRomGradient(colors []Color, positions []float64, space BlendMode) Gradient {
6060
n := len(colors)
6161
a := make([]float64, n)
6262
b := make([]float64, n)
@@ -90,35 +90,35 @@ func newCatmullRomGradient(colors []Color, pos []float64, space BlendMode) Gradi
9090
s4[i],
9191
}
9292
}
93-
dmin := pos[0]
94-
dmax := pos[n-1]
93+
min := positions[0]
94+
max := positions[n-1]
9595
gradbase := catmullRomGradient{
96-
segments: segments,
97-
positions: pos,
98-
dmin: dmin,
99-
dmax: dmax,
100-
mode: space,
101-
firstColor: colors[0],
102-
lastColor: colors[len(colors)-1],
96+
segments: segments,
97+
positions: positions,
98+
min: min,
99+
max: max,
100+
mode: space,
101+
first: colors[0],
102+
last: colors[len(colors)-1],
103103
}
104104
return Gradient{
105105
grad: gradbase,
106-
dmin: dmin,
107-
dmax: dmax,
106+
dmin: min,
107+
dmax: max,
108108
}
109109
}
110110

111111
func (g catmullRomGradient) At(t float64) Color {
112112
if math.IsNaN(t) {
113-
return Color{R: 0, G: 0, B: 0, A: 1}
113+
return Color{A: 1}
114114
}
115115

116-
if t <= g.dmin {
117-
return g.firstColor
116+
if t <= g.min {
117+
return g.first
118118
}
119119

120-
if t >= g.dmax {
121-
return g.lastColor
120+
if t >= g.max {
121+
return g.last
122122
}
123123

124124
low := 0
@@ -162,5 +162,5 @@ func (g catmullRomGradient) At(t float64) Color {
162162
return Oklab(a, b, c, d).Clamp()
163163
}
164164

165-
return Color{R: 0, G: 0, B: 0, A: 0}
165+
return Color{}
166166
}

gimp.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,21 @@ type gimpSegment struct {
5454

5555
type gimpGradient struct {
5656
segments []gimpSegment
57-
dmin float64
58-
dmax float64
57+
min float64
58+
max float64
5959
}
6060

6161
func (ggr gimpGradient) At(t float64) Color {
62-
if t <= ggr.dmin {
62+
if t <= ggr.min {
6363
return ggr.segments[0].lcolor
6464
}
6565

66-
if t >= ggr.dmax {
66+
if t >= ggr.max {
6767
return ggr.segments[len(ggr.segments)-1].rcolor
6868
}
6969

7070
if math.IsNaN(t) {
71-
return Color{R: 0, G: 0, B: 0, A: 1}
71+
return Color{A: 1}
7272
}
7373

7474
low := 0
@@ -277,8 +277,8 @@ func ParseGgr(r io.Reader, fg, bg Color) (Gradient, string, error) {
277277

278278
gradbase := gimpGradient{
279279
segments: segments,
280-
dmin: 0,
281-
dmax: 1,
280+
min: 0,
281+
max: 1,
282282
}
283283

284284
return Gradient{

gimp_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ func Test_GIMPGradient(t *testing.T) {
2626
ggr = "GIMP Gradient\nName: My Gradient\n1\n0 0.5 1 0 0 0 1 1 1 1 1 0 0 1 3"
2727
grad, name, err = ParseGgr(strings.NewReader(ggr), red, blue)
2828
test(t, err, nil)
29+
test(t, name, "My Gradient")
2930
test(t, grad.At(0).HexString(), "#ff0000")
3031
test(t, grad.At(1).HexString(), "#0000ff")
3132

3233
// Blending function: step
3334
ggr = "GIMP Gradient\nName: My Gradient\n1\n0 0.5 1 1 0 0 1 0 0 1 1 5 0 0 0"
3435
grad, name, err = ParseGgr(strings.NewReader(ggr), black, black)
3536
test(t, err, nil)
37+
test(t, name, "My Gradient")
3638
test(t, grad.At(0.00).HexString(), "#ff0000")
3739
test(t, grad.At(0.25).HexString(), "#ff0000")
3840
test(t, grad.At(0.49).HexString(), "#ff0000")
@@ -44,6 +46,7 @@ func Test_GIMPGradient(t *testing.T) {
4446
ggr = "GIMP Gradient\nName: My Gradient\n1\n0 0.5 1 1 1 1 1 0 0 1 1 0 1 0 0"
4547
grad, name, err = ParseGgr(strings.NewReader(ggr), black, black)
4648
test(t, err, nil)
49+
test(t, name, "My Gradient")
4750
test(t, grad.At(0.0).HexString(), "#ffffff")
4851
test(t, grad.At(0.5).HexString(), "#80ff80")
4952
test(t, grad.At(1.0).HexString(), "#0000ff")
@@ -52,6 +55,7 @@ func Test_GIMPGradient(t *testing.T) {
5255
ggr = "GIMP Gradient\nName: My Gradient\n1\n0 0.5 1 1 1 1 1 0 0 1 1 0 2 0 0"
5356
grad, name, err = ParseGgr(strings.NewReader(ggr), black, black)
5457
test(t, err, nil)
58+
test(t, name, "My Gradient")
5559
test(t, grad.At(0.0).HexString(), "#ffffff")
5660
test(t, grad.At(0.5).HexString(), "#ff80ff")
5761
test(t, grad.At(1.0).HexString(), "#0000ff")
@@ -60,7 +64,8 @@ func Test_GIMPGradient(t *testing.T) {
6064

6165
data := []string{
6266
"",
63-
"GIMP Pallete",
67+
" ",
68+
"GIMP Palette\nName: Gold\n#\n252 252 128",
6469
"GIMP Gradient\nxx",
6570
"GIMP Gradient\nName: Gradient\nx",
6671
"GIMP Gradient\nName: Gradient\n1\n0 0 0",

linear.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@ import (
55
)
66

77
type linearGradient struct {
8-
colors [][4]float64
9-
pos []float64
10-
dmin float64
11-
dmax float64
12-
mode BlendMode
13-
firstColor Color
14-
lastColor Color
8+
colors [][4]float64
9+
positions []float64
10+
min float64
11+
max float64
12+
mode BlendMode
13+
first Color
14+
last Color
1515
}
1616

1717
func (lg linearGradient) At(t float64) Color {
18-
if t <= lg.dmin {
19-
return lg.firstColor
18+
if t <= lg.min {
19+
return lg.first
2020
}
2121

22-
if t >= lg.dmax {
23-
return lg.lastColor
22+
if t >= lg.max {
23+
return lg.last
2424
}
2525

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

3030
low := 0
31-
high := len(lg.pos)
31+
high := len(lg.positions)
3232

3333
for low < high {
3434
mid := (low + high) / 2
35-
if lg.pos[mid] < t {
35+
if lg.positions[mid] < t {
3636
low = mid + 1
3737
} else {
3838
high = mid
@@ -43,8 +43,8 @@ func (lg linearGradient) At(t float64) Color {
4343
low = 1
4444
}
4545

46-
p1 := lg.pos[low-1]
47-
p2 := lg.pos[low]
46+
p1 := lg.positions[low-1]
47+
p2 := lg.positions[low]
4848
t = (t - p1) / (p2 - p1)
4949
a, b, c, d := linearInterpolate(lg.colors[low-1], lg.colors[low], t)
5050

@@ -57,23 +57,23 @@ func (lg linearGradient) At(t float64) Color {
5757
return Oklab(a, b, c, d).Clamp()
5858
}
5959

60-
return Color{R: 0, G: 0, B: 0, A: 0}
60+
return Color{}
6161
}
6262

63-
func newLinearGradient(colors []Color, pos []float64, mode BlendMode) Gradient {
63+
func newLinearGradient(colors []Color, positions []float64, mode BlendMode) Gradient {
6464
gradbase := linearGradient{
65-
colors: convertColors(colors, mode),
66-
pos: pos,
67-
dmin: pos[0],
68-
dmax: pos[len(pos)-1],
69-
mode: mode,
70-
firstColor: colors[0],
71-
lastColor: colors[len(colors)-1],
65+
colors: convertColors(colors, mode),
66+
positions: positions,
67+
min: positions[0],
68+
max: positions[len(positions)-1],
69+
mode: mode,
70+
first: colors[0],
71+
last: colors[len(colors)-1],
7272
}
7373

7474
return Gradient{
7575
grad: gradbase,
76-
dmin: pos[0],
77-
dmax: pos[len(pos)-1],
76+
dmin: positions[0],
77+
dmax: positions[len(positions)-1],
7878
}
7979
}

0 commit comments

Comments
 (0)