/
colorm.go
327 lines (302 loc) · 7.36 KB
/
colorm.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2014 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package affine
import (
"image/color"
"math"
)
// ColorMDim is a dimension of a ColorM.
const ColorMDim = 5
var (
colorMIdentityBody = []float32{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
}
colorMIdentityTranslate = []float32{
0, 0, 0, 0,
}
)
// A ColorM represents a matrix to transform coloring when rendering an image.
//
// A ColorM is applied to the source alpha color
// while an Image's pixels' format is alpha premultiplied.
// Before applying a matrix, a color is un-multiplied, and after applying the matrix,
// the color is multiplied again.
//
// The nil and initial value is identity.
type ColorM struct {
// When elements is nil, this matrix is identity.
// elements are immutable and a new array must be created when updating.
body []float32
translate []float32
}
func clamp(x float32) float32 {
if x > 1 {
return 1
}
if x < 0 {
return 0
}
return x
}
func (c *ColorM) isInited() bool {
return c != nil && c.body != nil
}
func (c *ColorM) Apply(clr color.Color) color.Color {
if !c.isInited() {
return clr
}
r, g, b, a := clr.RGBA()
rf, gf, bf, af := float32(0.0), float32(0.0), float32(0.0), float32(0.0)
// Unmultiply alpha
if a > 0 {
rf = float32(r) / float32(a)
gf = float32(g) / float32(a)
bf = float32(b) / float32(a)
af = float32(a) / 0xffff
}
eb := c.body
et := c.translate
rf2 := eb[0]*rf + eb[4]*gf + eb[8]*bf + eb[12]*af + et[0]
gf2 := eb[1]*rf + eb[5]*gf + eb[9]*bf + eb[13]*af + et[1]
bf2 := eb[2]*rf + eb[6]*gf + eb[10]*bf + eb[14]*af + et[2]
af2 := eb[3]*rf + eb[7]*gf + eb[11]*bf + eb[15]*af + et[3]
rf2 = clamp(rf2)
gf2 = clamp(gf2)
bf2 = clamp(bf2)
af2 = clamp(af2)
return color.NRGBA64{
R: uint16(rf2 * 0xffff),
G: uint16(gf2 * 0xffff),
B: uint16(bf2 * 0xffff),
A: uint16(af2 * 0xffff),
}
}
func (c *ColorM) UnsafeElements() ([]float32, []float32) {
if !c.isInited() {
return colorMIdentityBody, colorMIdentityTranslate
}
return c.body, c.translate
}
// SetElement sets an element at (i, j).
func (c *ColorM) SetElement(i, j int, element float32) *ColorM {
newC := &ColorM{
body: make([]float32, 16),
translate: make([]float32, 4),
}
if !c.isInited() {
copy(newC.body, colorMIdentityBody)
copy(newC.translate, colorMIdentityTranslate)
} else {
copy(newC.body, c.body)
copy(newC.translate, c.translate)
}
if j < (ColorMDim - 1) {
newC.body[i+j*(ColorMDim-1)] = element
} else {
newC.translate[i] = element
}
return newC
}
func (c *ColorM) Equals(other *ColorM) bool {
if !c.isInited() && !other.isInited() {
return true
}
lhsb := colorMIdentityBody
lhst := colorMIdentityTranslate
rhsb := colorMIdentityBody
rhst := colorMIdentityTranslate
if other.isInited() {
lhsb = other.body
lhst = other.translate
}
if c.isInited() {
rhsb = c.body
rhst = c.translate
}
if &lhsb == &rhsb && &lhst == &rhst {
return true
}
for i := range lhsb {
if lhsb[i] != rhsb[i] {
return false
}
}
for i := range lhst {
if lhst[i] != rhst[i] {
return false
}
}
return true
}
// Concat multiplies a color matrix with the other color matrix.
// This is same as muptiplying the matrix other and the matrix c in this order.
func (c *ColorM) Concat(other *ColorM) *ColorM {
if !c.isInited() {
return other
}
if !other.isInited() {
return c
}
lhsb := colorMIdentityBody
lhst := colorMIdentityTranslate
rhsb := colorMIdentityBody
rhst := colorMIdentityTranslate
if other.isInited() {
lhsb = other.body
lhst = other.translate
}
if c.isInited() {
rhsb = c.body
rhst = c.translate
}
return &ColorM{
// TODO: This is a temporary hack to calculate multiply of transposed matrices.
// Fix mulSquare implmentation and swap the arguments.
body: mulSquare(rhsb, lhsb, ColorMDim-1),
translate: []float32{
lhsb[0]*rhst[0] + lhsb[4]*rhst[1] + lhsb[8]*rhst[2] + lhsb[12]*rhst[3] + lhst[0],
lhsb[1]*rhst[0] + lhsb[5]*rhst[1] + lhsb[9]*rhst[2] + lhsb[13]*rhst[3] + lhst[1],
lhsb[2]*rhst[0] + lhsb[6]*rhst[1] + lhsb[10]*rhst[2] + lhsb[14]*rhst[3] + lhst[2],
lhsb[3]*rhst[0] + lhsb[7]*rhst[1] + lhsb[11]*rhst[2] + lhsb[15]*rhst[3] + lhst[3],
},
}
}
// Add is deprecated.
func (c *ColorM) Add(other *ColorM) *ColorM {
lhsb := colorMIdentityBody
lhst := colorMIdentityTranslate
rhsb := colorMIdentityBody
rhst := colorMIdentityTranslate
if other.isInited() {
lhsb = other.body
lhst = other.translate
}
if c.isInited() {
rhsb = c.body
rhst = c.translate
}
newC := &ColorM{
body: make([]float32, 16),
translate: make([]float32, 4),
}
for i := range lhsb {
newC.body[i] = lhsb[i] + rhsb[i]
}
for i := range lhst {
newC.translate[i] = lhst[i] + rhst[i]
}
return newC
}
// Scale scales the matrix by (r, g, b, a).
func (c *ColorM) Scale(r, g, b, a float32) *ColorM {
if !c.isInited() {
return &ColorM{
body: []float32{
r, 0, 0, 0,
0, g, 0, 0,
0, 0, b, 0,
0, 0, 0, a,
},
translate: colorMIdentityTranslate,
}
}
es := make([]float32, len(c.body))
copy(es, c.body)
for i := 0; i < ColorMDim-1; i++ {
es[i*(ColorMDim-1)] *= r
es[i*(ColorMDim-1)+1] *= g
es[i*(ColorMDim-1)+2] *= b
es[i*(ColorMDim-1)+3] *= a
}
return &ColorM{
body: es,
translate: []float32{
c.translate[0] * r,
c.translate[1] * g,
c.translate[2] * b,
c.translate[3] * a,
},
}
}
// Translate translates the matrix by (r, g, b, a).
func (c *ColorM) Translate(r, g, b, a float32) *ColorM {
if !c.isInited() {
return &ColorM{
body: colorMIdentityBody,
translate: []float32{r, g, b, a},
}
}
es := make([]float32, len(c.translate))
copy(es, c.translate)
es[0] += r
es[1] += g
es[2] += b
es[3] += a
return &ColorM{
body: c.body,
translate: es,
}
}
var (
// The YCbCr value ranges are:
// Y: [ 0 - 1 ]
// Cb: [-0.5 - 0.5]
// Cr: [-0.5 - 0.5]
rgbToYCbCr = &ColorM{
body: []float32{
0.2990, -0.1687, 0.5000, 0,
0.5870, -0.3313, -0.4187, 0,
0.1140, 0.5000, -0.0813, 0,
0, 0, 0, 1,
},
translate: []float32{0, 0, 0, 0},
}
yCbCrToRgb = &ColorM{
body: []float32{
1, 1, 1, 0,
0, -0.34414, 1.77200, 0,
1.40200, -0.71414, 0, 0,
0, 0, 0, 1,
},
translate: []float32{0, 0, 0, 0},
}
)
// ChangeHSV changes HSV (Hue-Saturation-Value) elements.
// hueTheta is a radian value to ratate hue.
// saturationScale is a value to scale saturation.
// valueScale is a value to scale value (a.k.a. brightness).
//
// This conversion uses RGB to/from YCrCb conversion.
func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float32, valueScale float32) *ColorM {
sin, cos := math.Sincos(hueTheta)
s32, c32 := float32(sin), float32(cos)
c = c.Concat(rgbToYCbCr)
c = c.Concat(&ColorM{
body: []float32{
1, 0, 0, 0,
0, c32, s32, 0,
0, -s32, c32, 0,
0, 0, 0, 1,
},
translate: []float32{0, 0, 0, 0},
})
s := saturationScale
v := valueScale
c = c.Scale(v, s*v, s*v, 1)
c = c.Concat(yCbCrToRgb)
return c
}