-
Notifications
You must be signed in to change notification settings - Fork 0
/
colour_conv.pas
503 lines (447 loc) · 12.8 KB
/
colour_conv.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
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
{
This file is part of the Mufasa Macro Library (MML)
Copyright (c) 2009-2012 by Raymond van Venetië and Merlijn Wajer
MML 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.
MML 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 MML. If not, see <http://www.gnu.org/licenses/>.
See the file COPYING, included in this distribution,
for details about the copyright.
Colour Conversion Utilities for the Mufasa Macro Library
}
unit colour_conv;
{$mode objfpc}{$H+}
{$Inline on}
interface
uses
Classes, SysUtils,
Graphics, mufasatypes,
Math;
Function RGBtoColor(r,g,b : byte) : TColor; overload; inline;
Function RGBtoColor(r,g,b : integer) : TColor; overload; inline;
Procedure ColorToRGB(Color : integer;out r,g,b : byte); overload; inline;
Procedure ColorToRGB(Color : integer;out r,g,b : integer); overload; inline;
Procedure RGBToXYZ(R,G,B : byte;out x,y,z : Extended); inline;
Procedure XYZToRGB(X,Y,Z : Extended;out R,G,B: byte); overload; inline;
Procedure XYZToRGB(X,Y,Z : Extended;out R,G,B: integer); overload; inline;
Procedure RGBToHSL(RR,GG,BB : byte;out H,S,L : Extended); inline;
Procedure RGBToHSLNonFixed(RR,GG,BB : byte;out H,S,L : Extended); inline;
Procedure HSLtoRGB(H,S,L : extended;out R,G,B : Byte); inline;
Procedure ColorToHSL(Col: Integer; out h, s, l: Extended); inline;
procedure ColorToXYZ(color: Integer; out X, Y, Z: Extended); inline;
function XYZToColor(X, Y, Z: Extended): TColor; inline;
function HSLToColor(H, S, L: Extended): TColor; inline;
function BGRToRGB(BGR : TRGB32) : TColor; inline;
procedure XYZToHSL(X, Y, Z: Extended; out H, S, L: Extended); inline;
procedure HSLToXYZ(H, S, L: Extended; out X, Y, Z: Extended); inline;
procedure XYZtoCIELab(X, Y, Z: Extended; out L, a, b: Extended);
procedure CIELabtoXYZ(L, a, b: Extended; out X, Y, Z: Extended);
procedure CIELabToRGB(L, a, b: Extended; out rr, gg, bb: byte); overload; inline;
procedure CIELabToRGB(L, a, b: Extended; out rr, gg, bb: integer); overload; inline;
procedure RGBToCIELab(rr, gg, bb: byte; out L, a, b: Extended); overload; inline;
procedure RGBToCIELab(rr, gg, bb: integer; out L, a, b: Extended); overload; inline;
function CIELabToColor(L, a, b: Extended): TColor; inline;
procedure ColorToCIELab(Color: integer; out L, a, b: Extended); inline;
procedure CIELabToHSL(L, a, b: Extended; out HH, SS, LL: Extended); inline;
procedure HSLToCIELab(HH, SS, LL: Extended; out L, a, b: Extended); inline;
implementation
Const
OneDivThree = 1/3.0;
TwoDivThree = 2 / 3.0;
OneDivTwoPointFour = 1 / 2.4;
function BGRToRGB(BGR : TRGB32) : TColor;inline;
begin;
Result := BGR.R or BGR.g shl 8 or BGR.b shl 16;
end;
Function RGBtoColor(r,g,b : byte): TColor; overload; inline;
begin;
Result := R or g shl 8 or b shl 16;
end;
{/\
Translates the given Red (R), Green (G) and Blue (B) components to a TColor.
R, G and B are integers.
/\}
Function RGBtoColor(r,g,b : integer): TColor; overload; inline;
begin;
Result := R or g shl 8 or b shl 16;
end;
{/\
Translates the given win-32 color in the Red (R), Green (G) and Blue (B)
components. R, G and B are bytes.
/\}
Procedure ColorToRGB(Color : integer;out r,g,b : byte); overload; inline;
begin
R := Color and $ff;
G := Color shr 8 and $ff;
B := Color shr 16 and $ff;
end;
{/\
Translates the given win-32 color in the Red (R), Green (G) and Blue (B)
components. R, G and B are integers.
/\}
Procedure ColorToRGB(Color : integer;out r,g,b : integer); overload; inline;
begin
R := Color and $ff;
G := Color shr 8 and $ff;
B := Color shr 16 and $ff;
end;
{/\
Translates the given Red (R), Green (G) and Blue (B) components to
X, Y and Z components.
/\}
Procedure RGBToXYZ(R,G,B : byte;out x,y,z : Extended); inline;
var
Red,Green,Blue : Extended;
begin;
Red := R / 255;
Green := G / 255;
Blue := B / 255;
if Red > 0.04045 then
Red := Power( ( Red + 0.055 ) / 1.055 , 2.4) * 100
else
Red := Red * 7.73993808;
if Green > 0.04045 then
Green := Power( ( Green + 0.055 ) / 1.055 , 2.4) * 100
else
Green := Green * 7.73993808;
if Blue > 0.04045 then
Blue := Power( ( Blue + 0.055 ) / 1.055 , 2.4) * 100
else
Blue := Blue * 7.73993808;
X := Red * 0.4124 + Green * 0.3576 + Blue * 0.1805;
Y := Red * 0.2126 + Green * 0.7152 + Blue * 0.0722;
Z := Red * 0.0193 + Green * 0.1192 + Blue * 0.9505;
end;
{/\
Translates the given X, Y and Z components to
Red (R), Green (G) and Blue (B) components.
/\}
Procedure XYZToRGB(X,Y,Z : Extended;out R,G,B: byte); overload; inline;
var
TempR,TempG,TempB,Tempx,tempy,tempz : Extended;
begin;
Tempx := X / 100;
tempy := Y / 100;
tempz := Z / 100;
TempR := tempx * 3.2406 + tempy * -1.5372 + tempz * -0.4986;
TempG := tempx * -0.9689 + tempy * 1.8758 + tempz * 0.0415;
TempB := tempx * 0.0557 + tempy * -0.2040 + tempz * 1.0570;
if TempR > 0.0031308 then
TempR := 1.055 * ( Power(TempR, (OneDivTwoPointFour)) ) - 0.055
else
TempR := 12.92 * TempR;
if TempG > 0.0031308 then
TempG := 1.055 * ( Power(TempG, ( OneDivTwoPointFour)) ) - 0.055
else
TempG := 12.92 * TempG;
if TempB > 0.0031308 then
TempB := 1.055 * ( Power(TempB , ( OneDivTwoPointFour )) ) - 0.055
else
TempB := 12.92 * TempB;
R := Round(TempR * 255);
G := Round(TempG * 255);
B := Round(TempB * 255);
end;
Procedure XYZToRGB(X,Y,Z : Extended;out R,G,B: integer); overload; inline;
var
TempR,TempG,TempB: Extended;
begin;
TempR := x * 3.2406 + y * -1.5372 + z * -0.4986;
TempG := x * -0.9689 + y * 1.8758 + z * 0.0415;
TempB := x * 0.0557 + y * -0.2040 + z * 1.0570;
if TempR > 0.0031308 then
TempR := 1.055 * ( Power(TempR, (OneDivTwoPointFour)) ) - 0.055
else
TempR := 12.92 * TempR;
if TempG > 0.0031308 then
TempG := 1.055 * ( Power(TempG, ( OneDivTwoPointFour)) ) - 0.055
else
TempG := 12.92 * TempG;
if TempB > 0.0031308 then
TempB := 1.055 * ( Power(TempB , ( OneDivTwoPointFour )) ) - 0.055
else
TempB := 12.92 * TempB;
R := Round(TempR * 255);
G := Round(TempG * 255);
B := Round(TempB * 255);
end;
{/\
Translates the given Red (R), Green (G) and Blue (B) components to
H (Hue), S (Saturation) and L (Luminance) components.
/\}
Procedure RGBToHSL(RR,GG,BB : byte;out H,S,L : Extended); inline;
var
R, G, B, D, Cmax, Cmin: Extended;
begin
R := RR / 255;
G := GG / 255;
B := BB / 255;
CMin := R;
if G < Cmin then Cmin := G;
if B < Cmin then Cmin := B;
CMax := R;
if G > Cmax then Cmax := G;
if B > Cmax then Cmax := B;
L := 0.5 * (Cmax + Cmin);
if Cmax = Cmin then
begin
H := 0;
S := 0;
end else
begin;
D := Cmax - Cmin;
if L < 0.5 then
S := D / (Cmax + Cmin)
else
S := D / (2 - Cmax - Cmin);
if R = Cmax then
H := (G - B) / D
else
if G = Cmax then
H := 2 + (B - R) / D
else
H := 4 + (R - G) / D;
H := H / 6;
if H < 0 then
H := H + 1;
end;
H := H * 100;
S := S * 100;
L := L * 100;
end;
{/\
Translates the given Red (R), Green (G) and Blue (B) components to
H (Hue), S (Saturation) and L (Luminance) components.
This function does not multiply it by 100.
/\}
Procedure RGBToHSLNonFixed(RR,GG,BB : byte;out H,S,L : Extended); inline;
var
R, G, B, D, Cmax, Cmin: Extended;
begin
R := RR / 255;
G := GG / 255;
B := BB / 255;
CMin := R;
if G < Cmin then Cmin := G;
if B < Cmin then Cmin := B;
CMax := R;
if G > Cmax then Cmax := G;
if B > Cmax then Cmax := B;
L := 0.5 * (Cmax + Cmin);
if Cmax = Cmin then
begin
H := 0;
S := 0;
end else
begin;
D := Cmax - Cmin;
if L < 0.5 then
S := D / (Cmax + Cmin)
else
S := D / (2 - Cmax - Cmin);
if R = Cmax then
H := (G - B) / D
else
if G = Cmax then
H := 2 + (B - R) / D
else
H := 4 + (R - G) / D;
H := H / 6;
if H < 0 then
H := H + 1;
end;
end;
{/\
Translates the given H (Hue), S (Saturation) and L (Luminance) components to
Red (R), Green (G) and Blue (B) components.
/\}
procedure HSLtoRGB(H, S, L: extended; out R, G, B: Byte); inline;
var
Temp,Temp2 : Extended;
//begin
Function Hue2RGB(TempHue : Extended) : integer;
begin;
if TempHue < 0 then
TempHue := TempHue + 1
else if TempHue > 1 then
TempHue := TempHue - 1;
if ( ( 6 * TempHue ) < 1 ) then
Result :=Round(255 * (( Temp + ( Temp2 - Temp ) * 6 * TempHue )))
else if ( ( 2 * TempHue ) < 1 ) then
Result :=Round(255 * Temp2)
else if ( ( 3 * TempHue ) < 2 ) then
Result :=Round(255 * (Temp + ( Temp2 - Temp ) * ( ( TwoDivThree ) - TempHue ) * 6))
else
Result :=Round(255 * Temp);
end;
begin;
H := H / 100;
S := S / 100;
L := L / 100;
if s = 0 then
begin;
R := Byte(Round(L * 255));
G := R;
B := R;
end else
begin;
if (L < 0.5) then
Temp2 := L * ( 1 + S )
else
Temp2 := (L + S) - ( S * L);
Temp := 2 * L - Temp2;
R := Hue2RGB( H + ( OneDivThree ) );
G := Hue2RGB( H );
B := Hue2RGB( H - ( OneDivThree ) );
end;
end;
{/\
Split the Given Color col in H, S, L components.
/\}
Procedure ColorToHSL(Col: Integer; out h, s, l: Extended); inline;
Var
R, G, B: byte;
Begin
ColorToRGB(Col, R, G, B);
RGBToHSL(R, G, B, H, S, L);
End;
procedure ColorToXYZ(color: Integer; out X, Y, Z: Extended); inline;
var
R, G, B: byte;
begin
ColorToRGB(Color, R, G, B);
RGBToXYZ(R, G, B, X, Y, Z);
end;
function HSLToColor(H, S, L: Extended): TColor; inline;
var
r, g, b: byte;
begin
HSLToRGB(H, S, L, r, g, b);
Result := RGBToColor(r, g, b);
end;
function XYZToColor(X, Y, Z: Extended): TColor; inline;
var
r, g, b: byte;
begin
XYZToRGB(X, Y, Z, r, g, b);
Result := RGBToColor(r, g, b);
end;
procedure XYZToHSL(X, Y, Z: Extended; out H, S, L: Extended); inline;
var
r, g, b: byte;
begin
XYZToRGB(X, Y, Z, r, g, b);
RGBToHSL(r, g, b, H, S, L);
end;
procedure HSLToXYZ(H, S, L: Extended; out X, Y, Z: Extended); inline;
var
r, g, b: byte;
begin
HSLToRGB(H, S, L, r, g, b);
RGBToXYZ(r, g, b, X, Y, Z);
end;
procedure XYZtoCIELab(X, Y, Z: Extended; out L, a, b: Extended);
begin
X := X / 95.047;
Y := Y / 100.000;
Z := Z / 108.883;
if ( X > 0.008856 ) then
X := Power(X, 1.0/3.0)
else
X := ( 7.787 * X ) + ( 16.0 / 116.0 );
if ( Y > 0.008856 ) then
Y := Power(Y, 1.0/3.0)
else
Y := ( 7.787 * Y ) + ( 16.0 / 116.0 );
if ( Z > 0.008856 ) then
Z := Power(Z, 1.0/3.0)
else
Z := ( 7.787 * Z ) + ( 16.0 / 116.0 );
L := (116.0 * Y ) - 16.0;
a := 500.0 * ( X - Y );
b := 200.0 * ( Y - Z );
end;
procedure CIELabtoXYZ(L, a, b: Extended; out X, Y, Z: Extended);
begin
Y := ( L + 16 ) / 116.0;
X := ( a / 500.0 )+ Y;
Z := Y - ( b / 200.0 );
if ( Power(Y, 3) > 0.008856 ) then
Y := Power(Y, 3)
else
Y := ( Y - (16.0 / 116.0 )) / 7.787;
if ( Power(X, 3) > 0.008856 ) then
X := Power(X, 3)
else
X := ( X - (16.0 / 116.0) ) / 7.787;
if ( Power(Z, 3) > 0.008856 ) then
Z := Power(Z, 3)
else
Z := ( Z - (16.0 / 116.0) ) / 7.787;
X := 95.047 * X;
Y := 100.000 * Y;
Z := 108.883 * Z;
end;
procedure CIELabToRGB(L, a, b: Extended; out rr, gg, bb: byte); overload; inline;
var
X, Y, Z: Extended;
begin
CIELabToXYZ(L, a, b, X, Y, Z);
XYZToRGB(X, Y, Z, rr, gg, bb);
end;
procedure CIELabToRGB(L, a, b: Extended; out rr, gg, bb: integer); overload; inline;
var
X, Y, Z: Extended;
begin
CIELabToXYZ(L, a, b, X, Y, Z);
XYZToRGB(X, Y, Z, rr, gg, bb);
end;
procedure RGBToCIELab(rr, gg, bb: byte; out L, a, b: Extended); overload; inline;
var
X, Y, Z: Extended;
begin
RGBToXYZ(rr, gg, bb, X, Y, Z);
XYZtoCIELab(X, Y, Z, L, a, b);
end;
procedure RGBToCIELab(rr, gg, bb: integer; out L, a, b: Extended); overload; inline;
var
X, Y, Z: Extended;
begin
RGBToXYZ(rr, gg, bb, X, Y, Z);
XYZtoCIELab(X, Y, Z, L, a, b);
end;
function CIELabToColor(L, a, b: Extended): TColor; inline;
var
X, Y, Z: Extended;
begin
CIELabToXYZ(L, a, b, X, Y, Z);
Result := XYZToColor(X, Y, Z);
end;
procedure ColorToCIELab(Color: integer; out L, a, b: Extended); inline;
var
X, Y, Z: Extended;
begin
ColorToXYZ(Color, X, Y, Z);
XYZtoCIELab(X, Y, Z, L, a, b);
end;
procedure CIELabToHSL(L, a, b: Extended; out HH, SS, LL: Extended); inline;
var
rr, gg, bb: byte;
begin
CIELabToRGB(L, a, b, rr, gg, bb);
RGBToHSL(rr, gg, bb, HH, SS, LL);
end;
procedure HSLToCIELab(HH, SS, LL: Extended; out L, a, b: Extended); inline;
var
rr, gg, bb: byte;
begin
HSLtoRGB(HH, SS, LL, rr, gg, bb);
RGBToCIELab(rr, gg, bb, L, a, b);
end;
end.