-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcolor.go
More file actions
230 lines (206 loc) · 5.1 KB
/
Copy pathcolor.go
File metadata and controls
230 lines (206 loc) · 5.1 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package gg
import (
"fmt"
"image/color"
"math"
)
// RGBA represents a color with red, green, blue, and alpha components.
// Each component is in the range [0, 1].
type RGBA struct {
R, G, B, A float64
}
// RGBA implements the color.Color interface.
// Returns premultiplied alpha values scaled to [0, 65535] as required by the interface.
// This allows gg.RGBA to be used directly with dc.SetColor(gg.Black).
func (c RGBA) RGBA() (r, g, b, a uint32) {
a = uint32(clamp65535(c.A * 65535))
r = uint32(clamp65535(c.R * c.A * 65535))
g = uint32(clamp65535(c.G * c.A * 65535))
b = uint32(clamp65535(c.B * c.A * 65535))
return
}
// Color converts RGBA to the standard color.Color interface.
//
// Deprecated: gg.RGBA now implements color.Color directly.
// Use the value itself instead of calling .Color().
func (c RGBA) Color() color.Color {
return color.NRGBA{
R: uint8(clamp255(c.R * 255)),
G: uint8(clamp255(c.G * 255)),
B: uint8(clamp255(c.B * 255)),
A: uint8(clamp255(c.A * 255)),
}
}
// FromColor converts a standard color.Color to straight-alpha RGBA.
// Go's color.Color.RGBA() returns premultiplied values; this un-premultiplies them.
func FromColor(c color.Color) RGBA {
r, g, b, a := c.RGBA()
if a == 0 {
return RGBA{0, 0, 0, 0}
}
return RGBA{
R: float64(r) / float64(a),
G: float64(g) / float64(a),
B: float64(b) / float64(a),
A: float64(a) / 65535,
}
}
// RGB creates an opaque color from RGB components.
func RGB(r, g, b float64) RGBA {
return RGBA{R: r, G: g, B: b, A: 1.0}
}
// RGBA2 creates a color from RGBA components.
func RGBA2(r, g, b, a float64) RGBA {
return RGBA{R: r, G: g, B: b, A: a}
}
// ParseHex creates a color from a hex string, returning an error for invalid formats.
// Supports formats: "RGB", "RGBA", "RRGGBB", "RRGGBBAA".
func ParseHex(hex string) (RGBA, error) {
original := hex
if hex != "" && hex[0] == '#' {
hex = hex[1:]
}
var r, g, b, a uint32
a = 255
var valid bool
switch len(hex) {
case 3: // RGB
valid = parseHex(hex[0:1], &r) && parseHex(hex[1:2], &g) && parseHex(hex[2:3], &b)
r, g, b = r*17, g*17, b*17
case 4: // RGBA
valid = parseHex(hex[0:1], &r) && parseHex(hex[1:2], &g) && parseHex(hex[2:3], &b) && parseHex(hex[3:4], &a)
r, g, b, a = r*17, g*17, b*17, a*17
case 6: // RRGGBB
valid = parseHex(hex[0:2], &r) && parseHex(hex[2:4], &g) && parseHex(hex[4:6], &b)
case 8: // RRGGBBAA
valid = parseHex(hex[0:2], &r) && parseHex(hex[2:4], &g) && parseHex(hex[4:6], &b) && parseHex(hex[6:8], &a)
default:
valid = false
}
if !valid {
return RGBA{R: 0, G: 0, B: 0, A: 1}, fmt.Errorf("invalid hex color format: %q", original)
}
return RGBA{
R: float64(r) / 255,
G: float64(g) / 255,
B: float64(b) / 255,
A: float64(a) / 255,
}, nil
}
// Hex creates a color from a hex string.
// Supports formats: "RGB", "RGBA", "RRGGBB", "RRGGBBAA".
func Hex(hex string) RGBA {
if c, err := ParseHex(hex); err == nil {
return c
}
return RGBA{R: 0, G: 0, B: 0, A: 1}
}
// parseHex is a helper for hex parsing. Returns false if any character is not valid hex.
func parseHex(s string, val *uint32) bool {
*val = 0
for i := 0; i < len(s); i++ {
c := s[i]
*val *= 16
switch {
case '0' <= c && c <= '9':
*val += uint32(c - '0')
case 'a' <= c && c <= 'f':
*val += uint32(c - 'a' + 10)
case 'A' <= c && c <= 'F':
*val += uint32(c - 'A' + 10)
default:
return false
}
}
return true
}
// Premultiply returns a premultiplied color.
func (c RGBA) Premultiply() RGBA {
return RGBA{
R: c.R * c.A,
G: c.G * c.A,
B: c.B * c.A,
A: c.A,
}
}
// Unpremultiply returns an unpremultiplied color.
func (c RGBA) Unpremultiply() RGBA {
if c.A == 0 {
return RGBA{R: 0, G: 0, B: 0, A: 0}
}
return RGBA{
R: c.R / c.A,
G: c.G / c.A,
B: c.B / c.A,
A: c.A,
}
}
// Lerp performs linear interpolation between two colors.
func (c RGBA) Lerp(other RGBA, t float64) RGBA {
return RGBA{
R: c.R + (other.R-c.R)*t,
G: c.G + (other.G-c.G)*t,
B: c.B + (other.B-c.B)*t,
A: c.A + (other.A-c.A)*t,
}
}
// clamp65535 restricts a value to [0, 65535] range.
func clamp65535(x float64) float64 {
if x < 0 {
return 0
}
if x > 65535 {
return 65535
}
return x
}
// clamp255 restricts a value to [0, 255] range.
func clamp255(x float64) float64 {
if x < 0 {
return 0
}
if x > 255 {
return 255
}
return x
}
// Common colors
var (
Black = RGB(0, 0, 0)
White = RGB(1, 1, 1)
Red = RGB(1, 0, 0)
Green = RGB(0, 1, 0)
Blue = RGB(0, 0, 1)
Yellow = RGB(1, 1, 0)
Cyan = RGB(0, 1, 1)
Magenta = RGB(1, 0, 1)
Transparent = RGBA2(0, 0, 0, 0)
)
// HSL creates a color from HSL values.
// h is hue [0, 360), s is saturation [0, 1], l is lightness [0, 1].
func HSL(h, s, l float64) RGBA {
h = math.Mod(h, 360)
if h < 0 {
h += 360
}
h /= 360
c := (1 - math.Abs(2*l-1)) * s
x := c * (1 - math.Abs(math.Mod(h*6, 2)-1))
m := l - c/2
var r, g, b float64
switch {
case h < 1.0/6:
r, g, b = c, x, 0
case h < 2.0/6:
r, g, b = x, c, 0
case h < 3.0/6:
r, g, b = 0, c, x
case h < 4.0/6:
r, g, b = 0, x, c
case h < 5.0/6:
r, g, b = x, 0, c
default:
r, g, b = c, 0, x
}
return RGB(r+m, g+m, b+m)
}