2016-12-23 13:25:47 -06:00
|
|
|
package pixel
|
|
|
|
|
|
|
|
import "image/color"
|
|
|
|
|
2017-04-09 15:00:26 -05:00
|
|
|
// RGBA represents a alpha-premultiplied RGBA color with components within range [0, 1].
|
2016-12-23 13:25:47 -06:00
|
|
|
//
|
2017-04-09 15:00:26 -05:00
|
|
|
// The difference between color.RGBA is that the value range is [0, 1] and the values are floats.
|
|
|
|
type RGBA struct {
|
2016-12-23 13:25:47 -06:00
|
|
|
R, G, B, A float64
|
|
|
|
}
|
|
|
|
|
2017-04-09 16:03:30 -05:00
|
|
|
// RGB returns a fully opaque RGBA color with the given RGB values.
|
2017-04-09 16:08:35 -05:00
|
|
|
//
|
|
|
|
// A common way to construct a transparent color is to create one with RGB constructor, then
|
|
|
|
// multiply it by a color obtained from the Alpha constructor.
|
2017-04-09 16:03:30 -05:00
|
|
|
func RGB(r, g, b float64) RGBA {
|
|
|
|
return RGBA{r, g, b, 1}
|
|
|
|
}
|
|
|
|
|
2017-04-09 16:08:35 -05:00
|
|
|
// Alpha returns a while RGBA color with the given alpha component.
|
2017-04-09 16:03:30 -05:00
|
|
|
func Alpha(a float64) RGBA {
|
|
|
|
return RGBA{a, a, a, a}
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// Add adds color d to color c component-wise and returns the result (the components are not
|
|
|
|
// clamped).
|
2017-04-09 15:00:26 -05:00
|
|
|
func (c RGBA) Add(d RGBA) RGBA {
|
|
|
|
return RGBA{
|
2016-12-23 13:25:47 -06:00
|
|
|
R: c.R + d.R,
|
|
|
|
G: c.G + d.G,
|
|
|
|
B: c.B + d.B,
|
|
|
|
A: c.A + d.A,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// Sub subtracts color d from color c component-wise and returns the result (the components
|
|
|
|
// are not clamped).
|
2017-04-09 15:00:26 -05:00
|
|
|
func (c RGBA) Sub(d RGBA) RGBA {
|
|
|
|
return RGBA{
|
2016-12-23 13:25:47 -06:00
|
|
|
R: c.R - d.R,
|
|
|
|
G: c.G - d.G,
|
|
|
|
B: c.B - d.B,
|
|
|
|
A: c.A - d.A,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mul multiplies color c by color d component-wise (the components are not clamped).
|
2017-04-09 15:00:26 -05:00
|
|
|
func (c RGBA) Mul(d RGBA) RGBA {
|
|
|
|
return RGBA{
|
2016-12-23 13:25:47 -06:00
|
|
|
R: c.R * d.R,
|
|
|
|
G: c.G * d.G,
|
|
|
|
B: c.B * d.B,
|
|
|
|
A: c.A * d.A,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// Scaled multiplies each component of color c by scale and returns the result (the components
|
|
|
|
// are not clamped).
|
2017-04-09 15:00:26 -05:00
|
|
|
func (c RGBA) Scaled(scale float64) RGBA {
|
|
|
|
return RGBA{
|
2016-12-23 13:25:47 -06:00
|
|
|
R: c.R * scale,
|
|
|
|
G: c.G * scale,
|
|
|
|
B: c.B * scale,
|
|
|
|
A: c.A * scale,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-09 15:00:26 -05:00
|
|
|
// RGBA returns alpha-premultiplied red, green, blue and alpha components of the RGBA color.
|
|
|
|
func (c RGBA) RGBA() (r, g, b, a uint32) {
|
|
|
|
r = uint32(0xffff * c.R)
|
|
|
|
g = uint32(0xffff * c.G)
|
|
|
|
b = uint32(0xffff * c.B)
|
2016-12-23 13:25:47 -06:00
|
|
|
a = uint32(0xffff * c.A)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-09 15:00:26 -05:00
|
|
|
// ToRGBA converts a color to RGBA format. Using this function is preferred to using RGBAModel, for
|
|
|
|
// performance (using RGBAModel introduced additional unnecessary allocations).
|
|
|
|
func ToRGBA(c color.Color) RGBA {
|
|
|
|
if c, ok := c.(RGBA); ok {
|
2017-01-04 12:02:12 -06:00
|
|
|
return c
|
|
|
|
}
|
2017-04-09 15:00:26 -05:00
|
|
|
if c, ok := c.(color.RGBA); ok {
|
|
|
|
return RGBA{
|
2017-03-19 14:09:05 -05:00
|
|
|
R: float64(c.R) / 255,
|
|
|
|
G: float64(c.G) / 255,
|
|
|
|
B: float64(c.B) / 255,
|
|
|
|
A: float64(c.A) / 255,
|
|
|
|
}
|
|
|
|
}
|
2016-12-23 13:25:47 -06:00
|
|
|
r, g, b, a := c.RGBA()
|
2017-04-09 15:00:26 -05:00
|
|
|
return RGBA{
|
|
|
|
float64(r) / 0xffff,
|
|
|
|
float64(g) / 0xffff,
|
|
|
|
float64(b) / 0xffff,
|
2016-12-23 13:25:47 -06:00
|
|
|
float64(a) / 0xffff,
|
|
|
|
}
|
2017-01-05 07:50:09 -06:00
|
|
|
}
|
2017-03-23 17:03:07 -05:00
|
|
|
|
2017-04-09 15:00:26 -05:00
|
|
|
// RGBAModel converts colors to RGBA format.
|
|
|
|
var RGBAModel = color.ModelFunc(rgbaModel)
|
2017-03-23 17:03:07 -05:00
|
|
|
|
2017-04-09 15:00:26 -05:00
|
|
|
func rgbaModel(c color.Color) color.Color {
|
|
|
|
return ToRGBA(c)
|
2017-03-23 17:03:07 -05:00
|
|
|
}
|