Skip to content

Commit 5eb6bf0

Browse files
committed
plotter: add special handling for image.Uniform
Fixes #752. Signed-off-by: Sebastien Binet <binet@cern.ch>
1 parent b4f15d2 commit 5eb6bf0

4 files changed

Lines changed: 41 additions & 0 deletions

File tree

plotter/image.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ type Image struct {
2727
// (xmin, ymin) and (xmax, ymax) points given in the data space.
2828
// The img will be scaled to fit inside the rectangle.
2929
func NewImage(img image.Image, xmin, ymin, xmax, ymax float64) *Image {
30+
if src, ok := img.(*image.Uniform); ok {
31+
img = uniform{
32+
src,
33+
image.Rect(0, 0, int(xmax-xmin+0.5), int(ymax-ymin+0.5)),
34+
}
35+
}
3036
bounds := img.Bounds()
3137
cols := bounds.Dx()
3238
rows := bounds.Dy()
@@ -126,3 +132,13 @@ func (img *Image) y(r int) float64 {
126132
}
127133
return img.ymin + float64(r)*img.dy
128134
}
135+
136+
// uniform is a cropped uniform image.
137+
type uniform struct {
138+
*image.Uniform
139+
rect image.Rectangle
140+
}
141+
142+
func (img uniform) Bounds() image.Rectangle {
143+
return img.rect
144+
}

plotter/image_example_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package plotter_test
66

77
import (
8+
"image"
9+
"image/color"
810
"image/png"
911
"log"
1012
"os"
@@ -79,3 +81,22 @@ func ExampleImage_log() {
7981
log.Fatalf("error saving image plot: %v\n", err)
8082
}
8183
}
84+
85+
// An example of embedding a uniform image in a plot.
86+
func ExampleImage_uniform() {
87+
p := plot.New()
88+
p.Title.Text = "Uniform image"
89+
90+
img := image.NewUniform(color.RGBA{R: 90, G: 155, B: 212, A: 255})
91+
p.Add(plotter.NewImage(img, 100, 100, 10000, 10000))
92+
93+
const (
94+
w = 5 * vg.Centimeter
95+
h = 5 * vg.Centimeter
96+
)
97+
98+
err := p.Save(w, h, "testdata/image_plot_uniform.png")
99+
if err != nil {
100+
log.Fatalf("error saving image plot: %v\n", err)
101+
}
102+
}

plotter/image_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ func TestImagePlot(t *testing.T) {
1717
func TestImagePlot_log(t *testing.T) {
1818
cmpimg.CheckPlot(ExampleImage_log, t, "image_plot_log.png")
1919
}
20+
21+
func TestImagePlot_uniform(t *testing.T) {
22+
cmpimg.CheckPlot(ExampleImage_uniform, t, "image_plot_uniform.png")
23+
}
6.37 KB
Loading

0 commit comments

Comments
 (0)