github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/color/color.go (about)

     1  // Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package color implements a basic color library.
     6  package color
     7  
     8  import (
     9  	"image/color"
    10  )
    11  
    12  // Gray32f represents a float32 grayscale color.
    13  type Gray32f struct {
    14  	Y float32
    15  }
    16  
    17  func (c Gray32f) RGBA() (r, g, b, a uint32) {
    18  	y := uint32(f32ToU16(c.Y))
    19  	return y, y, y, 0xffff
    20  }
    21  
    22  // RGB represents a traditional 24-bit fully opaque color,
    23  // having 8 bits for each of red, green and blue.
    24  type RGB struct {
    25  	R, G, B uint8
    26  }
    27  
    28  func (c RGB) RGBA() (r, g, b, a uint32) {
    29  	r = uint32(c.R)
    30  	r |= r << 8
    31  	g = uint32(c.G)
    32  	g |= g << 8
    33  	b = uint32(c.B)
    34  	b |= b << 8
    35  	a = 0xFFFF
    36  	return
    37  }
    38  
    39  // RGB48 represents a 48-bit fully opaque color,
    40  // having 16 bits for each of red, green and blue.
    41  type RGB48 struct {
    42  	R, G, B uint16
    43  }
    44  
    45  func (c RGB48) RGBA() (r, g, b, a uint32) {
    46  	return uint32(c.R), uint32(c.G), uint32(c.B), 0xFFFF
    47  }
    48  
    49  // RGB96f represents a 48-bit fully opaque color,
    50  // having float32 for each of red, green and blue.
    51  type RGB96f struct {
    52  	R, G, B float32
    53  }
    54  
    55  func (c RGB96f) RGBA() (r, g, b, a uint32) {
    56  	r = uint32(f32ToU16(c.R))
    57  	g = uint32(f32ToU16(c.G))
    58  	b = uint32(f32ToU16(c.B))
    59  	a = 0xFFFF
    60  	return
    61  }
    62  
    63  // RGBA128f represents a 64-bit alpha-premultiplied color,
    64  // having float32 for each of red, green, blue and alpha.
    65  type RGBA128f struct {
    66  	R, G, B, A float32
    67  }
    68  
    69  func (c RGBA128f) RGBA() (r, g, b, a uint32) {
    70  	r = uint32(f32ToU16(c.R))
    71  	g = uint32(f32ToU16(c.G))
    72  	b = uint32(f32ToU16(c.B))
    73  	a = uint32(f32ToU16(c.A))
    74  	return
    75  }
    76  
    77  // Models for the standard color types.
    78  var (
    79  	Gray32fModel  color.Model = color.ModelFunc(gray32fModel)
    80  	RGBModel      color.Model = color.ModelFunc(rgbModel)
    81  	RGB48Model    color.Model = color.ModelFunc(rgb48Model)
    82  	RGB96fModel   color.Model = color.ModelFunc(rgb96fModel)
    83  	RGBA128fModel color.Model = color.ModelFunc(rgba128fModel)
    84  )
    85  
    86  func gray32fModel(c color.Color) color.Color {
    87  	switch c := c.(type) {
    88  	case Gray32f:
    89  		return c
    90  	case RGB96f:
    91  		y := (299*c.R + 587*c.G + 114*c.B + 500) / 1000
    92  		return Gray32f{float32(y)}
    93  	case RGBA128f:
    94  		y := (299*c.R + 587*c.G + 114*c.B + 500) / 1000
    95  		return Gray32f{float32(y)}
    96  	default:
    97  		r, g, b, _ := c.RGBA()
    98  		y := (299*r + 587*g + 114*b + 500) / 1000
    99  		return Gray32f{float32(y)}
   100  	}
   101  }
   102  
   103  func rgbModel(c color.Color) color.Color {
   104  	if _, ok := c.(RGB); ok {
   105  		return c
   106  	}
   107  	r, g, b, _ := c.RGBA()
   108  	return RGB{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
   109  }
   110  
   111  func rgb48Model(c color.Color) color.Color {
   112  	if _, ok := c.(RGB48); ok {
   113  		return c
   114  	}
   115  	r, g, b, _ := c.RGBA()
   116  	return RGB48{uint16(r), uint16(g), uint16(b)}
   117  }
   118  
   119  func rgb96fModel(c color.Color) color.Color {
   120  	switch c := c.(type) {
   121  	case Gray32f:
   122  		return RGB96f{c.Y, c.Y, c.Y}
   123  	case RGB96f:
   124  		return c
   125  	case RGBA128f:
   126  		return RGB96f{c.R, c.G, c.B}
   127  	default:
   128  		r, g, b, _ := c.RGBA()
   129  		return RGB96f{float32(r), float32(g), float32(b)}
   130  	}
   131  }
   132  
   133  func rgba128fModel(c color.Color) color.Color {
   134  	switch c := c.(type) {
   135  	case Gray32f:
   136  		return RGBA128f{c.Y, c.Y, c.Y, 0xFFFF}
   137  	case RGB96f:
   138  		return RGBA128f{c.R, c.G, c.B, 0xFFFF}
   139  	case RGBA128f:
   140  		return c
   141  	default:
   142  		r, g, b, a := c.RGBA()
   143  		return RGBA128f{float32(r), float32(g), float32(b), float32(a)}
   144  	}
   145  }