forked from HemulGM/Components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHGM.Utils.Color.pas
More file actions
569 lines (478 loc) · 12 KB
/
HGM.Utils.Color.pas
File metadata and controls
569 lines (478 loc) · 12 KB
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
unit HGM.Utils.Color;
interface
uses
System.Types, System.StrUtils, System.SysUtils, System.UIConsts,
System.UITypes;
type
TRGB = record
private
FB: Byte;
FG: Byte;
FColor: TColor;
FR: Byte;
procedure SetB(const Value: Byte);
procedure SetColor(const Value: TColor);
procedure SetG(const Value: Byte);
procedure SetR(const Value: Byte);
public
class operator Implicit(const Color: TColor): TRGB;
class operator Implicit(const RGB: TRGB): TColor;
class operator Implicit(const RGB: TRGB): string;
class function Create(const RGB: Cardinal): TRGB; overload; static;
class function Create(const R, G, B: Byte): TRGB; overload; static;
procedure SetRGB(const R, G, B: Byte);
property Color: TColor read FColor write SetColor;
property R: Byte read FR write SetR;
property G: Byte read FG write SetG;
property B: Byte read FB write SetB;
end;
TCMYK = record
private
FK: Byte;
FM: Byte;
FC: Byte;
FColor: TColor;
FY: Byte;
procedure SetC(const Value: Byte);
procedure SetColor(const Value: TColor);
procedure SetK(const Value: Byte);
procedure SetM(const Value: Byte);
procedure SetY(const Value: Byte);
public
class operator Implicit(const Color: TColor): TCMYK;
class operator Implicit(const CMYK: TCMYK): TColor;
class function Create(const C, M, Y, K: Byte): TCMYK; static;
procedure SetCMYK(const C, M, Y, K: Byte);
property Color: TColor read FColor write SetColor;
property C: Byte read FC write SetC;
property M: Byte read FM write SetM;
property Y: Byte read FY write SetY;
property K: Byte read FK write SetK;
end;
THSV = record
private
FColor: TColor;
FH: Double;
FS: Double;
FV: Double;
procedure SetColor(const Value: TColor);
procedure SetH(const Value: Double);
procedure SetS(const Value: Double);
procedure SetV(const Value: Double);
public
class operator Implicit(const Color: TColor): THSV;
class operator Implicit(const HSV: THSV): TColor;
class function Create(const H, S, V: Double): THSV; overload; static;
procedure SetHSV(const H, S, V: Double);
property Color: TColor read FColor write SetColor;
property H: Double read FH write SetH;
property S: Double read FS write SetS;
property V: Double read FV write SetV;
end;
function CMYKToColor(C, M, Y, K: Byte): TColor;
function RGBToColor(R, G, B: Byte): TColor;
function GetRValue(RGB: Cardinal): Byte;
function GetGValue(RGB: Cardinal): Byte;
function GetBValue(RGB: Cardinal): Byte;
function GetAValue(RGB: Cardinal): Byte;
function ColorToRGB(Color: TColor): TRGB;
function GrayColor(Color: TColor): TColor;
function RGBToHSV(R, G, B: Byte; var H, S, V: Double): Boolean;
function HSVToRGB(H, S, V: Double; var R, G, B: Byte): Boolean;
function HSVToColor(H, S, V: Double; var Value: TColor): Boolean;
procedure RGBToCMYK(const R, G, B: Byte; var C: Byte; var M: Byte; var Y: Byte; var K: Byte);
procedure CMYKToRGB(C, M, Y, K: Byte; var R: Byte; var G: Byte; var B: Byte);
procedure ColorCorrectCMYK(var C: Byte; var M: Byte; var Y: Byte; var K: Byte);
function HexToTColor(Value: string): TColor;
function ColorToHex(Color: TColor): string;
function ColorToHtml(Color: TColor): string;
function ColorToFMXColor(Color: TColor): string;
function FMXColorToColor(Color: string): TColor;
function HtmlToColor(Color: string): TColor;
function ColorToString(Color: TColor): string;
function InvertColor(const Color: TColor): TColor;
function VisibilityColor(const Color: TColor): TColor;
implementation
uses
{$IFDEF MSWINDOWS}
Winapi.Windows,
{$ENDIF}
System.Math;
function VisibilityColor(const Color: TColor): TColor;
begin
Result := RGBToColor(
IfThen(GetRValue(Color) > $40, $00, $FF),
IfThen(GetGValue(Color) > $40, $00, $FF),
IfThen(GetBValue(Color) > $40, $00, $FF));
end;
function InvertColor(const Color: TColor): TColor;
begin
Result := RGBToColor(
255 - GetRValue(Color),
255 - GetGValue(Color),
255 - GetBValue(Color));
end;
function ColorToString(Color: TColor): string;
begin
Result := IntToHex(GetAValue(Color), 2) + IntToHex(GetRValue(Color), 2) + IntToHex(GetGValue(Color), 2) + IntToHex(GetBValue
(Color), 2);
end;
function CMYKToColor(C, M, Y, K: Byte): TColor;
begin
Result := (K or (Y shl 8) or (M shl 16) or (C shl 24));
end;
procedure ColorToCMYK(const Color: TColor; var C, M, Y, K: Byte);
var
R, G, B: Byte;
begin
R := GetRValue(Color);
G := GetGValue(Color);
B := GetBValue(Color);
RGBToCMYK(R, G, B, C, M, Y, K);
end;
function RGBToColor(R, G, B: Byte): TColor;
begin
Result := (R or (G shl 8) or (B shl 16));
end;
function GetRValue(RGB: Cardinal): Byte;
begin
Result := Byte(RGB);
end;
function GetGValue(RGB: Cardinal): Byte;
begin
Result := Byte(RGB shr 8);
end;
function GetBValue(RGB: Cardinal): Byte;
begin
Result := Byte(RGB shr 16);
end;
function GetAValue(RGB: Cardinal): Byte;
begin
Result := Byte(RGB shr 24);
end;
function ColorToRGB(Color: TColor): TRGB;
begin
Result := Color;
end;
function GrayColor(Color: TColor): TColor;
var
Gr: Byte;
begin
Gr := Trunc((GetBValue(Color) + GetGValue(Color) + GetRValue(Color)) / 3);
Result := RGBToColor(Gr, Gr, Gr);
end;
function RGBToHSV(R, G, B: Byte; var H, S, V: Double): Boolean;
var
minRGB, maxRGB, delta: Double;
begin
H := 0.0;
minRGB := Min(Min(R, G), B);
maxRGB := Max(Max(R, G), B);
delta := (maxRGB - minRGB);
V := maxRGB;
if (maxRGB <> 0.0) then
S := 255.0 * delta / maxRGB
else
S := 0.0;
if (S <> 0.0) then
begin
if R = maxRGB then
H := (G - B) / delta
else if G = maxRGB then
H := 2.0 + (B - R) / delta
else if B = maxRGB then
H := 4.0 + (R - G) / delta
end
else
H := -1.0;
H := H * 60;
if H < 0.0 then
H := H + 360.0;
S := S / 255 * 100;
V := V / 255 * 100;
Result := True;
end;
function HSVToRGB(H, S, V: Double; var R, G, B: Byte): Boolean;
var
i: Integer;
f, p, q, t: Double;
procedure CopyOutput(const RV, GV, BV: Double);
const
RGBmax = 255;
begin
R := Round(RGBmax * RV);
G := Round(RGBmax * GV);
B := Round(RGBmax * BV);
end;
begin
S := S / 100;
V := V / 100;
H := H / 60;
//Assert(InRange(H, 0.0, 1.0));
//Assert(InRange(S, 0.0, 1.0));
//Assert(InRange(V, 0.0, 1.0));
if S = 0.0 then
begin
CopyOutput(B, B, B);
Result := True;
Exit;
end;
//H:=H*6.0;
i := floor(H);
f := H - i;
p := V * (1.0 - S);
q := V * (1.0 - S * f);
t := V * (1.0 - S * (1.0 - f));
case i of
0:
CopyOutput(V, t, p);
1:
CopyOutput(q, V, p);
2:
CopyOutput(p, V, t);
3:
CopyOutput(p, q, V);
4:
CopyOutput(t, p, V);
else
CopyOutput(V, p, q);
end;
Result := True;
end;
function HSVToColor(H, S, V: Double; var Value: TColor): Boolean;
var
R, G, B: Byte;
begin
Result := HSVToRGB(H, S, V, R, G, B);
if Result then
Value := RGBToColor(R, G, B)
else
Value := 0;
end;
procedure RGBToCMYK(const R, G, B: Byte; var C: Byte; var M: Byte; var Y: Byte; var K: Byte);
begin
C := 255 - R;
M := 255 - G;
Y := 255 - B;
if C < M then
K := C
else
K := M;
if Y < K then
K := Y;
if K > 0 then
begin
C := C - K;
M := M - K;
Y := Y - K;
end;
end;
procedure CMYKToRGB(C, M, Y, K: Byte; var R: Byte; var G: Byte; var B: Byte);
begin
if (Integer(C) + Integer(K)) < 255 then
R := 255 - (C + K)
else
R := 0;
if (Integer(M) + Integer(K)) < 255 then
G := 255 - (M + K)
else
G := 0;
if (Integer(Y) + Integer(K)) < 255 then
B := 255 - (Y + K)
else
B := 0;
end;
procedure ColorCorrectCMYK(var C: Byte; var M: Byte; var Y: Byte; var K: Byte);
var
MinColor: Byte;
begin
if C < M then
MinColor := C
else
MinColor := M;
if Y < MinColor then
MinColor := Y;
if MinColor + K > 255 then
MinColor := 255 - K;
C := C - MinColor;
M := M - MinColor;
Y := Y - MinColor;
K := K + MinColor;
end;
function HexToTColor(Value: string): TColor;
begin
Result := RGBToColor(StrToInt('$' + Copy(Value, 5, 2)), StrToInt('$' + Copy(Value, 3, 2)), StrToInt('$' + Copy(Value, 1, 2)));
end;
function ColorToHex(Color: TColor): string;
begin
Result := IntToHex(GetBValue(Color), 2) + IntToHex(GetGValue(Color), 2) + IntToHex(GetRValue(Color), 2);
end;
function ColorToHtml(Color: TColor): string;
begin
Result := '#' + IntToHex(GetRValue(Color), 2) + IntToHex(GetGValue(Color), 2) + IntToHex(GetBValue(Color), 2);
end;
function ColorToFMXColor(Color: TColor): string;
begin
Result := '#FF' + IntToHex(GetRValue(Color), 2) + IntToHex(GetGValue(Color), 2) + IntToHex(GetBValue(Color), 2);
end;
function HtmlToColor(Color: string): TColor;
begin
Result := StringToColor('$' + Copy(Color, 6, 2) + Copy(Color, 4, 2) + Copy(Color, 2, 2));
end;
function FMXColorToColor(Color: string): TColor;
begin
Result := StringToColor('$' + Copy(Color, 8, 2) + Copy(Color, 6, 2) + Copy(Color, 4, 2));
end;
{ TRGB }
class operator TRGB.Implicit(const Color: TColor): TRGB;
begin
{$IFDEF MSWINDOWS}
if Color < 0 then
Result.Color := GetSysColor(Color and $000000FF)
else
{$ENDIF}
Result.Color := Color;
end;
class function TRGB.Create(const RGB: Cardinal): TRGB;
begin
Result.Color := RGB;
end;
class function TRGB.Create(const R, G, B: Byte): TRGB;
begin
Result.SetRGB(R, G, B);
end;
class operator TRGB.Implicit(const RGB: TRGB): string;
begin
Result := IntToStr(RGB.Color);
end;
class operator TRGB.Implicit(const RGB: TRGB): TColor;
begin
Result := RGB.Color;
end;
procedure TRGB.SetColor(const Value: TColor);
begin
FColor := Value;
FR := GetRValue(Value);
FG := GetGValue(Value);
FB := GetBValue(Value);
end;
procedure TRGB.SetB(const Value: Byte);
begin
FB := Value;
FColor := RGBToColor(FR, FG, FB);
end;
procedure TRGB.SetG(const Value: Byte);
begin
FG := Value;
FColor := RGBToColor(FR, FG, FB);
end;
procedure TRGB.SetR(const Value: Byte);
begin
FR := Value;
FColor := RGBToColor(FR, FG, FB);
end;
procedure TRGB.SetRGB(const R, G, B: Byte);
begin
FR := R;
FG := G;
FB := B;
FColor := RGBToColor(R, G, B);
end;
{ TCMYK }
class function TCMYK.Create(const C, M, Y, K: Byte): TCMYK;
begin
Result.SetCMYK(C, M, Y, K);
end;
class operator TCMYK.Implicit(const CMYK: TCMYK): TColor;
begin
Result := CMYK.Color;
end;
class operator TCMYK.Implicit(const Color: TColor): TCMYK;
begin
Result.Color := Color;
end;
procedure TCMYK.SetColor(const Value: TColor);
begin
FColor := Value;
ColorToCMYK(FColor, FC, FM, FY, FK);
end;
procedure TCMYK.SetC(const Value: Byte);
begin
FC := Value;
FColor := CMYKToColor(FC, FM, FY, FK);
end;
procedure TCMYK.SetCMYK(const C, M, Y, K: Byte);
begin
FC := C;
FM := M;
FY := Y;
FK := K;
FColor := CMYKToColor(C, M, Y, K);
end;
procedure TCMYK.SetK(const Value: Byte);
begin
FK := Value;
FColor := CMYKToColor(FC, FM, FY, FK);
end;
procedure TCMYK.SetM(const Value: Byte);
begin
FM := Value;
FColor := CMYKToColor(FC, FM, FY, FK);
end;
procedure TCMYK.SetY(const Value: Byte);
begin
FY := Value;
FColor := CMYKToColor(FC, FM, FY, FK);
end;
{ THSV }
class function THSV.Create(const H, S, V: Double): THSV;
begin
Result.SetHSV(H, S, V);
end;
class operator THSV.Implicit(const Color: TColor): THSV;
begin
Result.Color := Color;
end;
class operator THSV.Implicit(const HSV: THSV): TColor;
begin
Result := HSV.Color;
end;
procedure THSV.SetColor(const Value: TColor);
var
RGB: TRGB;
begin
FColor := Value;
RGB := ColorToRGB(FColor);
RGBToHSV(RGB.R, RGB.G, RGB.B, FH, FS, FV);
end;
procedure THSV.SetHSV(const H, S, V: Double);
var
R, G, B: Byte;
begin
FH := H;
FS := S;
FV := V;
if HSVToRGB(H, S, V, R, G, B) then
FColor := RGBToColor(R, G, B)
else
FColor := 0;
end;
procedure THSV.SetH(const Value: Double);
begin
FH := Value;
if not HSVToColor(FH, FS, FV, FColor) then
FColor := 0;
end;
procedure THSV.SetS(const Value: Double);
begin
FS := Value;
if not HSVToColor(FH, FS, FV, FColor) then
FColor := 0;
end;
procedure THSV.SetV(const Value: Double);
begin
FV := Value;
if not HSVToColor(FH, FS, FV, FColor) then
FColor := 0;
end;
end.