This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
srgba8.go
70 lines (59 loc) · 2.15 KB
/
srgba8.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
62
63
64
65
66
67
68
69
70
// Copyright (c) 2013-2018 Laurent Moussault. All rights reserved.
// Licensed under a simplified BSD license (see LICENSE file).
package color
////////////////////////////////////////////////////////////////////////////////
// SRGBA8 represents a 32-bit color in alpha-premultiplied sRGB color space.
// There is 8 bits for each components.
//
// 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 SRGBA8 struct {
R uint8
G uint8
B uint8
A uint8
}
////////////////////////////////////////////////////////////////////////////////
// SRGBA8of converts any color to alpha-premultiplied sRGB color space.
func SRGBA8of(c Color) SRGBA8 {
cc, ok := c.(SRGBA8)
if ok {
return cc
}
ccc, ok := c.(SRGB8)
if ok {
return SRGBA8{ccc.R, ccc.G, ccc.B, 1}
}
r, g, b, a := c.Standard()
return SRGBA8{uint8(r * 0xFF), uint8(g * 0xFF), uint8(b * 0xFF), uint8(a * 0xFF)}
}
////////////////////////////////////////////////////////////////////////////////
// Linear implements the Colour interface.
func (c SRGBA8) Linear() (r, g, b, a float32) {
r = linearOf(float32(c.R) / float32(0xFF))
g = linearOf(float32(c.G) / float32(0xFF))
b = linearOf(float32(c.B) / float32(0xFF))
a = float32(c.A) / float32(0xFF)
return r, g, b, a
}
// Standard implements the Colour interface.
func (c SRGBA8) Standard() (r, g, b, a float32) {
r = float32(c.R) / float32(0xFF)
g = float32(c.R) / float32(0xFF)
b = float32(c.R) / float32(0xFF)
a = float32(c.A) / float32(0xFF)
return r, g, b, a
}
////////////////////////////////////////////////////////////////////////////////
// RGBA implements the image.Color interface.
func (c SRGBA8) RGBA() (r, g, b, a uint32) {
r = uint32((float64(c.R) / float64(0xFF)) * float64(0xFFFF))
g = uint32((float64(c.G) / float64(0xFF)) * float64(0xFFFF))
b = uint32((float64(c.B) / float64(0xFF)) * float64(0xFFFF))
a = uint32((float64(c.A) / float64(0xFF)) * float64(0xFFFF))
return r, g, b, a
}
////////////////////////////////////////////////////////////////////////////////