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
This commit is contained in:
Russ Cox 2011-12-06 10:47:42 -05:00
parent bbbd41f4ff
commit e4e4cdb39a
2 changed files with 21 additions and 8 deletions

View File

@ -134,13 +134,22 @@ type Model interface {
Convert(c Color) Color Convert(c Color) Color
} }
// ModelFunc is an adapter type to allow the use of a color conversion // ModelFunc returns a Model that invokes f to implement the conversion.
// function as a Model. If f is such a function, ModelFunc(f) is a Model that func ModelFunc(f func(Color) Color) Model {
// invokes f to implement the conversion. // Note: using *modelFunc as the implementation
type ModelFunc func(Color) Color // 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}
}
func (f ModelFunc) Convert(c Color) Color { type modelFunc struct {
return f(c) f func(Color) Color
}
func (m *modelFunc) Convert(c Color) Color {
return m.f(c)
} }
// RGBAModel is the Model for RGBA colors. // RGBAModel is the Model for RGBA colors.

View File

@ -20,7 +20,7 @@ var (
) )
// Uniform is an infinite-sized Image of uniform color. // 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 { type Uniform struct {
C color.Color C color.Color
} }
@ -30,7 +30,11 @@ func (c *Uniform) RGBA() (r, g, b, a uint32) {
} }
func (c *Uniform) ColorModel() color.Model { 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}} } func (c *Uniform) Bounds() Rectangle { return Rectangle{Point{-1e9, -1e9}, Point{1e9, 1e9}} }