-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuColors.pas
457 lines (379 loc) · 9.94 KB
/
uColors.pas
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
{
This file is part of Teka, a drawing game for very young children who
will discover mouse moving.
Copyright (C) 2011-2012 João Marcelo S. Vaz
Teka is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Teka is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}
unit uColors;
{$mode objfpc}{$H+}
interface
uses
Graphics, Math;
type
{ TColorPallete }
TColorPallete = class
protected
function DoGetColor: TColor; virtual; abstract;
public
function GetColor: TColor;
end;
{ TCustomRainbowColorPallete }
TCustomRainbowColorPallete = class(TColorPallete)
private
fMaximumWavelength: Double;
fMinimumWavelength: Double;
function GetARainbowColor(AWavelength: Double): TColor;
public
property MaximumWavelength: Double read fMaximumWavelength;
property MinimumWavelength: Double read fMinimumWavelength;
end;
{ TRandomRainbowColorPallete }
TRandomRainbowColorPallete = class(TCustomRainbowColorPallete)
protected
function DoGetColor: TColor; override;
end;
{ TRainbowColorPallete }
TRainbowColorPallete = class(TCustomRainbowColorPallete)
private
fStep: Double;
fWavelength: Double;
procedure SetStep(const AValue: Double);
protected
function DoGetColor: TColor; override;
public
constructor Create;
property Step: Double read fStep write SetStep;
end;
{ TCustomWebSafeColorPallete }
TCustomWebSafeColorPallete = class(TColorPallete)
private
function GetAWebSafeColor(r_i, g_i, b_i: Integer): TColor;
end;
{ TRandomWebSafeColorPallete }
TRandomWebSafeColorPallete = class(TCustomWebSafeColorPallete)
protected
function DoGetColor: TColor; override;
end;
{
procedure HSVToRGB(const H, S, V: Single; out R, G, B: Single);
in
H = Hue. Range is from 0..1. 0.5 = 180 degrees, 1 = 360. or H < 0 for gray
S = Satration. Range is 0..1 where 0 is white and 1 is no saturation.
V = Value. Range is 0..255
out
R = 0..255
G = 0..255
B = 0..255
If H < 0 then the result is a gray value R=V, G=V, B=V
William Egge, [email protected]
http://www.eggcentric.com
}
procedure HSVToRGB(const H, S, V: Single; out R, G, B: Single);
{
procedure RGBToHSV(const R, G, B: Single; out H, S, V: Single);
in
R = 0..255
G = 0..255
B = 0..255
out
H = Hue. -1 for grey scale or range 0..1. 0..1 represents 0..360 degrees
S = Saturation. Range = 0..1. 0 = white, 1 = no saturation.
V = Value or intensity. Range 0..255
William Egge, [email protected]
http://www.eggcentric.com
}
procedure RGBToHSV(const R, G, B: Single; out H, S, V: Single);
procedure WavelengthToRGB(const Wavelength: Double; out R,G,B: Byte);
function GetRandomMixedColor(AColor: TColor): TColor;
function GetRandomGoldenRatioColor: TColor;
function GetRandomGaminePathColor: TColor;
function GetRandomGamineStarColor: TColor;
function GetRandomStandardColor: TColor;
implementation
procedure HSVToRGB(const H, S, V: Single; out R, G, B: Single);
const
SectionSize = 60/360;
var
Section: Single;
SectionIndex: Integer;
f: single;
p, q, t: Single;
begin
if H < 0 then
begin
R:= V;
G:= R;
B:= R;
end
else
begin
Section:= H/SectionSize;
SectionIndex:= Floor(Section);
f:= Section - SectionIndex;
p:= V * ( 1 - S );
q:= V * ( 1 - S * f );
t:= V * ( 1 - S * ( 1 - f ) );
case SectionIndex of
0:
begin
R:= V;
G:= t;
B:= p;
end;
1:
begin
R:= q;
G:= V;
B:= p;
end;
2:
begin
R:= p;
G:= V;
B:= t;
end;
3:
begin
R:= p;
G:= q;
B:= V;
end;
4:
begin
R:= t;
G:= p;
B:= V;
end;
else
R:= V;
G:= p;
B:= q;
end;
end;
end;
procedure RGBToHSV(const R, G, B: Single; out H, S, V: Single);
var
RGB: array[0..2] of Single;
MinIndex, MaxIndex: Integer;
Range: Single;
begin
RGB[0]:= R;
RGB[1]:= G;
RGB[2]:= B;
MinIndex:= 0;
if G < R then
MinIndex:= 1;
if B < RGB[MinIndex] then
MinIndex:= 2;
MaxIndex:= 0;
if G > R then
MaxIndex:= 1;
if B > RGB[MaxIndex] then
MaxIndex:= 2;
Range:= RGB[MaxIndex] - RGB[MinIndex];
// Check for a gray level
if Range = 0 then
begin
H:= -1; // Can't determine on greys, so set to -1
S:= 0; // Gray is at the center;
V:= R; // could choose R, G, or B because they are all the same value.
end
else
begin
case MaxIndex of
0: H:= (G-B)/Range;
1: H:= 2 + (B-R)/Range;
2: H:= 4 + (R-G)/Range;
end;
S:= Range/RGB[MaxIndex];
V:= RGB[MaxIndex];
H:= H * (1/6);
if H < 0 then
H:= 1 + H;
end;
end;
const
WavelengthMinimum = 380; // Nanometers
WavelengthMaximum = 780;
// Adapted http://www.physics.sfasu.edu/astro/color.html
procedure WavelengthToRGB(const Wavelength: Double; out R,G,B: Byte);
CONST
Gamma = 0.80;
IntensityMax = 255;
VAR
Blue : DOUBLE;
factor: DOUBLE;
Green : DOUBLE;
Red : DOUBLE;
FUNCTION Adjust(CONST Color, Factor: DOUBLE): INTEGER;
BEGIN
IF Color = 0.0
THEN RESULT := 0 // Don't want 0^x = 1 for x <> 0
ELSE RESULT := ROUND(IntensityMax * Power(Color * Factor, Gamma))
END {Adjust};
BEGIN
CASE TRUNC(Wavelength) OF
380..439:
BEGIN
Red := -(Wavelength - 440) / (440 - 380);
Green := 0.0;
Blue := 1.0
END;
440..489:
BEGIN
Red := 0.0;
Green := (Wavelength - 440) / (490 - 440);
Blue := 1.0
END;
490..509:
BEGIN
Red := 0.0;
Green := 1.0;
Blue := -(Wavelength - 510) / (510 - 490)
END;
510..579:
BEGIN
Red := (Wavelength - 510) / (580 - 510);
Green := 1.0;
Blue := 0.0
END;
580..644:
BEGIN
Red := 1.0;
Green := -(Wavelength - 645) / (645 - 580);
Blue := 0.0
END;
645..780:
BEGIN
Red := 1.0;
Green := 0.0;
Blue := 0.0
END;
ELSE
Red := 0.0;
Green := 0.0;
Blue := 0.0
END;
// Let the intensity fall off near the vision limits
CASE TRUNC(Wavelength) OF
380..419: factor := 0.3 + 0.7*(Wavelength - 380) / (420 - 380);
420..700: factor := 1.0;
701..780: factor := 0.3 + 0.7*(780 - Wavelength) / (780 - 700)
ELSE factor := 0.0
END;
R := Adjust(Red, Factor);
G := Adjust(Green, Factor);
B := Adjust(Blue, Factor)
END {WavelengthToRGB};
function GetRandomMixedColor(AColor: TColor): TColor;
var
r, g, b: Byte;
begin
r:= Trunc((Random(256) + Red(AColor))/2);
g:= Trunc((Random(256) + Green(AColor))/2);
b:= Trunc((Random(256) + Blue(AColor))/2);
Result:= RGBToColor(r, g, b);
end;
function GetRandomGoldenRatioColor: TColor;
const
golden_ratio_conjugate = 0.618033988749895;
var
h,s,v: Single;
r, g, b: Single;
begin
s:= 0.5;
v:= 0.95;
h:= Frac(random + golden_ratio_conjugate);
HSVToRGB(h, s, 255*v, r,g,b);
Result:= RGBToColor(Trunc(r), Trunc(g), Trunc(b));
end;
function GetRandomGaminePathColor: TColor;
var
r, g, b: Byte;
begin
r:= Random(128) + 128;
g:= Random(128) + 128;
b:= Random(128) + 128;
Result:= RGBToColor(r, g, b);
end;
function GetRandomGamineStarColor: TColor;
var
r, g, b: Byte;
begin
r:= 10*(Random(255) div 10);
g:= 10*(Random(255) div 10);
b:= 10*(Random(255) div 10);
Result:= RGBToColor(r, g, b);
end;
function GetRandomStandardColor: TColor;
const
Colors: array[0..(StandardColorsCount-1)] of TColor = (clBlack,clMaroon,clGreen,
clOlive,clNavy,clPurple,clTeal,clGray,
clSilver,clRed,clLime,clYellow,clBlue,
clFuchsia,clAqua,clWhite);
begin
Result:= Colors[Random(StandardColorsCount)];
end;
{ TRandomWebSafeColorPallete }
function TRandomWebSafeColorPallete.DoGetColor: TColor;
begin
Result:= GetAWebSafeColor(Random(6),Random(6),Random(6));
end;
{ TCustomWebSafeColorPallete }
function TCustomWebSafeColorPallete.GetAWebSafeColor(r_i, g_i, b_i: Integer
): TColor;
var
r, g, b: Byte;
begin
r:= r_i * $33;
g:= g_i * $33;
b:= b_i * $33;
Result:= RGBToColor(r, g, b);
end;
{ TRainbowColorPallete }
constructor TRainbowColorPallete.Create;
begin
inherited Create;
fWavelength:= WavelengthMinimum;
Step:= 5;
end;
function TRainbowColorPallete.DoGetColor: TColor;
begin
Result:= GetARainbowColor(fWavelength);
fWavelength:= fWavelength + Step;
if fWavelength > WavelengthMaximum then
fWavelength:= WavelengthMinimum;
end;
procedure TRainbowColorPallete.SetStep(const AValue: Double);
begin
if fStep=AValue then exit;
fStep:=AValue;
end;
{ TRandomRainbowColorPallete }
function TRandomRainbowColorPallete.DoGetColor: TColor;
begin
Result:= GetARainbowColor(WavelengthMinimum + Random*(WavelengthMaximum - WavelengthMinimum));
end;
{ TCustomRainbowColorPallete }
function TCustomRainbowColorPallete.GetARainbowColor(AWavelength: Double): TColor;
var
r, g, b: Byte;
begin
WavelengthToRGB(AWavelength,r,g,b);
Result:= RGBToColor(r, g, b);
end;
{ TColorPallete }
function TColorPallete.GetColor: TColor;
begin
Result:= DoGetColor;
end;
end.