-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcolor.go
376 lines (351 loc) · 11 KB
/
color.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Package plates provides utilities for manipulating colors and images.
package plates
import (
"image"
"image/color"
"math"
)
// Red function isolates and returns the red channel from an image.
// It returns a new image where only the red component of each pixel's color is retained.
func Red(m image.Image) image.Image {
var (
rect = m.Bounds()
c color.Color
cr color.RGBA
newImage = image.NewRGBA(image.Rect(0, 0, rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y))
)
for y := rect.Min.Y; y < rect.Max.Y; y++ {
for x := rect.Min.X; x < rect.Max.X; x++ {
c = m.At(x, y) // c is RGBAColor, which implements Color
cr = c.(color.RGBA) // this is needed
c = color.RGBA{cr.R, 0, 0, cr.A}
newImage.Set(x-rect.Min.X, y-rect.Min.Y, c)
}
}
return newImage
}
// Green function isolates and returns the green channel from an image.
// It returns a new image where only the green component of each pixel's color is retained.
func Green(m image.Image) image.Image {
var (
rect = m.Bounds()
c color.Color
cr color.RGBA
newImage = image.NewRGBA(image.Rect(0, 0, rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y))
)
for y := rect.Min.Y; y < rect.Max.Y; y++ {
for x := rect.Min.X; x < rect.Max.X; x++ {
c = m.At(x, y) // c is RGBAColor, which implements Color
cr = c.(color.RGBA) // this is needed
c = color.RGBA{0, cr.G, 0, cr.A}
newImage.Set(x-rect.Min.X, y-rect.Min.Y, c)
}
}
return newImage
}
// Blue function isolates and returns the blue channel from an image.
// It returns a new image where only the blue component of each pixel's color is retained.
func Blue(m image.Image) image.Image {
var (
rect = m.Bounds()
c color.Color
cr color.RGBA
newImage = image.NewRGBA(image.Rect(0, 0, rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y))
)
for y := rect.Min.Y; y < rect.Max.Y; y++ {
for x := rect.Min.X; x < rect.Max.X; x++ {
c = m.At(x, y) // c is RGBAColor, which implements Color
cr = c.(color.RGBA) // this is needed
c = color.RGBA{0, 0, cr.B, cr.A}
newImage.Set(x-rect.Min.X, y-rect.Min.Y, c)
}
}
return newImage
}
// CloseTo1 function isolates pixels in an image that are similar to a target color within a given threshold.
// It returns a new image where only the pixels close to the target color are retained.
// Pixels that do not meet the threshold will be set to black (RGB{0,0,0}).
func CloseTo1(m image.Image, target color.RGBA, threshold uint8) image.Image {
var (
rect = m.Bounds()
c color.Color
cr color.RGBA
r, g, b, a uint8
newImage = image.NewRGBA(image.Rect(0, 0, rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y))
)
for y := rect.Min.Y; y < rect.Max.Y; y++ {
for x := rect.Min.X; x < rect.Max.X; x++ {
c = m.At(x, y) // c is RGBAColor, which implements Color
cr = c.(color.RGBA) // this is needed
r = 0
g = 0
b = 0
a = cr.A
if abs(int8(target.R)-int8(cr.R)) < threshold {
r = target.R
}
if abs(int8(target.G)-int8(cr.G)) < threshold {
g = target.G
}
if abs(int8(target.B)-int8(cr.B)) < threshold {
b = target.B
}
c = color.RGBA{r, g, b, a}
newImage.Set(x-rect.Min.X, y-rect.Min.Y, c)
}
}
return newImage
}
// CloseTo2 will pick out only the colors close to the given color,
// within a given threshold. Make it uniform.
// Zero alpha to unused pixels in returned image.
func CloseTo2(m image.Image, target color.RGBA, threshold uint8) image.Image {
var (
rect = m.Bounds()
c color.Color
cr color.RGBA
r, g, b, a uint8
newImage = image.NewRGBA(image.Rect(0, 0, rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y))
)
for y := rect.Min.Y; y < rect.Max.Y; y++ {
for x := rect.Min.X; x < rect.Max.X; x++ {
c = m.At(x, y) // c is RGBAColor, which implements Color
cr = c.(color.RGBA) // this is needed
r = 0
g = 0
b = 0
a = 0
if abs(int8(target.R)-int8(cr.R)) < threshold || abs(int8(target.G)-int8(cr.G)) < threshold || abs(int8(target.B)-int8(cr.B)) < threshold {
r = target.R
g = target.G
b = target.B
a = cr.A
}
c = color.RGBA{r, g, b, a}
newImage.Set(x-rect.Min.X, y-rect.Min.Y, c)
}
}
return newImage
}
// AddToAs will take an image, add the nontransparent colors from addimage,
// use addcolor and return an image.
func AddToAs(orig image.Image, addimage image.Image, addcolor color.RGBA) image.Image {
var (
rect = addimage.Bounds()
c color.Color
cr, or color.RGBA
r, g, b, a uint8
newImage = image.NewRGBA(image.Rect(0, 0, rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y))
)
for y := rect.Min.Y; y < rect.Max.Y; y++ {
for x := rect.Min.X; x < rect.Max.X; x++ {
cr = addimage.At(x, y).(color.RGBA)
or = orig.At(x, y).(color.RGBA)
r = or.R
g = or.G
b = or.B
a = or.A
if cr.A > 0 {
r = addcolor.R
g = addcolor.G
b = addcolor.B
a = addcolor.A
}
c = color.RGBA{r, g, b, a}
newImage.Set(x-rect.Min.X, y-rect.Min.Y, c)
}
}
return newImage
}
// Hue will convert an RGB color to a Hue float
func Hue(cr color.RGBA) float64 {
r := float64(cr.R) / 255.0
g := float64(cr.G) / 255.0
b := float64(cr.B) / 255.0
var h float64
RGBmax := r
if g > RGBmax {
RGBmax = g
}
if b > RGBmax {
RGBmax = b
}
if RGBmax == r {
h = 60 * (g - b)
if h < 0 {
h += 360
}
} else if RGBmax == g {
h = 120 + 60*(b-r)
} else /* RGBmax == rgb.b */ {
h = 240 + 60*(r-g)
}
return h
}
// HSV will convert an RGB color to huse, saturation and value
func HSV(cr color.RGBA) (uint8, uint8, uint8) {
var hue, sat, val uint8
RGBmin := min(cr.R, cr.G, cr.B)
RGBmax := max(cr.R, cr.G, cr.B)
val = RGBmax
if val == 0 {
hue = 0
sat = 0
return hue, sat, val
}
sat = 255 * (RGBmax - RGBmin) / val
if sat == 0 {
hue = 0
return hue, sat, val
}
span := (RGBmax - RGBmin)
if RGBmax == cr.R {
hue = 43 * (cr.G - cr.B) / span
} else if RGBmax == cr.G {
hue = 85 + 43*(cr.B-cr.R)/span
} else { /* RGBmax == cr.B */
hue = 171 + 43*(cr.R-cr.G)/span
}
return hue, sat, val
}
// Separate3 an image into three images with the three given colors and a given threshold.
func Separate3(inImage image.Image, color1, color2, color3 color.RGBA, threshold uint8) (image.Image, image.Image, image.Image) {
var (
rect = inImage.Bounds()
cr color.RGBA
r, g, b, a uint8
h, s float64
newImage1 = image.NewRGBA(image.Rect(0, 0, rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y))
newImage2 = image.NewRGBA(image.Rect(0, 0, rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y))
newImage3 = image.NewRGBA(image.Rect(0, 0, rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y))
)
hue1, _, s1 := HLS(float64(color1.R)/255.0, float64(color1.G)/255.0, float64(color1.B)/255.0)
hue2, _, s2 := HLS(float64(color2.R)/255.0, float64(color2.G)/255.0, float64(color2.B)/255.0)
hue3, _, s3 := HLS(float64(color3.R)/255.0, float64(color3.G)/255.0, float64(color3.B)/255.0)
for y := rect.Min.Y; y < rect.Max.Y; y++ {
for x := rect.Min.X; x < rect.Max.X; x++ {
// get the rgba color
// cr = inImage.At(x, y).(image.RGBAColor)
cr = color.RGBAModel.Convert(inImage.At(x, y)).(color.RGBA)
a = 255
h, _, s = HLS(float64(cr.R)/255.0, float64(cr.G)/255.0, float64(cr.B)/255.0)
// Find the closest color of the three, measured in hue and saturation
if ((fabs(h-hue1) < fabs(h-hue2)) && (fabs(h-hue1) < fabs(h-hue3))) ||
((fabs(s-s1) < fabs(s-s2)) && (fabs(s-s1) < fabs(s-s3))) {
// Only add if the color is close enough
if abs(int8(color1.R)-int8(cr.R)) < threshold || abs(int8(color1.G)-int8(cr.G)) < threshold || abs(int8(color1.B)-int8(cr.B)) < threshold {
r = color1.R
g = color1.G
b = color1.B
newImage1.Set(x-rect.Min.X, y-rect.Min.Y, color.RGBA{r, g, b, a})
}
} else if ((fabs(h-hue2) < fabs(h-hue1)) && (fabs(h-hue2) < fabs(h-hue3))) ||
((fabs(s-s2) < fabs(s-s1)) && (fabs(s-s2) < fabs(s-s3))) {
// Only add if the color is close enough
if abs(int8(color2.R)-int8(cr.R)) < threshold || abs(int8(color2.G)-int8(cr.G)) < threshold || abs(int8(color2.B)-int8(cr.B)) < threshold {
r = color2.R
g = color2.G
b = color2.B
newImage2.Set(x-rect.Min.X, y-rect.Min.Y, color.RGBA{r, g, b, a})
}
} else if ((fabs(h-hue3) < fabs(h-hue1)) && (fabs(h-hue3) < fabs(h-hue2))) ||
((fabs(s-s3) < fabs(s-s1)) && (fabs(s-s3) < fabs(s-s2))) {
if abs(int8(color3.R)-int8(cr.R)) < threshold || abs(int8(color3.G)-int8(cr.G)) < threshold || abs(int8(color3.B)-int8(cr.B)) < threshold {
r = color3.R
g = color3.G
b = color3.B
newImage3.Set(x-rect.Min.X, y-rect.Min.Y, color.RGBA{r, g, b, a})
}
}
}
}
return newImage1, newImage2, newImage3
}
// HLS will convert an RGB color to hue, lightness and saturation
func HLS(r, g, b float64) (float64, float64, float64) {
// Ported from Python colorsys
var h, l, s float64
maxc := fmax(r, g, b)
minc := fmin(r, g, b)
l = (minc + maxc) / 2.0
if minc == maxc {
return 0.0, l, 0.0
}
span := (maxc - minc)
if l <= 0.5 {
s = span / (maxc + minc)
} else {
s = span / (2.0 - maxc - minc)
}
rc := (maxc - r) / span
gc := (maxc - g) / span
bc := (maxc - b) / span
if r == maxc {
h = bc - gc
} else if g == maxc {
h = 2.0 + rc - bc
} else {
h = 4.0 + gc - rc
}
h = math.Mod((h / 6.0), 1.0)
return h, l, s
}
// computeColorChannel computes the value of a color channel based on hue, m1 and m2,
// according to the HLS color model. Ported from Python colorsys.
func computeColorChannel(m1, m2, hue float64) float64 {
const oneSixth = 1.0 / 6.0
const twoThird = 2.0 / 3.0
hue = math.Mod(hue, 1.0)
if hue < oneSixth {
return m1 + (m2-m1)*hue*6.0
}
if hue < 0.5 {
return m2
}
if hue < twoThird {
return m1 + (m2-m1)*(twoThird-hue)*6.0
}
return m1
}
// HLStoRGB will convert a HLS color to red, green, blue
func HLStoRGB(h, l, s float64) (float64, float64, float64) {
const oneThird = 1.0 / 3.0
if s == 0.0 {
return l, l, l
}
var m1, m2 float64
// Ported from Python colorsys
if l <= 0.5 {
m2 = l * (1.0 + s)
} else {
m2 = l + s - (l * s)
}
m1 = 2.0*l - m2
return computeColorChannel(m1, m2, h+oneThird), computeColorChannel(m1, m2, h), computeColorChannel(m1, m2, h-oneThird)
}
// PaintMix will attempt to mix two RGB colors, a bit like how paint mixes (but not exactly like it)
func PaintMix(c1, c2 color.RGBA) color.RGBA {
/*
* The less pi-precision, the greener the mix between blue and yellow.
* Curiously, using math.Pi gives a completely different result.
*/
const twoPi = 2.0 * 3.141592653
//const twoPi = 2.0 * math.Pi
//const twoPi = 2.0 * 3.1415
//const twoPi = 2.0 * 3.141592653589793
// Thanks to Mark Ransom
h1, l1, s1 := HLS(float64(c1.R)/255.0, float64(c1.G)/255.0, float64(c1.B)/255.0)
h2, l2, s2 := HLS(float64(c2.R)/255.0, float64(c2.G)/255.0, float64(c2.B)/255.0)
h := 0.0
s := 0.5 * (s1 + s2)
l := 0.5 * (l1 + l2)
x := math.Cos(twoPi*h1) + math.Cos(twoPi*h2)
y := math.Sin(twoPi*h1) + math.Sin(twoPi*h2)
if (x != 0.0) || (y != 0.0) {
h = math.Atan2(y, x) / twoPi
} else {
s = 0.0
}
r, g, b := HLStoRGB(h, l, s)
return color.RGBA{uint8(r * 255.0), uint8(g * 255.0), uint8(b * 255.0), 255}
}