-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcolorspaces.reb
228 lines (216 loc) · 4.42 KB
/
colorspaces.reb
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
REBOL[
Title: "Colorspaces"
File: %colorspaces.reb
Author: "Boleslav Březovský"
Date: 3-4-2014
Version: 0.0.1
Type: 'module
Name: 'colorspaces
Exports: [
load-web-color load-hsl load-hsv
to-hsl to-hsv
new-color set-color apply-color
]
]
load-web-color: func [
"Convert hex RGB issue! value to tuple!"
color [issue!] ; add string also? is it needed?
/local pos
] [
to tuple! debase/base next form color 16
]
to-hsl: func [
color [tuple!]
/local min max delta alpha total
][
if color/4 [alpha: color/4 / 255]
color: reduce [color/1 color/2 color/3]
bind/new [r g b] local: object []
set words-of local map-each c color [c / 255]
color: local
min: first minimum-of values-of color
max: first maximum-of values-of color
delta: max - min
total: max + min
local: object [h: s: l: to percent! total / 2]
do in local bind [
either zero? delta [h: s: 0] [
s: to percent! either l > .5 [2 - max - min] [delta / total]
h: 60 * switch max reduce [
r [g - b / delta + either g < b 6 0]
g [b - r / delta + 2]
b [r - g / delta + 4]
]
]
] color
local: values-of local
if alpha [append local alpha]
local
]
to-hsv: func [
color [tuple!]
/local min max delta alpha
][
if color/4 [alpha: color/4 / 255]
color: reduce [color/1 color/2 color/3]
bind/new [r g b] local: object []
set words-of local map-each c color [c / 255]
color: local
min: first minimum-of values-of color
max: first maximum-of values-of color
delta: max - min
local: object [h: s: v: to percent! max]
do in local bind [
either zero? delta [h: s: 0] [
s: to percent! either delta = 0 [0] [delta / max]
h: 60 * switch max reduce [
r [g - b / delta + either g < b 6 0]
g [b - r / delta + 2]
b [r - g / delta + 4]
]
]
] color
local: values-of local
if alpha [append local alpha]
local
]
load-hsl: func [
color [block!]
/local alpha c x m i
][
if color/4 [alpha: color/4]
; LOCAL: HSL, COLOR: RGB
bind/new [h s l] local: object []
set words-of local color
bind/new [r g b] color: object []
do in local [
i: h / 60
c: 1 - (abs 2 * l - 1) * s
x: 1 - (abs -1 + mod i 2) * c
m: l - (c / 2)
]
do in color [
set [r g b] reduce switch to integer! i [
0 [[c x 0]]
1 [[x c 0]]
2 [[0 c x]]
3 [[0 x c]]
4 [[x 0 c]]
5 [[c 0 x]]
]
]
color: to tuple! map-each value values-of color [to integer! round m + value * 255]
if alpha [color/4: alpha * 255]
color
]
load-hsv: func [
color [block!]
/local alpha c x m i
][
if color/4 [alpha: color/4]
; LOCAL: HSV, COLOR: RGB
bind/new [h s v] local: object []
set words-of local color
bind/new [r g b] color: object []
do in local [
i: h / 60
c: v * s
x: 1 - (abs -1 + mod i 2) * c
m: v - c
]
do in color [
set [r g b] reduce switch to integer! i [
0 [[c x 0]]
1 [[x c 0]]
2 [[0 c x]]
3 [[0 x c]]
4 [[x 0 c]]
5 [[c 0 x]]
]
]
color: to tuple! map-each value values-of color [to integer! round m + value * 255]
if alpha [color/4: alpha * 255]
color
]
color!: object [
rgb: 0.0.0.0
web: #000000
hsl: make block! 4
hsv: make block! 4
]
new-color: does [make color! []]
set-color: func [
color [object!] "Color object"
value [block! tuple! issue!]
type [word!]
] [
switch type [
rgb [
do in color [
rgb: value
web: to-hex value
hsl: to-hsl value
hsv: to-hsv value
]
]
web [
do in color [
rgb: load-web-color value
web: value
hsl: to-hsl rgb
hsv: to-hsv rgb
]
]
hsl [
do in color [
rgb: load-hsl value
web: to-hex rgb
hsl: value
hsv: to-hsv load-hsv value
]
]
hsv [
do in color [
rgb: load-hsv value
web: to-hex rgb
hsl: to-hsl load-hsv value
hsv: value
]
]
]
color
]
;apply-color color 'saturate 50%
apply-color: func [
"Apply color effect on color"
color [object!] "Color! object"
effect [word!] "Effect to apply"
amount [number!] "Effect amount"
] [
effect: do bind select effects effect 'amount
set-color color color/:effect effect
]
effects: [
; return changed colorspace
darken [
color/hsl/3: max 0% color/hsl/3 - amount
'hsl
]
lighten [
color/hsl/3: min 100% color/hsl/3 + amount
'hsl
]
saturate [
color/hsl/2: min 100% max 0% color/hsl/2 + amount
;color/hsv/2: color/hsv/2 + (100% - color/hsv/2 * amount)
'hsl
]
desaturate [
color/hsl/2: min 100% max 0% color/hsl/2 - amount
'hsl
]
hue [
color/hsl/1: color/hsl/1 + amount // 360
'hsl
]
]