-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
206 lines (172 loc) · 3.99 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
package ops
import (
"image/color"
"math"
"github.com/blackchip-org/zc/v5/pkg/ansi"
"github.com/blackchip-org/zc/v5/pkg/zc"
)
/*
oper cmyk-rgb
func CMYKToRGB c:Uint8 m:Uint8 y:Uint8 k:Uint8 -- r:Uint8 g:Uint8 b:Uint8
title CMYK to RGB color space
desc
Convert the CMYK color *c*, *m*, *y*, *k*, to the RGB color space as *r*, *b*,
*g*.
end
example
0 127 191 127 -- 0 | 127 | 191 | 127
cmyk-rgb -- 128 | 64 | 32
end
*/
func CMYKToRGB(calc zc.Calc) {
k := zc.PopUint8(calc)
y := zc.PopUint8(calc)
m := zc.PopUint8(calc)
c := zc.PopUint8(calc)
r, g, b := color.CMYKToRGB(c, m, y, k)
zc.PushUint8(calc, r)
zc.PushUint8(calc, g)
zc.PushUint8(calc, b)
}
/*
oper color-sample
func ColorSample r:Uint8 g:Uint8 b:Uint8 -- Str
title Render a sample of a color
desc
Render five spaces with a background color of *r*, *g*, and *b*. The
terminal in use must be able to support 24-bit ANSI color codes.
end
*/
func ColorSample(calc zc.Calc) {
b := zc.PopUint8(calc)
g := zc.PopUint8(calc)
r := zc.PopUint8(calc)
r0 := "#raw:" + ansi.BgColor24(r, g, b) + " " + ansi.Reset
zc.PushString(calc, r0)
}
/*
oper hsl-rgb
func HSLToRGB h:Float s:Float l:Float -- r:Uint8 g:Uint8 b:Uint8
title HSL to RGB color space
desc
Convert the HSL color *h*, *s*, *l* to the RGB color space as *r*, *g*, *b*.
end
example
20 0.6 0.314 -- 20 | 0.6 | 0.314
hsl-rgb -- 128 | 64 | 32
end
*/
func HSLToRGB(calc zc.Calc) {
l := zc.PopFloat(calc)
s := zc.PopFloat(calc)
h := zc.PopFloat(calc)
l = zc.Clamp(l, 0.0, 1.0)
s = zc.Clamp(s, 0.0, 1.0)
// Written by ChatGPT
c := (1 - math.Abs(2*l-1)) * s
x := c * (1 - math.Abs(math.Mod(h/60, 2)-1))
m := l - c/2
var rp, gp, bp float64
if h >= 0 && h < 60 {
rp, gp, bp = c, x, 0
} else if h >= 60 && h < 120 {
rp, gp, bp = x, c, 0
} else if h >= 120 && h < 180 {
rp, gp, bp = 0, c, x
} else if h >= 180 && h < 240 {
rp, gp, bp = 0, x, c
} else if h >= 240 && h < 300 {
rp, gp, bp = x, 0, c
} else {
rp, gp, bp = c, 0, x
}
r := uint8((rp + m) * 255)
g := uint8((gp + m) * 255)
b := uint8((bp + m) * 255)
// End ChatGPT
zc.PushUint8(calc, r)
zc.PushUint8(calc, g)
zc.PushUint8(calc, b)
}
/*
oper rgb-cmyk
func RGBToCMYK r:Uint8 g:Uint8 b:Uint8 -- c:Uint8 m:Uint8 y:Uint8 k:Uint8
title RGB to CMYK color space
desc
Convert the RGB color *r*, *b*, *g* to the CMYK color space as *c*, *m*, *y*,
*k*.
end
example
128 64 32 -- 128 | 64 | 32
rgb-cmyk -- 0 | 127 | 191 | 127
end
*/
func RGBToCMYK(calc zc.Calc) {
b := zc.PopUint8(calc)
g := zc.PopUint8(calc)
r := zc.PopUint8(calc)
c, m, y, k := color.RGBToCMYK(r, g, b)
zc.PushUint8(calc, c)
zc.PushUint8(calc, m)
zc.PushUint8(calc, y)
zc.PushUint8(calc, k)
}
/*
oper rgb-hsl
func RGBToHSL r:Uint8 g:Uint8 b:Uint8 -- h:Float s:Float l:Float
title RGB to HSL color space
desc
Convert the RGB color *r*, *g*, *b* to the HSL color space as *h*, *s*, *l*.
end
example
128 64 32 -- 128 | 64 | 32
rgb-hsl [3 round] map -- 20 | 0.6 | 0.314
end
*/
func RGBToHSL(calc zc.Calc) {
b := zc.PopUint8(calc)
g := zc.PopUint8(calc)
r := zc.PopUint8(calc)
// Written by ChatGPT
// Convert RGB values from 0-255 range to 0-1 range
rf := float64(r) / 255.0
gf := float64(g) / 255.0
bf := float64(b) / 255.0
// Find the minimum and maximum values of the RGB components
cmax := math.Max(math.Max(rf, gf), bf)
cmin := math.Min(math.Min(rf, gf), bf)
// Calculate lightness
l := (cmax + cmin) / 2.0
var s float64
// If the minimum and maximum values are equal, then the color is gray and saturation is 0
if cmax == cmin {
s = 0.0
} else {
// Calculate saturation
if l < 0.5 {
s = (cmax - cmin) / (cmax + cmin)
} else {
s = (cmax - cmin) / (2.0 - cmax - cmin)
}
}
// Calculate hue
rc := (cmax - rf) / (cmax - cmin)
gc := (cmax - gf) / (cmax - cmin)
bc := (cmax - bf) / (cmax - cmin)
var h float64
if rf == cmax {
h = bc - gc
} else if gf == cmax {
h = 2.0 + rc - bc
} else {
h = 4.0 + gc - rc
}
h *= 60.0
if h < 0 {
h += 360.0
}
// End ChatGPT
zc.PushFloat(calc, h)
zc.PushFloat(calc, s)
zc.PushFloat(calc, l)
}