diff --git a/plotter/colorbar_test.go b/plotter/colorbar_test.go index f2c11437..37892b56 100644 --- a/plotter/colorbar_test.go +++ b/plotter/colorbar_test.go @@ -5,6 +5,7 @@ package plotter import ( + "image/color" "log" "testing" @@ -41,7 +42,11 @@ func ExampleColorBar_horizontal_log() { if err != nil { log.Panic(err) } - l := &ColorBar{ColorMap: moreland.ExtendedBlackBody()} + colorMap, err := moreland.NewLuminance([]color.Color{color.Black, color.White}) + if err != nil { + log.Panic(err) + } + l := &ColorBar{ColorMap: colorMap} l.ColorMap.SetMin(1) l.ColorMap.SetMax(100) p.Add(l) diff --git a/plotter/image.go b/plotter/image.go index cd154d61..86b36fb2 100644 --- a/plotter/image.go +++ b/plotter/image.go @@ -56,7 +56,7 @@ func (img *Image) Plot(c draw.Canvas, p *plot.Plot) { Min: vg.Point{X: xmin, Y: ymin}, Max: vg.Point{X: xmax, Y: ymax}, } - c.DrawImage(rect, img.transform(p)) + c.DrawImage(rect, img.transformFor(p)) } // DataRange implements the DataRange method @@ -72,7 +72,7 @@ func (img *Image) GlyphBoxes(plt *plot.Plot) []plot.GlyphBox { } // transform warps the image to align with non-linear axes. -func (img *Image) transform(p *plot.Plot) image.Image { +func (img *Image) transformFor(p *plot.Plot) image.Image { _, xLinear := p.X.Scale.(plot.LinearScale) _, yLinear := p.Y.Scale.(plot.LinearScale) if xLinear && yLinear { @@ -81,21 +81,24 @@ func (img *Image) transform(p *plot.Plot) image.Image { b := img.img.Bounds() o := image.NewNRGBA64(b) for c := 0; c < img.cols; c++ { + // Find the equivalent image column after applying axis transforms. + cTrans := int(p.X.Norm(img.x(c)) * float64(img.cols)) + // Find the equivalent column of the previous image column after applying + // axis transforms. + cPrevTrans := int(p.X.Norm(img.x(maxInt(c-1, 0))) * float64(img.cols)) for r := 0; r < img.rows; r++ { - // Find the equivalent image row and column after applying - // the axis transforms. - cTrans := int(p.X.Norm(img.x(c)) * float64(img.cols)) + // Find the equivalent image row after applying axis transforms. rTrans := int(p.Y.Norm(img.y(r)) * float64(img.rows)) - // Find the equivalent row and column of the previous image row - // and column after applying the axis transforms. - cPrevTrans := int(p.X.Norm(img.x(maxInt(c-1, 0))) * float64(img.cols)) + // Find the equivalent row of the previous image row after applying + // axis transforms. rPrevTrans := int(p.Y.Norm(img.y(maxInt(r-1, 0))) * float64(img.rows)) - crColor := img.img.At(c, r) + crColor := img.img.At(c, img.rows-r-1) // Set all the pixels in the new image between (cPrevTrans, rPrevTrans) // and (cTrans, rTrans) to the color at (c,r) in the original image. + // TODO: Improve interpolation. for cPrime := cPrevTrans; cPrime <= cTrans; cPrime++ { for rPrime := rPrevTrans; rPrime <= rTrans; rPrime++ { - o.Set(cPrime, rPrime, crColor) + o.Set(cPrime, img.rows-rPrime-1, crColor) } } } diff --git a/plotter/image_test.go b/plotter/image_test.go index dc4a8b84..f89a6b9e 100644 --- a/plotter/image_test.go +++ b/plotter/image_test.go @@ -63,7 +63,7 @@ func ExampleImage_log() { p.Title.Text = "A Logo" // load an image - f, err := os.Open("testdata/image_plot_input.png") + f, err := os.Open("../../gonum/gopher.png") if err != nil { log.Fatalf("error opening image file: %v\n", err) } diff --git a/plotter/testdata/colorBarHorizontalLog_golden.png b/plotter/testdata/colorBarHorizontalLog_golden.png index 6a7f3749..9df45360 100644 Binary files a/plotter/testdata/colorBarHorizontalLog_golden.png and b/plotter/testdata/colorBarHorizontalLog_golden.png differ diff --git a/plotter/testdata/image_plot_log_golden.png b/plotter/testdata/image_plot_log_golden.png index 4458bc51..56686b53 100644 Binary files a/plotter/testdata/image_plot_log_golden.png and b/plotter/testdata/image_plot_log_golden.png differ