/
convert.go
232 lines (205 loc) · 5.15 KB
/
convert.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
// Copyright ©2016 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package moreland
import (
"image/color"
"math"
)
// rgb represents a physically linear RGB color.
type rgb struct {
R, G, B float64
}
// cieXYZ returns a CIE XYZ color representation of the receiver.
func (c rgb) cieXYZ() cieXYZ {
return cieXYZ{
X: 0.4124*c.R + 0.3576*c.G + 0.1805*c.B,
Y: 0.2126*c.R + 0.7152*c.G + 0.0722*c.B,
Z: 0.0193*c.R + 0.1192*c.G + 0.9505*c.B,
}
}
// sRGBA returns an sRGB color representation of the receiver using the
// provided alpha which must be in [0, 1].
func (c rgb) sRGBA(alpha float64) sRGBA {
// f converts from a linear RGB component to an sRGB component.
f := func(v float64) float64 {
if v > 0.0031308 {
return 1.055*math.Pow(v, 1/2.4) - 0.055
}
return 12.92 * v
}
return sRGBA{
R: f(c.R),
G: f(c.G),
B: f(c.B),
A: alpha,
}
}
// cieXYZ represents a color in CIE XYZ space.
// Y must be in the range [0,1]. X and Z must be greater than 0.
type cieXYZ struct {
X, Y, Z float64
}
// rgb returns a linear RGB representation of the receiver.
func (c cieXYZ) rgb() rgb {
return rgb{
R: c.X*3.2406 + c.Y*-1.5372 + c.Z*-0.4986,
G: c.X*-0.9689 + c.Y*1.8758 + c.Z*0.0415,
B: c.X*0.0557 + c.Y*-0.204 + c.Z*1.057,
}
}
// cieLAB returns a CIELAB color representation of the receiver.
func (c cieXYZ) cieLAB() cieLAB {
// f is an intermediate step in converting from CIE XYZ to CIE LAB.
f := func(v float64) float64 {
if v > 0.008856 {
return math.Pow(v, 1.0/3.0)
}
return 7.787*v + 16.0/116.0
}
tempX := f(c.X / 0.9505)
tempY := f(c.Y)
tempZ := f(c.Z / 1.089)
return cieLAB{
L: (116.0 * tempY) - 16.0,
A: 500.0 * (tempX - tempY),
B: 200 * (tempY - tempZ),
}
}
// sRGBA represents a color within the sRGB color space, with an alpha channel
// but not premultiplied. All values must be in the range [0,1].
type sRGBA struct {
R, G, B, A float64
}
// rgb returns a linear RGB representation of the receiver.
func (c sRGBA) rgb() rgb {
// f converts from an sRGB component to a linear RGB component.
f := func(v float64) float64 {
if v > 0.04045 {
return math.Pow((v+0.055)/1.055, 2.4)
}
return v / 12.92
}
return rgb{
R: f(c.R),
G: f(c.G),
B: f(c.B),
}
}
// RGBA implements the color.Color interface.
func (c sRGBA) RGBA() (r, g, b, a uint32) {
return uint32(c.R * c.A * 0xffff), uint32(c.G * c.A * 0xffff), uint32(c.B * c.A * 0xffff), uint32(c.A * 0xffff)
}
// cieLAB returns a CIE LAB representation of the receiver.
func (c sRGBA) cieLAB() cieLAB {
return c.rgb().cieXYZ().cieLAB()
}
// colorTosRGBA converts a color to an sRGBA.
func colorTosRGBA(c color.Color) sRGBA {
r, g, b, a := c.RGBA()
if a == 0 {
return sRGBA{}
}
return sRGBA{
R: float64(r) / float64(a),
G: float64(g) / float64(a),
B: float64(b) / float64(a),
A: float64(a) / 0xffff,
}
}
// clamp forces all channels in c to be within the range [0, 1].
func (c *sRGBA) clamp() {
if c.R > 1 {
c.R = 1
}
if c.G > 1 {
c.G = 1
}
if c.B > 1 {
c.B = 1
}
if c.A > 1 {
c.A = 1
}
if c.R < 0 {
c.R = 0
}
if c.G < 0 {
c.G = 0
}
if c.B < 0 {
c.B = 0
}
if c.A < 0 {
c.A = 0
}
}
// cieLAB represents a color in CIE LAB space.
// L must be in the range [0, 100].
type cieLAB struct {
L, A, B float64
}
// sRGBA return a linear RGB color representation of the receiver using the
// provided alpha which must be in [0, 1].
func (c cieLAB) sRGBA(alpha float64) sRGBA {
return c.cieXYZ().rgb().sRGBA(alpha)
}
// cieXYZ returns a CIE XYZ color representation of the receiver.
func (c cieLAB) cieXYZ() cieXYZ {
// f is an intermediate step in converting from CIE LAB to CIE XYZ.
f := func(v float64) float64 {
const (
xlim = 0.008856
a = 7.787
b = 16. / 116.
ylim = a*xlim + b
)
if v > ylim {
return v * v * v
}
return (v - b) / a
}
// Reference white-point D65
const xn, yn, zn = 0.95047, 1.0, 1.08883
return cieXYZ{
X: xn * f((c.A/500)+(c.L+16)/116),
Y: yn * f((c.L+16)/116),
Z: zn * f((c.L+16)/116-(c.B/200)),
}
}
// MSH returns an MSH color representation of the receiver.
func (c cieLAB) MSH() msh {
m := math.Pow(c.L*c.L+c.A*c.A+c.B*c.B, 0.5)
return msh{
M: m,
S: math.Acos(c.L / m),
H: math.Atan2(c.B, c.A),
}
}
// MSH represents a color in Magnitude-Saturation-Hue color space.
type msh struct {
M, S, H float64
}
// colorToMSH converts a color to MSH space.
// TODO: If msh ever becomes exported, change this to implment color.Model
func colorToMSH(c color.Color) msh {
return colorTosRGBA(c).cieLAB().MSH()
}
// cieLAB returns a CIELAB representation of the receiver.
func (c msh) cieLAB() cieLAB {
return cieLAB{
L: c.M * math.Cos(c.S),
A: c.M * math.Sin(c.S) * math.Cos(c.H),
B: c.M * math.Sin(c.S) * math.Sin(c.H),
}
}
// RGBA implements the color.Color interface.
func (c msh) RGBA() (r, g, b, a uint32) {
return c.cieLAB().sRGBA(1.0).RGBA()
}
// hueTwist returns the hue twist between color c and converge magnitude
// convergeM.
func hueTwist(c msh, convergeM float64) float64 {
signH := c.H / math.Abs(c.H)
return signH * c.S * math.Sqrt(convergeM*convergeM-c.M*c.M) / (c.M * math.Sin(c.S))
}