mirror of
https://github.com/golang/go
synced 2024-11-21 22:54:40 -07:00
image/color: simplify documentation
R=nigeltao, dsymonds, adg CC=golang-dev https://golang.org/cl/5544073
This commit is contained in:
parent
0a97ef8f71
commit
cdf7654062
@ -152,26 +152,35 @@ func (m *modelFunc) Convert(c Color) Color {
|
||||
return m.f(c)
|
||||
}
|
||||
|
||||
// RGBAModel is the Model for RGBA colors.
|
||||
var RGBAModel Model = ModelFunc(func(c Color) Color {
|
||||
// Models for the standard color types.
|
||||
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 {
|
||||
return c
|
||||
}
|
||||
r, g, b, a := c.RGBA()
|
||||
return RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
|
||||
})
|
||||
}
|
||||
|
||||
// RGBAModel is the Model for RGBA64 colors.
|
||||
var RGBA64Model Model = ModelFunc(func(c Color) Color {
|
||||
func rgba64Model(c Color) Color {
|
||||
if _, ok := c.(RGBA64); ok {
|
||||
return c
|
||||
}
|
||||
r, g, b, a := c.RGBA()
|
||||
return RGBA64{uint16(r), uint16(g), uint16(b), uint16(a)}
|
||||
})
|
||||
}
|
||||
|
||||
// NRGBAModel is the Model for NRGBA colors.
|
||||
var NRGBAModel Model = ModelFunc(func(c Color) Color {
|
||||
func nrgbaModel(c Color) Color {
|
||||
if _, ok := c.(NRGBA); ok {
|
||||
return c
|
||||
}
|
||||
@ -187,10 +196,9 @@ var NRGBAModel Model = ModelFunc(func(c Color) Color {
|
||||
g = (g * 0xffff) / a
|
||||
b = (b * 0xffff) / a
|
||||
return NRGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
|
||||
})
|
||||
}
|
||||
|
||||
// NRGBAModel is the Model for NRGBA64 colors.
|
||||
var NRGBA64Model Model = ModelFunc(func(c Color) Color {
|
||||
func nrgba64Model(c Color) Color {
|
||||
if _, ok := c.(NRGBA64); ok {
|
||||
return c
|
||||
}
|
||||
@ -206,45 +214,41 @@ var NRGBA64Model Model = ModelFunc(func(c Color) Color {
|
||||
g = (g * 0xffff) / a
|
||||
b = (b * 0xffff) / a
|
||||
return NRGBA64{uint16(r), uint16(g), uint16(b), uint16(a)}
|
||||
})
|
||||
}
|
||||
|
||||
// AlphaModel is the Model for Alpha colors.
|
||||
var AlphaModel Model = ModelFunc(func(c Color) Color {
|
||||
func alphaModel(c Color) Color {
|
||||
if _, ok := c.(Alpha); ok {
|
||||
return c
|
||||
}
|
||||
_, _, _, a := c.RGBA()
|
||||
return Alpha{uint8(a >> 8)}
|
||||
})
|
||||
}
|
||||
|
||||
// Alpha16Model is the Model for Alpha16 colors.
|
||||
var Alpha16Model Model = ModelFunc(func(c Color) Color {
|
||||
func alpha16Model(c Color) Color {
|
||||
if _, ok := c.(Alpha16); ok {
|
||||
return c
|
||||
}
|
||||
_, _, _, a := c.RGBA()
|
||||
return Alpha16{uint16(a)}
|
||||
})
|
||||
}
|
||||
|
||||
// GrayModel is the Model for Gray colors.
|
||||
var GrayModel Model = ModelFunc(func(c Color) Color {
|
||||
func grayModel(c Color) Color {
|
||||
if _, ok := c.(Gray); ok {
|
||||
return c
|
||||
}
|
||||
r, g, b, _ := c.RGBA()
|
||||
y := (299*r + 587*g + 114*b + 500) / 1000
|
||||
return Gray{uint8(y >> 8)}
|
||||
})
|
||||
}
|
||||
|
||||
// Gray16Model is the Model for Gray16 colors.
|
||||
var Gray16Model Model = ModelFunc(func(c Color) Color {
|
||||
func gray16Model(c Color) Color {
|
||||
if _, ok := c.(Gray16); ok {
|
||||
return c
|
||||
}
|
||||
r, g, b, _ := c.RGBA()
|
||||
y := (299*r + 587*g + 114*b + 500) / 1000
|
||||
return Gray16{uint16(y)}
|
||||
})
|
||||
}
|
||||
|
||||
// Palette is a palette of colors.
|
||||
type Palette []Color
|
||||
@ -290,13 +294,10 @@ func (p Palette) Index(c Color) int {
|
||||
return ret
|
||||
}
|
||||
|
||||
// Standard colors.
|
||||
var (
|
||||
// Black is an opaque black Color.
|
||||
Black = Gray16{0}
|
||||
// White is an opaque white Color.
|
||||
White = Gray16{0xffff}
|
||||
// Transparent is a fully transparent Color.
|
||||
Black = Gray16{0}
|
||||
White = Gray16{0xffff}
|
||||
Transparent = Alpha16{0}
|
||||
// Opaque is a fully opaque Color.
|
||||
Opaque = Alpha16{0xffff}
|
||||
Opaque = Alpha16{0xffff}
|
||||
)
|
||||
|
@ -4,8 +4,7 @@
|
||||
|
||||
package color
|
||||
|
||||
// RGBToYCbCr converts an RGB triple to a Y'CbCr triple. All components lie
|
||||
// within the range [0, 255].
|
||||
// RGBToYCbCr converts an RGB triple to a Y'CbCr triple.
|
||||
func RGBToYCbCr(r, g, b uint8) (uint8, uint8, uint8) {
|
||||
// The JFIF specification says:
|
||||
// Y' = 0.2990*R + 0.5870*G + 0.1140*B
|
||||
@ -36,8 +35,7 @@ func RGBToYCbCr(r, g, b uint8) (uint8, uint8, uint8) {
|
||||
return uint8(yy), uint8(cb), uint8(cr)
|
||||
}
|
||||
|
||||
// YCbCrToRGB converts a Y'CbCr triple to an RGB triple. All components lie
|
||||
// within the range [0, 255].
|
||||
// YCbCrToRGB converts a Y'CbCr triple to an RGB triple.
|
||||
func YCbCrToRGB(y, cb, cr uint8) (uint8, uint8, uint8) {
|
||||
// The JFIF specification says:
|
||||
// R = Y' + 1.40200*(Cr-128)
|
||||
@ -89,11 +87,13 @@ func (c YCbCr) RGBA() (uint32, uint32, uint32, uint32) {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return c
|
||||
}
|
||||
r, g, b, _ := c.RGBA()
|
||||
y, u, v := RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8))
|
||||
return YCbCr{y, u, v}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user