Skip to content

Commit 1434e66

Browse files
committed
Apply code suggestions
Reference: #799 (review) Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 55662b3 commit 1434e66

File tree

4 files changed

+92
-12
lines changed

4 files changed

+92
-12
lines changed

palette/moreland/luminance.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,16 @@ func (l luminance) Palette(n int) palette.Palette {
176176
l.SetMax(1)
177177
}
178178
delta := (l.max - l.min) / float64(n-1)
179+
var v float64
179180
c := make([]color.Color, n)
180181
for i := range n {
181-
v := min(l.min+float64(delta*float64(i)), l.max)
182+
if i == n-1 {
183+
// Avoid potential overflow on last element
184+
// due to floating point error.
185+
v = l.max
186+
} else {
187+
v = l.min + float64(delta*float64(i))
188+
}
182189
var err error
183190
c[i], err = l.At(v)
184191
if err != nil {

palette/moreland/luminance_test.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,46 @@ func BenchmarkLuminance_At(b *testing.B) {
162162
}
163163
}
164164

165+
// See https://github.com/gonum/plot/issues/798
165166
func TestIssue798Kindlmann(t *testing.T) {
166-
// https://github.com/gonum/plot/issues/798
167-
colors := Kindlmann()
168-
colors.SetMin(0.3402859786606234)
169-
colors.SetMax(15.322841335211892)
170-
colors.Palette(15)
167+
for _, test := range []struct {
168+
n int
169+
min, max float64
170+
}{
171+
0: {n: 2, min: 0, max: 1},
172+
1: {n: 15, min: 0.3402859786606234, max: 15.322841335211892},
173+
} {
174+
t.Run("", func(t *testing.T) {
175+
defer func() {
176+
r := recover()
177+
if r != nil {
178+
t.Errorf("unexpected panic with n=%d min=%f max=%f: %v", test.n, test.min, test.max, r)
179+
}
180+
}()
181+
colors := Kindlmann()
182+
colors.SetMin(test.min)
183+
colors.SetMax(test.max)
184+
col := colors.Palette(test.n).Colors()
185+
min, err := colors.At(test.min)
186+
if err != nil {
187+
t.Fatalf("unexpected error calling colors.At(min): %v", err)
188+
}
189+
if !sameColor(min, col[0]) {
190+
t.Errorf("unexpected min color %#v != %#v", min, col[0])
191+
}
192+
max, err := colors.At(test.max)
193+
if err != nil {
194+
t.Fatalf("unexpected error calling colors.At(max): %v", err)
195+
}
196+
if !sameColor(max, col[len(col)-1]) {
197+
t.Errorf("unexpected max color %#v != %#v", max, col[len(col)-1])
198+
}
199+
})
200+
}
201+
}
202+
203+
func sameColor(a, b color.Color) bool {
204+
ar, ag, ab, aa := a.RGBA()
205+
br, bg, bb, ba := b.RGBA()
206+
return ar == br && ag == bg && ab == bb && aa == ba
171207
}

palette/moreland/smooth.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,16 @@ func (p smoothDiverging) Palette(n int) palette.Palette {
171171
p.SetMax(1)
172172
}
173173
delta := (p.max - p.min) / float64(n-1)
174+
var v float64
174175
c := make([]color.Color, n)
175176
for i := range c {
176-
v := min(p.min+float64(delta*float64(i)), p.max)
177+
if i == n-1 {
178+
// Avoid potential overflow on last element
179+
// due to floating point error.
180+
v = p.max
181+
} else {
182+
v = p.min + float64(delta*float64(i))
183+
}
177184
var err error
178185
c[i], err = p.At(v)
179186
if err != nil {

palette/moreland/smooth_test.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,40 @@ func similar(a, b color.Color, tolerance float64) bool {
247247
return true
248248
}
249249

250+
// See https://github.com/gonum/plot/issues/798
250251
func TestIssue798SmoothBlueRed(t *testing.T) {
251-
// https://github.com/gonum/plot/issues/798
252-
colors := SmoothBlueRed()
253-
colors.SetMin(0.3402859786606234)
254-
colors.SetMax(15.322841335211892)
255-
colors.Palette(15)
252+
for _, test := range []struct {
253+
n int
254+
min, max float64
255+
}{
256+
0: {n: 2, min: 0, max: 1},
257+
1: {n: 15, min: 0.3402859786606234, max: 15.322841335211892},
258+
} {
259+
t.Run("", func(t *testing.T) {
260+
defer func() {
261+
r := recover()
262+
if r != nil {
263+
t.Errorf("unexpected panic with n=%d min=%f max=%f: %v", test.n, test.min, test.max, r)
264+
}
265+
}()
266+
colors := SmoothBlueRed()
267+
colors.SetMin(test.min)
268+
colors.SetMax(test.max)
269+
col := colors.Palette(test.n).Colors()
270+
min, err := colors.At(test.min)
271+
if err != nil {
272+
t.Fatalf("unexpected error calling colors.At(min): %v", err)
273+
}
274+
if !sameColor(min, col[0]) {
275+
t.Errorf("unexpected min color %#v != %#v", min, col[0])
276+
}
277+
max, err := colors.At(test.max)
278+
if err != nil {
279+
t.Fatalf("unexpected error calling colors.At(max): %v", err)
280+
}
281+
if !sameColor(max, col[len(col)-1]) {
282+
t.Errorf("unexpected max color %#v != %#v", max, col[len(col)-1])
283+
}
284+
})
285+
}
256286
}

0 commit comments

Comments
 (0)