Commit cdf76540 authored by David Crawshaw's avatar David Crawshaw Committed by Nigel Tao

image/color: simplify documentation

R=nigeltao, dsymonds, adg
CC=golang-dev
https://golang.org/cl/5544073
parent 0a97ef8f
...@@ -152,26 +152,35 @@ func (m *modelFunc) Convert(c Color) Color { ...@@ -152,26 +152,35 @@ func (m *modelFunc) Convert(c Color) Color {
return m.f(c) return m.f(c)
} }
// RGBAModel is the Model for RGBA colors. // Models for the standard color types.
var RGBAModel Model = ModelFunc(func(c Color) Color { var (
RGBAModel Model = ModelFunc(rgbaModel)
RGBA64Model Model = ModelFunc(rgba64Model)
NRGBAModel Model = ModelFunc(nrgbaModel)
NRGBA64Model Model = ModelFunc(nrgba64Model)
AlphaModel Model = ModelFunc(alphaModel)
Alpha16Model Model = ModelFunc(alpha16Model)
GrayModel Model = ModelFunc(grayModel)
Gray16Model Model = ModelFunc(gray16Model)
)
func rgbaModel(c Color) Color {
if _, ok := c.(RGBA); ok { if _, ok := c.(RGBA); ok {
return c return c
} }
r, g, b, a := c.RGBA() r, g, b, a := c.RGBA()
return RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)} return RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
}) }
// RGBAModel is the Model for RGBA64 colors. func rgba64Model(c Color) Color {
var RGBA64Model Model = ModelFunc(func(c Color) Color {
if _, ok := c.(RGBA64); ok { if _, ok := c.(RGBA64); ok {
return c return c
} }
r, g, b, a := c.RGBA() r, g, b, a := c.RGBA()
return RGBA64{uint16(r), uint16(g), uint16(b), uint16(a)} return RGBA64{uint16(r), uint16(g), uint16(b), uint16(a)}
}) }
// NRGBAModel is the Model for NRGBA colors. func nrgbaModel(c Color) Color {
var NRGBAModel Model = ModelFunc(func(c Color) Color {
if _, ok := c.(NRGBA); ok { if _, ok := c.(NRGBA); ok {
return c return c
} }
...@@ -187,10 +196,9 @@ var NRGBAModel Model = ModelFunc(func(c Color) Color { ...@@ -187,10 +196,9 @@ var NRGBAModel Model = ModelFunc(func(c Color) Color {
g = (g * 0xffff) / a g = (g * 0xffff) / a
b = (b * 0xffff) / a b = (b * 0xffff) / a
return NRGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)} return NRGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
}) }
// NRGBAModel is the Model for NRGBA64 colors. func nrgba64Model(c Color) Color {
var NRGBA64Model Model = ModelFunc(func(c Color) Color {
if _, ok := c.(NRGBA64); ok { if _, ok := c.(NRGBA64); ok {
return c return c
} }
...@@ -206,45 +214,41 @@ var NRGBA64Model Model = ModelFunc(func(c Color) Color { ...@@ -206,45 +214,41 @@ var NRGBA64Model Model = ModelFunc(func(c Color) Color {
g = (g * 0xffff) / a g = (g * 0xffff) / a
b = (b * 0xffff) / a b = (b * 0xffff) / a
return NRGBA64{uint16(r), uint16(g), uint16(b), uint16(a)} return NRGBA64{uint16(r), uint16(g), uint16(b), uint16(a)}
}) }
// AlphaModel is the Model for Alpha colors. func alphaModel(c Color) Color {
var AlphaModel Model = ModelFunc(func(c Color) Color {
if _, ok := c.(Alpha); ok { if _, ok := c.(Alpha); ok {
return c return c
} }
_, _, _, a := c.RGBA() _, _, _, a := c.RGBA()
return Alpha{uint8(a >> 8)} return Alpha{uint8(a >> 8)}
}) }
// Alpha16Model is the Model for Alpha16 colors. func alpha16Model(c Color) Color {
var Alpha16Model Model = ModelFunc(func(c Color) Color {
if _, ok := c.(Alpha16); ok { if _, ok := c.(Alpha16); ok {
return c return c
} }
_, _, _, a := c.RGBA() _, _, _, a := c.RGBA()
return Alpha16{uint16(a)} return Alpha16{uint16(a)}
}) }
// GrayModel is the Model for Gray colors. func grayModel(c Color) Color {
var GrayModel Model = ModelFunc(func(c Color) Color {
if _, ok := c.(Gray); ok { if _, ok := c.(Gray); ok {
return c return c
} }
r, g, b, _ := c.RGBA() r, g, b, _ := c.RGBA()
y := (299*r + 587*g + 114*b + 500) / 1000 y := (299*r + 587*g + 114*b + 500) / 1000
return Gray{uint8(y >> 8)} return Gray{uint8(y >> 8)}
}) }
// Gray16Model is the Model for Gray16 colors. func gray16Model(c Color) Color {
var Gray16Model Model = ModelFunc(func(c Color) Color {
if _, ok := c.(Gray16); ok { if _, ok := c.(Gray16); ok {
return c return c
} }
r, g, b, _ := c.RGBA() r, g, b, _ := c.RGBA()
y := (299*r + 587*g + 114*b + 500) / 1000 y := (299*r + 587*g + 114*b + 500) / 1000
return Gray16{uint16(y)} return Gray16{uint16(y)}
}) }
// Palette is a palette of colors. // Palette is a palette of colors.
type Palette []Color type Palette []Color
...@@ -290,13 +294,10 @@ func (p Palette) Index(c Color) int { ...@@ -290,13 +294,10 @@ func (p Palette) Index(c Color) int {
return ret return ret
} }
// Standard colors.
var ( var (
// Black is an opaque black Color.
Black = Gray16{0} Black = Gray16{0}
// White is an opaque white Color.
White = Gray16{0xffff} White = Gray16{0xffff}
// Transparent is a fully transparent Color.
Transparent = Alpha16{0} Transparent = Alpha16{0}
// Opaque is a fully opaque Color.
Opaque = Alpha16{0xffff} Opaque = Alpha16{0xffff}
) )
...@@ -4,8 +4,7 @@ ...@@ -4,8 +4,7 @@
package color package color
// RGBToYCbCr converts an RGB triple to a Y'CbCr triple. All components lie // RGBToYCbCr converts an RGB triple to a Y'CbCr triple.
// within the range [0, 255].
func RGBToYCbCr(r, g, b uint8) (uint8, uint8, uint8) { func RGBToYCbCr(r, g, b uint8) (uint8, uint8, uint8) {
// The JFIF specification says: // The JFIF specification says:
// Y' = 0.2990*R + 0.5870*G + 0.1140*B // Y' = 0.2990*R + 0.5870*G + 0.1140*B
...@@ -36,8 +35,7 @@ func RGBToYCbCr(r, g, b uint8) (uint8, uint8, uint8) { ...@@ -36,8 +35,7 @@ func RGBToYCbCr(r, g, b uint8) (uint8, uint8, uint8) {
return uint8(yy), uint8(cb), uint8(cr) return uint8(yy), uint8(cb), uint8(cr)
} }
// YCbCrToRGB converts a Y'CbCr triple to an RGB triple. All components lie // YCbCrToRGB converts a Y'CbCr triple to an RGB triple.
// within the range [0, 255].
func YCbCrToRGB(y, cb, cr uint8) (uint8, uint8, uint8) { func YCbCrToRGB(y, cb, cr uint8) (uint8, uint8, uint8) {
// The JFIF specification says: // The JFIF specification says:
// R = Y' + 1.40200*(Cr-128) // R = Y' + 1.40200*(Cr-128)
...@@ -89,11 +87,13 @@ func (c YCbCr) RGBA() (uint32, uint32, uint32, uint32) { ...@@ -89,11 +87,13 @@ func (c YCbCr) RGBA() (uint32, uint32, uint32, uint32) {
} }
// YCbCrModel is the Model for Y'CbCr colors. // YCbCrModel is the Model for Y'CbCr colors.
var YCbCrModel Model = ModelFunc(func(c Color) Color { var YCbCrModel Model = ModelFunc(modelYCbCr)
func modelYCbCr(c Color) Color {
if _, ok := c.(YCbCr); ok { if _, ok := c.(YCbCr); ok {
return c return c
} }
r, g, b, _ := c.RGBA() r, g, b, _ := c.RGBA()
y, u, v := RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8)) y, u, v := RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8))
return YCbCr{y, u, v} return YCbCr{y, u, v}
}) }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment