This repository was archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.go
129 lines (113 loc) · 2.49 KB
/
color.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package image
import "fmt"
// Transparent is a special color where each component is zero (including alpha)
var Transparent = RGBA(0, 0, 0, 0)
// Color represents pixel color using 4 components: Red, Green, Black and Alpha.
// Red, Green and Blue components are premultiplied by alpha.
//
// Color is immutable struct. Changing the color means creating a new instance.
type Color struct {
r, g, b, a byte
}
// R returns the red component.
func (c Color) R() byte {
return c.r
}
// G returns the green component.
func (c Color) G() byte {
return c.g
}
// B returns the blue component.
func (c Color) B() byte {
return c.b
}
// A returns the alpha component.
func (c Color) A() byte {
return c.a
}
// RGBA returns all color components as bytes in range 0 to 255.
func (c Color) RGBA() (byte, byte, byte, byte) {
return c.r, c.g, c.b, c.a
}
// RGBAi returns all color components as integers in range 0 to 255.
func (c Color) RGBAi() (int, int, int, int) {
return int(c.r), int(c.g), int(c.b), int(c.a)
}
// RGBAf returns all color components as floats in range 0.0 to 1.0.
func (c Color) RGBAf() (float32, float32, float32, float32) {
return float32(c.r) / 255.0,
float32(c.g) / 255.0,
float32(c.b) / 255.0,
float32(c.a) / 255.0
}
func (c Color) String() string {
return fmt.Sprintf("RGBA(%d, %d, %d, %d)", c.r, c.g, c.b, c.a)
}
// RGBA creates Color using all four components: red, green, blue and alpha.
func RGBA(r, g, b, a byte) Color {
return Color{
r: r,
g: g,
b: b,
a: a,
}
}
// NRGBA creates Color using RGB components not premultiplied by alpha (aka straight
// alpha). Straight colors are being used by programs such as Aseprite.
func NRGBA(r, g, b, a byte) Color {
return Color{
r: mul(r, a),
g: mul(g, a),
b: mul(b, a),
a: a,
}
}
// mul is an optimized version of round(a * b / 255)
func mul(a, b byte) byte {
t := int(a)*int(b) + 0x80
return byte(((t >> 8) + t) >> 8)
}
// RGB creates Color using three components: red, green and blue.
// The color will be fully opaque (alpha=255)
func RGB(r, g, b byte) Color {
return Color{
r: r,
g: g,
b: b,
a: 255,
}
}
// RGBAi creates Color using components given as integer values.
// All values are clamped to [0-255] range.
func RGBAi(r, g, b, a int) Color {
if r < 0 {
r = 0
}
if r > 255 {
r = 255
}
if g < 0 {
g = 0
}
if g > 255 {
g = 255
}
if b < 0 {
b = 0
}
if b > 255 {
b = 255
}
if a < 0 {
a = 0
}
if a > 255 {
a = 255
}
return Color{
r: byte(r),
g: byte(g),
b: byte(b),
a: byte(a),
}
}