-
Notifications
You must be signed in to change notification settings - Fork 1
/
rgb.go
328 lines (253 loc) Β· 5.61 KB
/
rgb.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
328
package coco
import (
"math"
"strconv"
"strings"
)
func Rgb2Cmyk(r float64, g float64, b float64) [4]float64 {
r = r / 255
g = g / 255
b = b / 255
k := math.Min(math.Min(1-r, 1-g), 1-b)
c := (1 - r - k) / (1 - k)
if math.IsNaN(c) {
c = 0
}
m := (1 - g - k) / (1 - k)
if math.IsNaN(m) {
m = 0
}
y := (1 - b - k) / (1 - k)
if math.IsNaN(y) {
y = 0
}
var result [4]float64
result[0] = math.Round(c * 100)
result[1] = math.Round(m * 100)
result[2] = math.Round(y * 100)
result[3] = math.Round(k * 100)
return result
}
func Rgb2Hsl(r float64, g float64, b float64) [3]float64 {
r = r / 255
g = g / 255
b = b / 255
min := math.Min(math.Min(r, g), b)
max := math.Max(math.Max(r, g), b)
delta := max - min
var h float64
var s float64
if max == min {
h = 0
} else if r == max {
h = (g - b) / delta
} else if g == max {
h = 2 + (b-r)/delta
} else if b == max {
h = 4 + (r-g)/delta
}
h = math.Min(h*60, 360)
if h < 0 {
h += 360
}
l := (min + max) / 2
if max == min {
s = 0
} else if l <= 0.5 {
s = delta / (max + min)
} else {
s = delta / (2 - max - min)
}
var result [3]float64
result[0] = math.Round(h)
result[1] = math.Round(s * 100)
result[2] = math.Round(l * 100)
return result
}
func Rgb2Hsv(r float64, g float64, b float64) [3]float64 {
var rdif float64
var gdif float64
var bdif float64
var h float64
var s float64
r = r / 255
g = g / 255
b = b / 255
min := math.Min(math.Min(r, g), b)
v := math.Max(math.Max(r, g), b)
diff := v - min
diffc := func(c float64) float64 {
return (v-c)/6.0/diff + (1.0 / 2.0)
}
if diff == 0 {
h = 0
s = 0
} else {
s = diff / v
rdif = diffc(r)
gdif = diffc(g)
bdif = diffc(b)
if r == v {
h = bdif - gdif
} else if g == v {
h = (1.0 / 3.0) + rdif - bdif
} else if b == v {
h = (2.0 / 3.0) + gdif - rdif
}
if h < 0 {
h += 1
} else if h > 1 {
h -= 1
}
}
var result [3]float64
result[0] = math.Round(h * 360)
result[1] = math.Round(s * 100)
result[2] = math.Round(v * 100)
return result
}
func Rgb2Hwb(r float64, g float64, b float64) [3]float64 {
h := Rgb2Hsl(r, g, b)[0]
w := 1.0 / 255.0 * math.Min(r, math.Min(g, b))
b = 1.0 - 1.0/255.0*math.Max(r, math.Max(g, b))
var result [3]float64
result[0] = math.Round(h)
result[1] = math.Round(w * 100)
result[2] = math.Round(b * 100)
return result
}
func Rgb2Lab(r float64, g float64, b float64) [3]float64 {
xyz := Rgb2Xyz(r, g, b)
x := xyz[0]
y := xyz[1]
z := xyz[2]
x /= 95.047
y /= 100
z /= 108.883
if x > 0.008856 {
x = math.Pow(x, (1.0 / 3.0))
} else {
x = (7.787 * x) + (16.0 / 116.0)
}
if y > 0.008856 {
y = math.Pow(y, (1.0 / 3.0))
} else {
y = (7.787 * y) + (16.0 / 116.0)
}
if z > 0.008856 {
z = math.Pow(z, (1.0 / 3.0))
} else {
z = (7.787 * z) + (16.0 / 116.0)
}
l := (116 * y) - 16
a := 500 * (x - y)
b = 200 * (y - z)
var result [3]float64
result[0] = math.Round(l)
result[1] = math.Round(a)
result[2] = math.Round(b)
return result
}
func Rgb2Xyz(r float64, g float64, b float64) [3]float64 {
r = r / 255
g = g / 255
b = b / 255
// Assume sRGB
if r > 0.04045 {
r = math.Pow(((r + 0.055) / 1.055), 2.4)
} else {
r = (r / 12.92)
}
if g > 0.04045 {
g = math.Pow(((g + 0.055) / 1.055), 2.4)
} else {
g = (g / 12.92)
}
if b > 0.04045 {
b = math.Pow(((b + 0.055) / 1.055), 2.4)
} else {
b = (b / 12.92)
}
x := (r * 0.4124564) + (g * 0.3575761) + (b * 0.1804375)
y := (r * 0.2126729) + (g * 0.7151522) + (b * 0.072175)
z := (r * 0.0193339) + (g * 0.119192) + (b * 0.9503041)
var result [3]float64
result[0] = math.Round(x * 100)
result[1] = math.Round(y * 100)
result[2] = math.Round(z * 100)
return result
}
func Rgb2Hex(r float64, g float64, b float64) string {
integer := ((int64(math.Round(r)) & 0xFF) << 16) + ((int64(math.Round(g)) & 0xFF) << 8) + (int64(math.Round(b)) & 0xFF)
myString := strings.ToUpper(strconv.FormatInt(integer, 16))
return myString
}
func Rgb2Hcg(r float64, g float64, b float64) [3]float64 {
r = r / 255
g = g / 255
b = b / 255
max := math.Max(math.Max(r, g), b)
min := math.Min(math.Min(r, g), b)
chroma := max - min
var grayscale float64
var hue float64
if chroma < 1 {
grayscale = min / (1 - chroma)
} else {
grayscale = 0
}
if chroma <= 0 {
hue = 0
} else if max == r {
hue = math.Mod((g-b)/chroma, 6)
} else if max == g {
hue = 2 + (b-r)/chroma
} else {
hue = 4 + (r-g)/chroma
}
hue /= 6
hue = math.Mod(hue, 1)
var result [3]float64
result[0] = math.Round(hue * 360)
result[1] = math.Round(chroma * 100)
result[2] = math.Round(grayscale * 100)
return result
}
func Rgb2Apple(r float64, g float64, b float64) [3]float64 {
var result [3]float64
result[0] = math.Round((r / 255) * 65535)
result[1] = math.Round((g / 255) * 65535)
result[2] = math.Round((b / 255) * 65535)
return result
}
func Rgb2Gray(r float64, g float64, b float64) [1]float64 {
val := (r + g + b) / 3
var result [1]float64
result[0] = math.Round(val / 255 * 100)
return result
}
// func Rgb2Ansi16(r float64, g float64, b float64) float64 {
// value := Rgb2Hsv(r, g, b)[2] // Hsv -> ansi16 optimization
// /*
// // should check the saturation value
// var value float64
// if saturation == nil {
// value = Rgb2Hsv(r, g, b)[2]
// } else {
// value = saturation
// }
// */
// value = math.Round(value / 50)
// if value == 0 {
// return 30
// }
// rBit := math.Round(r / 255)
// gBit := math.Round(g / 255)
// bBit := math.Round(b / 255)
// ansi := (30 + (int64(bBit) << 2) | (int64(gBit) << 1) | int64(rBit))
// fmt.Println(ansi)
// if value == 2 {
// ansi += 60
// }
// return float64(ansi)
// }