-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcolor_scale.go
More file actions
227 lines (200 loc) · 5.63 KB
/
color_scale.go
File metadata and controls
227 lines (200 loc) · 5.63 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
package graphics
import (
"fmt"
"image/color"
"strconv"
"unsafe"
"github.com/hajimehoshi/ebiten/v2"
"github.com/quasilyte/gmath"
)
// ColorScale is like ebiten.ColorScale, but its values don't need to be premultiplied.
// In a sense, it's like color.NRGBA in comparison with color.RGBA.
//
// For a normal color, we use {1, 1, 1, 1}, for a transparent color it's {0, 0, 0, 0}.
// To double the amount of red, you can use {2, 1, 1, 1} or {1, 0.5, 0.5, 1}.
//
// Use ColorScaleFromRGBA and ColorScaleFromColor if you want to construct
// the color scale object easily.
type ColorScale struct {
R float32
G float32
B float32
A float32
}
var (
defaultColorScale = ColorScale{1, 1, 1, 1}
transparentColor = ColorScale{0, 0, 0, 0}
)
func FormatRGB(clr color.NRGBA) string {
return fmt.Sprintf("%02x%02x%02x", clr.R, clr.G, clr.B)
}
func FormatRGBA(clr color.NRGBA) string {
return fmt.Sprintf("%02x%02x%02x%02x", clr.R, clr.G, clr.B, clr.A)
}
func ParseRGB(s string) color.NRGBA {
result := color.NRGBA{A: 255}
if len(s) >= 2 {
r, err := strconv.ParseUint(s[:2], 16, 8)
if err == nil {
result.R = uint8(r)
}
s = s[2:]
}
if len(s) >= 2 {
g, err := strconv.ParseUint(s[:2], 16, 8)
if err == nil {
result.G = uint8(g)
}
s = s[2:]
}
if len(s) >= 2 {
b, err := strconv.ParseUint(s[:2], 16, 8)
if err == nil {
result.B = uint8(b)
}
}
return result
}
// RGB returns a ColorScale created from the bits of rgb value.
// RGB(0xAABBCC) is identical to R=0xAA, G=0xBB, B=0xCC, A=0xFF.
//
// Note: alpha value is fixed to 0xFF, if you need some other value,
// use RGBA() function instead.
//
// Hint: use RGB(v).Color() to get a color.NRGBA object.
func RGB(rgb uint64) ColorScale {
r := uint8((rgb & (0xFF << (8 * 2))) >> (8 * 2))
g := uint8((rgb & (0xFF << (8 * 1))) >> (8 * 1))
b := uint8((rgb & (0xFF << (8 * 0))) >> (8 * 0))
return ColorScaleFromRGBA(r, g, b, 0xff)
}
// RGBA returns a ColorScale created from the bits of rgba value.
// RGBA(0xAABBCCEE) is identical to R=0xAA, G=0xBB, B=0xCC, A=0xEE.
//
// Hint: use RGBA(v).Color() to get a color.NRGBA object.
func RGBA(rgb uint64) ColorScale {
r := uint8((rgb & (0xFF << (8 * 3))) >> (8 * 3))
g := uint8((rgb & (0xFF << (8 * 2))) >> (8 * 2))
b := uint8((rgb & (0xFF << (8 * 1))) >> (8 * 1))
a := uint8((rgb & (0xFF << (8 * 0))) >> (8 * 0))
return ColorScaleFromRGBA(r, g, b, a)
}
// ColorScaleFromRGBA constructs a ColorScale using ColorScale.SetRGBA method.
func ColorScaleFromRGBA(r, g, b, a uint8) ColorScale {
var cs ColorScale
cs.SetRGBA(r, g, b, a)
return cs
}
// ColorScaleFromRGBA constructs a ColorScale using ColorScale.SetColor method.
func ColorScaleFromColor(c color.NRGBA) ColorScale {
var cs ColorScale
cs.SetColor(c)
return cs
}
func ColorScaleFromHSL(h, s, l, a float32) ColorScale {
return hsl2rgb(h, s, l, a)
}
func (c ColorScale) String() string {
return FormatRGBA(c.Color())
}
// SetColor assigns the color.NRGBA equivalent to a color scale.
func (c *ColorScale) SetColor(rgba color.NRGBA) {
c.SetRGBA(rgba.R, rgba.G, rgba.B, rgba.A)
}
// SetRGBA is like SetColor, but accepts every color part separately.
func (c *ColorScale) SetRGBA(r, g, b, a uint8) {
c.R = float32(r) / 255
c.G = float32(g) / 255
c.B = float32(b) / 255
c.A = float32(a) / 255
}
// Color returns the color.NRGBA representation of a color scale.
//
// It will only work correctly for color scales those values are in [0, 1] range.
// If some color value overflows (or underflows) this range, the result
// of this operation is truncated garbage.
//
// This function is mostly useful in combination with RGB() function
// to construct a color.NRGBA easily: RGB(0xAABBCCEE).Color().
func (c ColorScale) Color() color.NRGBA {
return color.NRGBA{
R: uint8(c.R * 255),
G: uint8(c.G * 255),
B: uint8(c.B * 255),
A: uint8(c.A * 255),
}
}
// ScaleAlpha returns the color scale with alpha multiplied by x.
// It doesn't affect R/G/B channels.
func (c ColorScale) ScaleAlpha(x float32) ColorScale {
c2 := c
c2.A *= x
return c2
}
// ScaleRGB multiplies R, G and B color scale components by x.
// It doesn't affect the alpha channel.
func (c ColorScale) ScaleRGB(x float32) ColorScale {
c2 := c
c2.R *= x
c2.G *= x
c2.B *= x
return c2
}
func (c ColorScale) Mul(other ColorScale) ColorScale {
return ColorScale{
R: c.R * other.R,
G: c.G * other.G,
B: c.B * other.B,
A: c.A * other.A,
}
}
func (c ColorScale) Lerp(target ColorScale, t float32) ColorScale {
return ColorScale{
R: gmath.Lerp(c.R, target.R, t),
G: gmath.Lerp(c.G, target.G, t),
B: gmath.Lerp(c.B, target.B, t),
A: gmath.Lerp(c.A, target.A, t),
}
}
// RotateHue returns a color scale with its hue rotated.
// The argument specifies the number of degrees to rotate.
func (c ColorScale) RotateHue(deg float32) ColorScale {
return hueRotate(c, deg)
}
func (c ColorScale) ToHSL() (h, s, l float32) {
return rgb2hsl(c)
}
// AsVec3 returns a color scale as RGB slice (vec3 for shaders).
func (c *ColorScale) AsVec3() []float32 {
return unsafe.Slice(&c.R, 3)
}
// AsVec4returns a color scale as RGBA slice (vec4 for shaders).
func (c *ColorScale) AsVec4() []float32 {
return unsafe.Slice(&c.R, 4)
}
func (c *ColorScale) undoPremultiply() ColorScale {
return ColorScale{
R: c.R / c.A,
G: c.G / c.A,
B: c.B / c.A,
A: c.A,
}
}
func (c *ColorScale) premultiplyAlpha() ColorScale {
return ColorScale{
R: c.R * c.A,
G: c.G * c.A,
B: c.B * c.A,
A: c.A,
}
}
func (c *ColorScale) ToEbitenColorScale() ebiten.ColorScale {
// This basically turns a NRGBA-style color scale into
// RGBA-style color scale (alpha-premultiplied).
var ec ebiten.ColorScale
ec.SetR(c.R * c.A)
ec.SetG(c.G * c.A)
ec.SetB(c.B * c.A)
ec.SetA(c.A)
return ec
}