aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2011-12-06 10:47:42 -0500
committerRuss Cox <rsc@golang.org>2011-12-06 10:47:42 -0500
commite4e4cdb39a821eb850200967cb21ab36e8d3a1be (patch)
treeb2049d669058aa7c3d69148528138294494bba69
parentbbbd41f4fff790e9a340a4be77c3c05f37491273 (diff)
downloadgo-e4e4cdb39a821eb850200967cb21ab36e8d3a1be.tar.gz
go-e4e4cdb39a821eb850200967cb21ab36e8d3a1be.zip
image: avoid func comparison during ColorModel comparison
When I disallowed map + func comparisons, I only did it in the static case and missed the comparisons via == on interface values. Fixing that turned these up. R=nigeltao, r CC=golang-dev https://golang.org/cl/5440103
-rw-r--r--src/pkg/image/color/color.go21
-rw-r--r--src/pkg/image/names.go8
2 files changed, 21 insertions, 8 deletions
diff --git a/src/pkg/image/color/color.go b/src/pkg/image/color/color.go
index 4a0fae5a78..2948db7f38 100644
--- a/src/pkg/image/color/color.go
+++ b/src/pkg/image/color/color.go
@@ -134,13 +134,22 @@ type Model interface {
Convert(c Color) Color
}
-// ModelFunc is an adapter type to allow the use of a color conversion
-// function as a Model. If f is such a function, ModelFunc(f) is a Model that
-// invokes f to implement the conversion.
-type ModelFunc func(Color) Color
+// ModelFunc returns a Model that invokes f to implement the conversion.
+func ModelFunc(f func(Color) Color) Model {
+ // Note: using *modelFunc as the implementation
+ // means that callers can still use comparisons
+ // like m == RGBAModel. This is not possible if
+ // we use the func value directly, because funcs
+ // are no longer comparable.
+ return &modelFunc{f}
+}
+
+type modelFunc struct {
+ f func(Color) Color
+}
-func (f ModelFunc) Convert(c Color) Color {
- return f(c)
+func (m *modelFunc) Convert(c Color) Color {
+ return m.f(c)
}
// RGBAModel is the Model for RGBA colors.
diff --git a/src/pkg/image/names.go b/src/pkg/image/names.go
index a7ad51d537..a7d1a57983 100644
--- a/src/pkg/image/names.go
+++ b/src/pkg/image/names.go
@@ -20,7 +20,7 @@ var (
)
// Uniform is an infinite-sized Image of uniform color.
-// It implements both the color.Color and Image interfaces.
+// It implements the color.Color, color.ColorModel, and Image interfaces.
type Uniform struct {
C color.Color
}
@@ -30,7 +30,11 @@ func (c *Uniform) RGBA() (r, g, b, a uint32) {
}
func (c *Uniform) ColorModel() color.Model {
- return color.ModelFunc(func(color.Color) color.Color { return c.C })
+ return c
+}
+
+func (c *Uniform) Convert(color.Color) color.Color {
+ return c.C
}
func (c *Uniform) Bounds() Rectangle { return Rectangle{Point{-1e9, -1e9}, Point{1e9, 1e9}} }