This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
/
srgbna.go
61 lines (50 loc) · 1.92 KB
/
srgbna.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) 2013-2018 Laurent Moussault. All rights reserved.
// Licensed under a simplified BSD license (see LICENSE file).
package color
////////////////////////////////////////////////////////////////////////////////
// SRGBnA represents a color in *non* alpha-premultiplied sRGB color space. Each
// value ranges within [0, 1].
//
// An alpha-premultiplied color component c has been scaled by alpha (a), so has
// valid values 0 <= c <= a.
//
// Note that additive blending can also be achieved when alpha is set to 0 while
// the color components are non-null.
type SRGBnA struct {
R float32
G float32
B float32
A float32
}
////////////////////////////////////////////////////////////////////////////////
// SRGBnAof converts any color to sRGB non alpha-premultiplied sRGB color space.
func SRGBnAof(c Color) SRGBnA {
r, g, b, a := c.Linear()
r = standardOf(r / a)
g = standardOf(g / a)
b = standardOf(b / a)
return SRGBnA{r, g, b, a}
}
////////////////////////////////////////////////////////////////////////////////
// Linear implements the Colour interface: it returns the color converted to
// alpha-premultipled linear color space.
func (c SRGBnA) Linear() (r, g, b, a float32) {
r = c.A * linearOf(c.R)
g = c.A * linearOf(c.G)
b = c.A * linearOf(c.B)
return r, g, b, a
}
// Standard implements the Colour interface.
func (c SRGBnA) Standard() (r, g, b, a float32) {
r = standardOf(linearOf(c.R) * c.A)
g = standardOf(linearOf(c.R) * c.A)
b = standardOf(linearOf(c.R) * c.A)
return r, g, b, c.A
}
////////////////////////////////////////////////////////////////////////////////
// RGBA implements the image.Color interface: it returns the four components
// scaled by 0xFFFF.
func (c SRGBnA) RGBA() (r, g, b, a uint32) {
return uint32(c.A * c.R * 0xFFFF), uint32(c.A * c.G * 0xFFFF), uint32(c.A * c.B * 0xFFFF), uint32(c.A * 0xFFFF)
}
////////////////////////////////////////////////////////////////////////////////