// ColorConverter compares a color to another. IF it matches, return // the new color. func ColorConverter(c1 color.Color, c2 color.Color) color.Model { return color.ModelFunc(func(c color.Color) color.Color { cR, cG, cB, cA := c.RGBA() c1R, c1G, c1B, c1A := c1.RGBA() if cR == c1R && cG == c1G && cB == c1B && cA == c1A { return c2 } return c }) }
// At implements image.Image.At func (p *Image) At(x, y int) color.Color { return p.RGBAAt(x, y) } // RGBAAt returns the color of the pixel at (x, y) as RGBA. func (p *Image) RGBAAt(x, y int) color.RGBA { if !(image.Point{x, y}.In(p.Rect)) { return color.RGBA{} } i := (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3 return color.RGBA{p.Pix[i+0], p.Pix[i+1], p.Pix[i+2], 0xFF} } // ColorModel is RGB color model instance var ColorModel = color.ModelFunc(rgbModel) func rgbModel(c color.Color) color.Color { if _, ok := c.(RGB); ok { return c } r, g, b, _ := c.RGBA() return RGB{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)} } // RGB color type RGB struct { R, G, B uint8 } // RGBA implements Color.RGBA
func (m Image) ColorModel() color.Model { return color.ModelFunc(m.rgbaColor) }
// 32-bit floating point RGBA color data type. type Color struct { R, G, B, A float32 } // Implements image/color.Color interface. func (c Color) RGBA() (r, g, b, a uint32) { r = uint32(c.R * fMaxUint16) g = uint32(c.G * fMaxUint16) b = uint32(c.B * fMaxUint16) a = uint32(c.A * fMaxUint16) return } func colorModel(c color.Color) color.Color { if _, ok := c.(Color); ok { return c } r, g, b, a := c.RGBA() return Color{ R: float32(r) / float32(fMaxUint16), G: float32(g) / float32(fMaxUint16), B: float32(b) / float32(fMaxUint16), A: float32(a) / float32(fMaxUint16), } } // ColorModel represents the graphics color model (i.e. normalized 32-bit // floating point values RGBA color). var ColorModel color.Model = color.ModelFunc(colorModel)
r |= r >> 8 g = uint32(c.BGRA) << 8 & 0xf000 g |= g >> 4 g |= g >> 8 b = uint32(c.BGRA) << 12 & 0xf000 b |= b >> 4 b |= b >> 8 a = uint32(c.BGRA) << 0 & 0xf000 a |= a >> 4 a |= a >> 8 return r, g, b, a } // Model for RGB565 and BGRA used by Dxt and GL var ( BGRAModel color.Model = color.ModelFunc(bgraModel) BGR565Model color.Model = color.ModelFunc(bgr565Model) BGRA5551Model color.Model = color.ModelFunc(bgra5551Model) BGRA4444Model color.Model = color.ModelFunc(bgra4444Model) ) func bgraModel(c color.Color) color.Color { if _, ok := c.(BGRA); ok { return c } r, g, b, a := c.RGBA() return BGRA{uint8(b >> 8), uint8(g >> 8), uint8(r >> 8), uint8(a >> 8)} } func bgr565Model(c color.Color) color.Color { if _, ok := c.(BGR565); ok {
func ColorModel(channels int, dataType reflect.Kind) color.Model { return color.ModelFunc(func(c color.Color) color.Color { return colorModelConvert(channels, dataType, c) }) }
// Copyright 2012 The Gorilla Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package color import ( "image/color" "math" ) // HSLModel converts any Color to a HSL color. var HSLModel = color.ModelFunc(hslModel) // HSL represents a cylindrical coordinate of points in an RGB color model. // // Values are in the range 0 to 1. type HSL struct { H, S, L float64 } // RGBA returns the alpha-premultiplied red, green, blue and alpha values // for the HSL. func (c HSL) RGBA() (uint32, uint32, uint32, uint32) { r, g, b := HSLToRGB(c.H, c.S, c.L) return uint32(r) * 0x101, uint32(g) * 0x101, uint32(b) * 0x101, 0xffff } // hslModel converts a Color to HSL. func hslModel(c color.Color) color.Color { if _, ok := c.(HSL); ok {
// Its contents can be found in the enclosed LICENSE file. package framebuffer import ( "image" "image/color" ) var RGB555Model = color.ModelFunc( func(c color.Color) color.Color { if _, ok := c.(RGBColor); ok { return c } r, g, b, _ := c.RGBA() return RGBColor{ uint8(r>>8) & mask5, uint8(g>>8) & mask5, uint8(b>>8) & mask5, } }) type RGB555 struct { Pix []byte Rect image.Rectangle Stride int } func (i *RGB555) Bounds() image.Rectangle { return i.Rect } func (i *RGB555) ColorModel() color.Model { return RGB555Model }
// Its contents can be found in the enclosed LICENSE file. package framebuffer import ( "image" "image/color" ) var RGB565Model = color.ModelFunc( func(c color.Color) color.Color { if _, ok := c.(RGBColor); ok { return c } r, g, b, _ := c.RGBA() return RGBColor{ uint8(r >> (8 + (8 - 5))), uint8(g >> (8 + (8 - 6))), uint8(b >> (8 + (8 - 5))), } }) type RGB565 struct { Pix []byte Rect image.Rectangle Stride int } func (i *RGB565) Bounds() image.Rectangle { return i.Rect } func (i *RGB565) ColorModel() color.Model { return RGB565Model }
func (s QuantizedColorSlice) Less(i, j int) bool { return uint16(s[i]) < uint16(s[j]) } func (s QuantizedColorSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } // QuantizedColorGenerator creates a new QuantizedColor from a given red, green, and blue value. var QuantizedColorGenerator = func(r, g, b uint8) QuantizedColor { quantizedRed := quantizeColorValue(r) quantizedGreen := quantizeColorValue(g) quantizedBlue := quantizeColorValue(b) return QuantizedColor((quantizedRed << (quantizeWordWidth + quantizeWordWidth)) | (quantizedGreen << quantizeWordWidth) | quantizedBlue) } // QuantizedColorModel is the color.Model for the QuantizedColor type. var QuantizedColorModel = color.ModelFunc(func(c color.Color) color.Color { if _, ok := c.(QuantizedColor); ok { return c } nrgba := color.NRGBAModel.Convert(c).(color.NRGBA) return QuantizedColorGenerator(nrgba.R, nrgba.G, nrgba.B) }) // QuantizedColor represents a reduced RGB color space. type QuantizedColor uint16 // RGBA implements the color.Color interface. func (q QuantizedColor) RGBA() (uint32, uint32, uint32, uint32) { r := uint32(q.ApproximateRed()) r |= r << 8 g := uint32(q.ApproximateGreen()) g |= g << 8 b := uint32(q.ApproximateBlue()) b |= b << 8
} // PackedRGB is the packed int representing the RGB value (ignores the alpha channel). func (c RGBAInt) PackedRGB() uint32 { return c.PackedRGBA() | (uint32(0xFF) << 24) } func (c RGBAInt) String() string { return fmt.Sprintf("0x%06s", strings.ToUpper(strconv.FormatUint(uint64(c.PackedRGBA()), 16))) } // RGBAIntModel is the color.Model for the RGBAInt type. var RGBAIntModel = color.ModelFunc(func(c color.Color) color.Color { if _, ok := c.(RGBAInt); ok { return c } nrgba := color.NRGBAModel.Convert(c).(color.NRGBA) return RGBAInt((uint32(nrgba.A) << 24) | (uint32(nrgba.R) << 16) | (uint32(nrgba.G) << 8) | uint32(nrgba.B)) }) // HSL represents the HSL value for an RGBA color. type HSL struct { H, S, L float64 A uint8 } // RGBA implements the color.Color interface. func (c HSL) RGBA() (uint32, uint32, uint32, uint32) { r, g, b := hslToRGB(c.H, c.S, c.L) return color.NRGBA{r, g, b, c.A}.RGBA() }
if littleEndian { self.Pix[i+0] = c1.B self.Pix[i+1] = c1.G self.Pix[i+2] = c1.R } else { self.Pix[i+1] = c1.R self.Pix[i+2] = c1.G self.Pix[i+3] = c1.B } } var BGRNColorModel = color.ModelFunc( func(c color.Color) color.Color { if _, ok := c.(BGRNColor); ok { return c } r, g, b, _ := c.RGBA() return BGRNColor{R: uint8(r >> 8), G: uint8(g >> 8), B: uint8(b >> 8)} }, ) type BGRNColor struct { B, G, R uint8 } func (c BGRNColor) RGBA() (r, g, b, a uint32) { r = uint32(c.R) r |= r << 8 g = uint32(c.G) g |= g << 8
func (c Color) ColorModel() color.Model { return color.ModelFunc(toColor) }
func (c YCbCrColor) RGBA() (uint32, uint32, uint32, uint32) { r, g, b := YCbCrToRGB(c.Y, c.Cb, c.Cr) return uint32(r) * 0x101, uint32(g) * 0x101, uint32(b) * 0x101, 0xffff } func toYCbCrColor(c color.Color) color.Color { if _, ok := c.(YCbCrColor); ok { return c } r, g, b, _ := c.RGBA() y, u, v := RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8)) return YCbCrColor{y, u, v} } // YCbCrColorModel is the color model for YCbCrColor. var YCbCrColorModel color.Model = color.ModelFunc(toYCbCrColor) // SubsampleRatio is the chroma subsample ratio used in a YCbCr image. type SubsampleRatio int const ( SubsampleRatio444 SubsampleRatio = iota SubsampleRatio422 SubsampleRatio420 ) // YCbCr is an in-memory image of YCbCr colors. There is one Y sample per pixel, // but each Cb and Cr sample can span one or more pixels. // YStride is the Y slice index delta between vertically adjacent pixels. // CStride is the Cb and Cr slice index delta between vertically adjacent pixels // that map to separate chroma samples.
} //Color returns a without the alpha channel. func (a AlphaColor) Color() Color { return Color{a.R, a.G, a.B} } //These models can convert any color.Color to themselves. // //The conversion may be lossy. var ( ColorModel = color.ModelFunc(func(c color.Color) color.Color { if c, ok := c.(Color); ok { return c } if c, ok := c.(AlphaColor); ok { return Color{c.R, c.G, c.B} } r, g, b, _ := c.RGBA() return Color{cto01(r), cto01(g), cto01(b)} }) AlphaColorModel = color.ModelFunc(func(c color.Color) color.Color { if c, ok := c.(AlphaColor); ok { return c } if c, ok := c.(Color); ok { return AlphaColor{c.R, c.G, c.B, 1} } r, g, b, a := c.RGBA() return AlphaColor{cto01(r), cto01(g), cto01(b), cto01(a)} }) )
for _, c := range cs { r += c.R g += c.G b += c.B } k := 1 / float64(len(cs)) r *= k g *= k b *= k return RGB{r, g, b} } var RGBModel = color.ModelFunc(rgbModel) func rgbModel(c color.Color) color.Color { if _, ok := c.(RGB); ok { return c } r, g, b, _ := c.RGBA() const k = 1 / float64(math.MaxUint16) return RGB{ R: float64(r) * k, G: float64(g) * k, B: float64(b) * k, } }
// CIgnore, 8, CRed, 8, CGreen, 8, CBlue, 8 return color.RGBA{buf[2], buf[1], buf[0], 0xFF} case XBGR32: // CIgnore, 8, CBlue, 8, CGreen, 8, CRed, 8 return color.RGBA{buf[0], buf[1], buf[2], 0xFF} default: panic("unknown color") } } func (i *Image) Bounds() image.Rectangle { return i.Clipr } var ( Gray1Model color.Model = color.ModelFunc(gray1Model) Gray2Model color.Model = color.ModelFunc(gray2Model) Gray4Model color.Model = color.ModelFunc(gray4Model) CMap8Model color.Model = color.ModelFunc(cmapModel) CRGB15Model color.Model = color.ModelFunc(crgb15Model) CRGB16Model color.Model = color.ModelFunc(crgb16Model) ) // Gray1 represents a 1-bit black/white color. type Gray1 struct { White bool } func (c Gray1) RGBA() (r, g, b, a uint32) { if c.White { return 0xffff, 0xffff, 0xffff, 0xffff
b = uint32(math.Floor(c.B * k)) a = uint32(math.Floor(c.A * k)) return } // Alpha blending // c = a over b func (a RGBA) Over(b RGBA) (c RGBA) { c.A = lerp(b.A, 1.0, a.A) c.R = lerp(b.R*b.A, a.R, a.A) / c.A c.G = lerp(b.G*b.A, a.G, a.A) / c.A c.B = lerp(b.B*b.A, a.B, a.A) / c.A return } var RGBAModel = color.ModelFunc(rgbaModel) func rgbaModel(c color.Color) color.Color { if _, ok := c.(RGBA); ok { return c } r, g, b, a := c.RGBA() const k = 1 / float64(math.MaxUint16) return RGBA{ R: float64(r) * k, G: float64(g) * k, B: float64(b) * k, A: float64(a) * k, } }
r = c g = 0 b = x } m := l - 0.5*c red = uint32(utils.Truncatef((r+m)*a*255)) << 8 green = uint32(utils.Truncatef((g+m)*a*255)) << 8 blue = uint32(utils.Truncatef((b+m)*a*255)) << 8 alpha = uint32(a*255) << 8 return } var HSLAModel color.Model = color.ModelFunc(hslaModel) func hslaModel(c color.Color) color.Color { if _, ok := c.(HSLA); ok { return c } r, g, b, a := utils.RatioRGBA(c) maxi := utils.Maxf(r, g, b) mini := utils.Minf(r, g, b) chroma := maxi - mini // Work out hue hdash := 0.0 if chroma == 0 {
type Color struct { color.YCbCr A uint8 } func (c Color) RGBA() (r, g, b, a uint32) { r8, g8, b8 := color.YCbCrToRGB(c.Y, c.Cb, c.Cr) a = uint32(c.A) * 0x101 r = uint32(r8) * 0x101 * a / 0xffff g = uint32(g8) * 0x101 * a / 0xffff b = uint32(b8) * 0x101 * a / 0xffff return } // ColorModel is the Model for non-alpha-premultiplied Y'CbCr-with-alpha colors. var ColorModel color.Model = color.ModelFunc(nYCbCrAModel) func nYCbCrAModel(c color.Color) color.Color { switch c := c.(type) { case Color: return c case color.YCbCr: return Color{c, 0xff} } r, g, b, a := c.RGBA() // Convert from alpha-premultiplied to non-alpha-premultiplied. if a != 0 { r = (r * 0xffff) / a g = (g * 0xffff) / a b = (b * 0xffff) / a
return } func graynaModel(c color.Color) color.Color { if _, ok := c.(GrayNA); ok { return c } r, g, b, a := c.RGBA() r = (r * 0xffff) / a g = (g * 0xffff) / a b = (b * 0xffff) / a y := (299*r + 587*g + 114*b + 500) / 1000 return GrayNA{uint8(y >> 8), uint8(a >> 8)} } var GrayNAModel color.Model = color.ModelFunc(graynaModel) /*****************************************************************************/ type GrayA struct { Y uint8 A uint8 } func (c GrayA) RGBA() (r, g, b, a uint32) { y := uint32(c.Y) y |= y << 8 a = uint32(c.A) a |= a << 8 r = y g = y
b = z } else { g = x b = y r = z } red = uint32(utils.Truncatef(r*a*255)) << 8 green = uint32(utils.Truncatef(g*a*255)) << 8 blue = uint32(utils.Truncatef(b*a*255)) << 8 alpha = uint32(a*255) << 8 return } var HSIAModel color.Model = color.ModelFunc(hsiaModel) func hsiaModel(c color.Color) color.Color { if _, ok := c.(HSIA); ok { return c } r, g, b, a := utils.RatioRGBA(c) maxi := utils.Maxf(r, g, b) mini := utils.Minf(r, g, b) chroma := maxi - mini // Work out hue hdash := 0.0 if chroma == 0 {
// Copyright 2012 The Gorilla Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package color import ( "image/color" "math" ) // HSVModel converts any Color to a HSV color. var HSVModel = color.ModelFunc(hsvModel) // HSV represents a cylindrical coordinate of points in an RGB color model. // // Values are in the range 0 to 1. type HSV struct { H, S, V float64 } // RGBA returns the alpha-premultiplied red, green, blue and alpha values // for the HSV. func (c HSV) RGBA() (uint32, uint32, uint32, uint32) { r, g, b := HSVToRGB(c.H, c.S, c.V) return uint32(r) * 0x101, uint32(g) * 0x101, uint32(b) * 0x101, 0xffff } // hsvModel converts a Color to HSV. func hsvModel(c color.Color) color.Color { if _, ok := c.(HSV); ok {
package image import ( "image/color" ) // A BinaryColor represents either black or white. type BinaryColor struct { Black bool } func (c BinaryColor) RGBA() (r, g, b, a uint32) { a = 0xffff if c.Black { return } return a, a, a, a } func toBinaryColor(c color.Color) color.Color { if _, ok := c.(BinaryColor); ok { return c } // should be some dithering r, g, b, _ := c.RGBA() return BinaryColor{(299*r+587*g+114*b+500)/1000 < 0x8000} } // The ColorModel associated with BinaryColor. var BinaryColorModel color.Model = color.ModelFunc(toBinaryColor)
// but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. import ( "image/color" "math" ) const maxChannelValue = float64(0xFFFF) // HSVAModel converts any color.Color to an HSVA color. var HSVAModel color.Model = color.ModelFunc(hsvaModel) func hsvaModel(c color.Color) color.Color { if _, ok := c.(HSVA); ok { return c } return RGBAtoHSVA(c.RGBA()) } // HSVAColor represents a Hue/Saturation/Value/Alpha color. // H is valid within [0°, 360°]. S, V and A are valid within [0, 1]. type HSVA struct { H, S, V, A float64 } // Convert r, g, b, a to HSVA
// From https://github.com/zond/wildlife/blob/master/code.google.com/p/gorilla/color/hex.go // Copyright 2012 The Gorilla Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package hex import ( "fmt" "image/color" "strconv" ) // HexModel converts any Color to an Hex color. var HexModel = color.ModelFunc(hexModel) // Hex represents an RGB color in hexadecimal format. // // The length must be 3 or 6 characters, preceded or not by a '#'. type Hex string // RGBA returns the alpha-premultiplied red, green, blue and alpha values // for the Hex. func (c Hex) RGBA() (uint32, uint32, uint32, uint32) { r, g, b := HexToRGB(c) return uint32(r) * 0x101, uint32(g) * 0x101, uint32(b) * 0x101, 0xffff } // hexModel converts a Color to Hex. func hexModel(c color.Color) color.Color {
import ( "image" "image/color" "image/png" "io" "os" "github.com/BurntSushi/graphics-go/graphics" "github.com/BurntSushi/xgb/xproto" "github.com/BurntSushi/xgbutil" "github.com/BurntSushi/xgbutil/xwindow" ) // Model for the BGRA color type. var BGRAModel color.Model = color.ModelFunc(bgraModel) type Image struct { // X images must be tied to an X connection. X *xgbutil.XUtil // X images must also be tied to a pixmap (its drawing surface). // Calls to 'XDraw' will draw data to this pixmap. // Calls to 'XPaint' will tell X to show the pixmap on some window. Pixmap xproto.Pixmap // Pix holds the image's pixels in BGRA order, so that they don't need // to be swapped for every PutImage request. Pix []uint8 // Stride corresponds to the number of elements in Pix between two pixels
w = b } if w == 0 { return NCMYKA80{0xffff, 0xffff, 0xffff, 0xffff, uint16(a)} } cc := (w - r) * 0xffff / w mm := (w - g) * 0xffff / w yy := (w - b) * 0xffff / w kk := 0xffff - w if a == 0xffff { return NCMYKA80{uint16(0xffff - cc), uint16(0xffff - mm), uint16(0xffff - yy), uint16(0xffff - kk), 0xffff} } cc = (cc * 0xffff) / a mm = (mm * 0xffff) / a yy = (yy * 0xffff) / a kk = (kk * 0xffff) / a return NCMYKA80{uint16(0xffff - cc), uint16(0xffff - mm), uint16(0xffff - yy), uint16(0xffff - kk), uint16(a)} } // These are color model. var ( Gray1Model = color.ModelFunc(gray1Model) NGrayAModel = color.ModelFunc(nGrayAModel) Gray32Model = color.ModelFunc(gray32Model) NGrayA32Model = color.ModelFunc(nGrayA32Model) NGrayA64Model = color.ModelFunc(nGrayA64Model) NRGBA128Model = color.ModelFunc(nRGBA128Model) NCMYKAModel = color.ModelFunc(nCMYKAModel) NCMYKA80Model = color.ModelFunc(nCMYKA80Model) )
if !(image.Point{x, y}.In(self.Rect)) { return } i := self.PixOffset(x, y) c1 := ARGBColorModel.Convert(c).(ARGBColor) self.Pix[i+0] = c1.A self.Pix[i+1] = c1.R self.Pix[i+2] = c1.G self.Pix[i+3] = c1.B } var ARGBColorModel = color.ModelFunc( func(c color.Color) color.Color { if _, ok := c.(ARGBColor); ok { return c } r, g, b, a := c.RGBA() return ARGBColor{uint8(a >> 8), uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)} }, ) // RGBA represents a traditional 32-bit alpha-premultiplied color, // having 8 bits for each of alpha, red, green and blue. type ARGBColor struct { A, R, G, B uint8 } func (c ARGBColor) RGBA() (r, g, b, a uint32) { r = uint32(c.R) r |= r << 8 g = uint32(c.G)
type Color struct { L, A, B float64 } func (p Color) RGBA() (uint32, uint32, uint32, uint32) { R, G, B := Lab2rgb(p.L, p.A, p.B) r := uint32(R) r |= r << 8 g := uint32(G) g |= g << 8 b := uint32(B) b |= b << 8 return r, g, b, 0xffff } var ColorModel color.Model = color.ModelFunc(labModel) func labModel(c color.Color) color.Color { if _, ok := c.(Color); ok { return c } var ( r, g, b, _ = c.RGBA() L, A, B = Rgb2lab(uint8(r>>8), uint8(g>>8), uint8(b>>8)) ) return Color{L, A, B} } type Image struct { Pix []float64 Stride int