-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuColorUtils.pas
More file actions
186 lines (157 loc) · 5.03 KB
/
Copy pathuColorUtils.pas
File metadata and controls
186 lines (157 loc) · 5.03 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
{ uColorUtils.pas
Color Conversion Utilities v1.9
------------------------------------------------------------------------------
Provides RGB, HSB/HSV and HSL color conversion routines.
Supported ranges:
RGB : 0..255
H : 0..359
S : 0..100
B/L : 0..100
Used as the central color-conversion layer for the Color Picker.
Developer : Shohanur Rahman
Publisher : SR Studio 24 - BD
Copyright : © 2026 SR Studio 24 - BD
Website : https://srstudio24.blogspot.com/
IMPORTANT:
Conversion formulas, rounding behavior and value ranges are core logic.
Do not modify without testing all dependent color-picker features.
Internal project code.
Unauthorized modification or redistribution is not permitted.
}
unit uColorUtils;
interface
uses
System.SysUtils, System.Math, Vcl.Graphics;
procedure HSBtoRGBValues(H, S, B: Integer; out R, G, Bl: Byte);
procedure RGBtoHSBValues(R, G, B: Byte; var H, S, Br: Integer);
procedure HSLtoRGBValues(H, S, L: Integer; out R, G, Bl: Byte);
procedure RGBtoHSLValues(R, G, B: Byte; var H, S, L: Integer);
function ColorToHSB(const C: TColor; out H, S, B: Integer): Boolean;
function HSBToColor(H, S, B: Integer): TColor;
implementation
uses
Winapi.Windows;
procedure HSBtoRGBValues(H, S, B: Integer; out R, G, Bl: Byte);
var
Hf, Sf, Vf, C, X, M, Rf, Gf, Bf: Double;
Hi: Integer;
begin
H := ((H mod 360) + 360) mod 360;
S := EnsureRange(S, 0, 100);
B := EnsureRange(B, 0, 100);
Hf := H / 60.0;
Sf := S / 100.0;
Vf := B / 100.0;
C := Vf * Sf;
X := C * (1.0 - Abs(Frac(Hf * 0.5) * 2.0 - 1.0));
M := Vf - C;
Hi := Trunc(Hf) mod 6;
case Hi of
0: begin Rf := C; Gf := X; Bf := 0; end;
1: begin Rf := X; Gf := C; Bf := 0; end;
2: begin Rf := 0; Gf := C; Bf := X; end;
3: begin Rf := 0; Gf := X; Bf := C; end;
4: begin Rf := X; Gf := 0; Bf := C; end;
else
begin Rf := C; Gf := 0; Bf := X; end;
end;
R := EnsureRange(Round((Rf + M) * 255.0), 0, 255);
G := EnsureRange(Round((Gf + M) * 255.0), 0, 255);
Bl := EnsureRange(Round((Bf + M) * 255.0), 0, 255);
end;
procedure RGBtoHSBValues(R, G, B: Byte; var H, S, Br: Integer);
var
Rf, Gf, Bf, MaxC, MinC, Delta, Hf: Double;
begin
Rf := R / 255.0; Gf := G / 255.0; Bf := B / 255.0;
MaxC := Max(Rf, Max(Gf, Bf));
MinC := Min(Rf, Min(Gf, Bf));
Delta := MaxC - MinC;
Br := Round(MaxC * 100.0);
if MaxC <= 0.00001 then S := 0
else S := Round((Delta / MaxC) * 100.0);
if Delta >= 0.00001 then
begin
if Abs(MaxC - Rf) < 0.00001 then
Hf := 60.0 * ((Gf - Bf) / Delta)
else if Abs(MaxC - Gf) < 0.00001 then
Hf := 60.0 * ((Bf - Rf) / Delta + 2.0)
else
Hf := 60.0 * ((Rf - Gf) / Delta + 4.0);
if Hf < 0.0 then Hf := Hf + 360.0;
H := Round(Hf) mod 360;
end;
// else H is left unchanged (achromatic)
end;
procedure HSLtoRGBValues(H, S, L: Integer; out R, G, Bl: Byte);
var
Hf, Sf, Lf, C, X, M, Rf, Gf, Bf: Double;
Hi: Integer;
begin
H := ((H mod 360) + 360) mod 360;
S := EnsureRange(S, 0, 100);
L := EnsureRange(L, 0, 100);
Hf := H / 60.0;
Sf := S / 100.0;
Lf := L / 100.0;
C := (1.0 - Abs(2.0 * Lf - 1.0)) * Sf;
X := C * (1.0 - Abs(Frac(Hf * 0.5) * 2.0 - 1.0));
M := Lf - C * 0.5;
Hi := Trunc(Hf) mod 6;
case Hi of
0: begin Rf := C; Gf := X; Bf := 0; end;
1: begin Rf := X; Gf := C; Bf := 0; end;
2: begin Rf := 0; Gf := C; Bf := X; end;
3: begin Rf := 0; Gf := X; Bf := C; end;
4: begin Rf := X; Gf := 0; Bf := C; end;
else
begin Rf := C; Gf := 0; Bf := X; end;
end;
R := EnsureRange(Round((Rf + M) * 255.0), 0, 255);
G := EnsureRange(Round((Gf + M) * 255.0), 0, 255);
Bl := EnsureRange(Round((Bf + M) * 255.0), 0, 255);
end;
procedure RGBtoHSLValues(R, G, B: Byte; var H, S, L: Integer);
var
Rf, Gf, Bf, MaxC, MinC, Delta, Hf, Sf, Lf: Double;
begin
Rf := R / 255.0; Gf := G / 255.0; Bf := B / 255.0;
MaxC := Max(Rf, Max(Gf, Bf));
MinC := Min(Rf, Min(Gf, Bf));
Delta := MaxC - MinC;
Lf := (MaxC + MinC) * 0.5;
L := Round(Lf * 100.0);
if Delta < 0.00001 then
S := 0
else
begin
if Lf < 0.5 then Sf := Delta / (MaxC + MinC)
else Sf := Delta / (2.0 - MaxC - MinC);
S := Round(Sf * 100.0);
if Abs(MaxC - Rf) < 0.00001 then
Hf := 60.0 * ((Gf - Bf) / Delta)
else if Abs(MaxC - Gf) < 0.00001 then
Hf := 60.0 * ((Bf - Rf) / Delta + 2.0)
else
Hf := 60.0 * ((Rf - Gf) / Delta + 4.0);
if Hf < 0.0 then Hf := Hf + 360.0;
H := Round(Hf) mod 360;
end;
end;
function ColorToHSB(const C: TColor; out H, S, B: Integer): Boolean;
var
RGBVal: Longint;
begin
RGBVal := ColorToRGB(C);
H := 0;
RGBtoHSBValues(GetRValue(RGBVal), GetGValue(RGBVal), GetBValue(RGBVal), H, S, B);
Result := True;
end;
function HSBToColor(H, S, B: Integer): TColor;
var
R, G, Bl: Byte;
begin
HSBtoRGBValues(H, S, B, R, G, Bl);
Result := RGB(R, G, Bl);
end;
end.