-
Notifications
You must be signed in to change notification settings - Fork 19
/
Color.ahk
executable file
·308 lines (281 loc) · 10.9 KB
/
Color.ahk
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
#NoEnv
SetFormat, IntegerFast, Hex
c := new Color(Color.Unpack(0xAAFF00))
MsgBox % Color.Pack(c.RGB)
MsgBox % Color.Pack(c.HSV)
class Color
{
__New(Value = "",Type = "RGB")
{
this.Red := 0, this.Green := 0, this.Blue := 0
If (Value != "")
{
If (Type = "RGB")
this.RGB := Value
If (Type = "BGR")
this.BGR := Value
Else If (Type = "HSV")
this.HSV := Value
}
}
Pack(Triple)
{
;retrieve components as byte values
Component1 := Round(Triple[1] * 0xFF)
Component2 := Round(Triple[2] * 0xFF)
Component3 := Round(Triple[3] * 0xFF)
;clamp HDR values
Component1 := (Component1 < 0) ? 0 : ((Component1 > 0xFF) ? 0xFF : Component1)
Component2 := (Component2 < 0) ? 0 : ((Component2 > 0xFF) ? 0xFF : Component2)
Component3 := (Component3 < 0) ? 0 : ((Component3 > 0xFF) ? 0xFF : Component3)
Return, (Component1 << 16) | (Component2 << 8) | Component3
}
Unpack(Value)
{
;retrieve components packed in lower 3 bytes
Component1 := (Value >> 16) / 0xFF
Component2 := (((Value & 0xFF00) >> 8) & 0xFF) / 0xFF
Component3 := (Value & 0xFF) / 0xFF
Return, [Component1, Component2, Component3]
}
_RGB(Color = "")
{
If (Color = "")
Return, [this.Red, this.Green, this.Blue]
Else
{
this.Red := Color[1]
this.Green := Color[2]
this.Blue := Color[3]
}
}
_BGR(Color = "")
{
If (Color = "")
{
Return, [this.Blue, this.Green, this.Red]
}
Else
{
this.Red := Color[3]
this.Green := Color[2]
this.Blue := Color[1]
}
}
_Hue(Color = "")
{
If (Color = "")
{
Minimum := (this.Red < this.Green) ? ((this.Red < this.Blue) ? this.Red : this.Blue) : ((this.Green < this.Blue) ? this.Green : this.Blue)
Value := (this.Red > this.Green) ? ((this.Red > this.Blue) ? this.Red : this.Blue) : ((this.Green > this.Blue) ? this.Green : this.Blue)
Delta := Value - Minimum
If Delta = 0 ;achromatic
Return, 0 ;arbitrary hue
If this.Red = Value ;between yellow and magenta
Hue := (this.Green - this.Blue) / Delta
Else If this.Green = Value ;between cyan and yellow
Hue := 2 + ((this.Blue - this.Red) / Delta)
Else ;between magenta and cyan
Hue := 4 + ((this.Red - this.Green) / Delta)
If Hue < 0
Hue += 6
Return, Hue / 6
}
Else
{
;check input validity
If Color Is Not Number
throw Exception("Invalid hue component: " . Color . ".",-1)
;wip
}
}
_Saturation(Color = "")
{
If (Color = "")
{
Minimum := (this.Red < this.Green) ? ((this.Red < this.Blue) ? this.Red : this.Blue) : ((this.Green < this.Blue) ? this.Green : this.Blue)
Value := (this.Red > this.Green) ? ((this.Red > this.Blue) ? this.Red : this.Blue) : ((this.Green > this.Blue) ? this.Green : this.Blue)
Delta := Value - Minimum
If Delta = 0 ;achromatic
Return, 0 ;no saturation
Return, Delta / Value
}
Else
{
;check input validity
If Color Is Not Number
throw Exception("Invalid saturation component: " . Color . ".",-1)
;wip
}
}
_Value(Color = "")
{
If (Color = "")
{
;value is the magnitude of the greatest component
Return, (this.Red > this.Green) ? ((this.Red > this.Blue) ? this.Red : this.Blue) : ((this.Green > this.Blue) ? this.Green : this.Blue)
}
Else
{
;check input validity
If Color Is Not Number
throw Exception("Invalid value component: " . Color . ".",-1)
;wip
}
}
_HSV(Color = "")
{
If (Color = "")
{
;wip: handle HDR
Minimum := (this.Red < this.Green) ? ((this.Red < this.Blue) ? this.Red : this.Blue) : ((this.Green < this.Blue) ? this.Green : this.Blue)
Value := (this.Red > this.Green) ? ((this.Red > this.Blue) ? this.Red : this.Blue) : ((this.Green > this.Blue) ? this.Green : this.Blue)
Delta := Value - Minimum
If Delta = 0 ;achromatic
Return, Round(Value * 0xFF) ;arbitrary hue and no saturation
If this.Red = Value ;between yellow and magenta
Hue := (this.Green - this.Blue) / Delta
Else If this.Green = Value ;between cyan and yellow
Hue := 2 + ((this.Blue - this.Red) / Delta)
Else ;between magenta and cyan
Hue := 4 + ((this.Red - this.Green) / Delta)
If Hue < 0
Hue += 6
Hue /= 6
Saturation := Delta / Value
Return, [Hue, Saturation, Value]
}
Else
{
;retrieve components
Hue := Color[1]
Saturation := Color[2]
Value := Color[3]
Hue *= 6
Sector := Floor(Hue)
FractionalHue := Hue - Sector
Component1 := Value * (1 - Saturation)
Component2 := Value * (1 - (Saturation * FractionalHue))
Component3 := Value * (1 - (Saturation * (1 - FractionalHue)))
If Sector = 0 ;zeroth sector
this.Red := Value, this.Green := Component3, this.Blue := Component1
Else If Sector = 1 ;first sector
this.Red := Component2, this.Green := Value, this.Blue := Component1
Else If Sector = 2 ;second sector
this.Red := Component1, this.Green := Value, this.Blue := Component3
Else If Sector = 3 ;third sector
this.Red := Component1, this.Green := Component2, this.Blue := Value
Else If Sector = 4 ;fourth sector
this.Red := Component3, this.Green := Component1, this.Blue := Value
Else ;fifth sector
this.Red := Value, this.Green := Component1, this.Blue := Component2
}
}
__Get(Key)
{
If (Key = "RGB")
Return, this._RGB()
If (Key = "BGR")
Return, this._BGR()
If (Key = "Hue")
Return, this._Hue()
If (Key = "Saturation")
Return, this._Saturation()
If (Key = "Value")
Return, this._Value()
If (Key = "HSV")
Return, this._HSV()
throw Exception("Unknown key: " . Key,-1)
}
__Set(Key,Color)
{
If (Key = "Red" || Key = "Green" || Key = "Blue") ;wip: this never seems to run
{
If Color Is Not Number
throw Exception("Invalid RGB color component: " . Color . ".",-1)
ObjInsert(this,Key,Color)
}
Else If (Key = "RGB")
this._RGB(Color)
Else If (Key = "BGR")
this._BGR(Color)
Else If (Key = "Hue")
this._Hue(Color)
Else If (Key = "Saturation")
this._Saturation(Color)
Else If (Key = "Value")
this._Value(Color)
Else If (Key = "Hue" || Key = "Saturation" || Key = "Value")
{
HSV := this._HSV()
Hue := HSV[1], Saturation := HSV[2], Value := HSV[3]
If (Key = "Hue")
Hue := Color
Else If (Key = "Saturation")
Saturation := Color
Else If (Key = "Value")
Value := Color
;wip: HDR values
Sector := Floor(Hue)
FractionalHue := Hue - Sector
Component1 := Value * (1 - Saturation)
Component2 := Value * (1 - (Saturation * FractionalHue))
Component3 := Value * (1 - (Saturation * (1 - FractionalHue)))
If Sector = 0 ;zeroth sector
this.Red := Value, this.Green := Component3, this.Blue := Component1
Else If Sector = 1 ;first sector
this.Red := Component2, this.Green := Value, this.Blue := Component1
Else If Sector = 2 ;second sector
this.Red := Component1, this.Green := Value, this.Blue := Component3
Else If Sector = 3 ;third sector
this.Red := Component1, this.Green := Component2, this.Blue := Value
Else If Sector = 4 ;fourth sector
this.Red := Component3, this.Green := Component1, this.Blue := Value
Else ;fifth sector
this.Red := Value, this.Green := Component1, this.Blue := Component2
}
Else If (Key = "HSV")
this._HSV(Color)
Else
throw Exception("Unknown key: " . Key,-1)
Return, Color
}
}
RGBToHSV(RGB)
{
Minimum := (RGB.Red < RGB.Green) ? ((RGB.Red < RGB.Blue) ? RGB.Red : RGB.Blue) : ((RGB.Green < RGB.Blue) ? RGB.Green : RGB.Blue)
Value := (RGB.Red > RGB.Green) ? ((RGB.Red > RGB.Blue) ? RGB.Red : RGB.Blue) : ((RGB.Green > RGB.Blue) ? RGB.Green : RGB.Blue)
Delta := Value - Minimum
If Delta = 0 ;pure blackness
Return, Object("Hue",0,"Saturation",0,"Value",Value)
If RGB.Red = Value ;between yellow and magenta
Hue := (RGB.Green - RGB.Blue) / Delta
Else If RGB.Green = Value ;between cyan and yellow
Hue := 2 + ((RGB.Blue - RGB.Red) / Delta)
Else ;between magenta and cyan
Hue := 4 + ((RGB.Red - RGB.Green) / Delta)
If Hue < 0
Hue += 6
Hue /= 6
Return, Object("Hue",Hue,"Saturation",Delta / Value,"Value",Value)
}
HSVToRGB(HSV)
{
Hue := HSV.Hue * 6
Sector := Floor(Hue)
FractionalHue := Hue - Sector
Component1 := HSV.Value * (1 - HSV.Saturation)
Component2 := HSV.Value * (1 - (HSV.Saturation * FractionalHue))
Component3 := HSV.Value * (1 - (HSV.Saturation * (1 - FractionalHue)))
If Sector = 0 ;zeroth sector
Return, Object("Red",HSV.Value,"Green",Component3,"Blue",Component1)
If Sector = 1 ;first sector
Return, Object("Red",Component2,"Green",HSV.Value,"Blue",Component1)
If Sector = 2 ;second sector
Return, Object("Red",Component1,"Green",HSV.Value,"Blue",Component3)
If Sector = 3 ;third sector
Return, Object("Red",Component1,"Green",Component2,"Blue",HSV.Value)
If Sector = 4 ;fourth sector
Return, Object("Red",Component3,"Green",Component1,"Blue",HSV.Value)
Return, Object("Red",HSV.Value,"Green",Component1,"Blue",Component2) ;fifth sector
}