Skip to content

Commit 8dab18a

Browse files
committed
palette/moreland: fix floating-point arithmetic error in Palette
Fixes #798. Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 2815ea1 commit 8dab18a

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

palette/moreland/luminance.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,9 @@ 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
180179
c := make([]color.Color, n)
181180
for i := range n {
182-
v = l.min + float64(delta*float64(i))
181+
v := min(l.min+float64(delta*float64(i)), l.max)
183182
var err error
184183
c[i], err = l.At(v)
185184
if err != nil {

palette/moreland/luminance_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,11 @@ func BenchmarkLuminance_At(b *testing.B) {
161161
})
162162
}
163163
}
164+
165+
func Test_luminance_Palette(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)
171+
}

palette/moreland/smooth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (p smoothDiverging) Palette(n int) palette.Palette {
173173
delta := (p.max - p.min) / float64(n-1)
174174
c := make([]color.Color, n)
175175
for i := range c {
176-
v := p.min + float64(delta*float64(i))
176+
v := min(p.min+float64(delta*float64(i)), p.max)
177177
var err error
178178
c[i], err = p.At(v)
179179
if err != nil {

palette/moreland/smooth_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,11 @@ func similar(a, b color.Color, tolerance float64) bool {
246246
}
247247
return true
248248
}
249+
250+
func Test_smoothDiverging_Palette(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)
256+
}

0 commit comments

Comments
 (0)