| 1 |
unit RGBHSLUtils; |
| 2 |
|
| 3 |
{$IFDEF FPC} |
| 4 |
{$MODE DELPHI} |
| 5 |
{$ENDIF} |
| 6 |
|
| 7 |
interface |
| 8 |
|
| 9 |
uses |
| 10 |
LCLIntf, LCLType, Graphics, Math, Scanlines; |
| 11 |
|
| 12 |
var //set these variables to your needs, e.g. 360, 255, 255 |
| 13 |
MaxHue: integer = 359; |
| 14 |
MaxSat: integer = 240; |
| 15 |
MaxLum: integer = 240; |
| 16 |
|
| 17 |
{function HSLtoRGB(H, S, L: double): TColor;} |
| 18 |
function HSLRangeToRGB(H, S, L: integer): TColor; |
| 19 |
|
| 20 |
{procedure ColorToHSL(AColor: TColor; var H, S, L: Double);} |
| 21 |
function HSLtoColor(H, S, L: Double): TColor; |
| 22 |
|
| 23 |
{procedure RGBtoHSL(RGB: TColor; out H, S, L: Double); } |
| 24 |
procedure RGBtoHSLRange(RGB: TColor; out H1, S1, L1: integer); |
| 25 |
|
| 26 |
function GetHValue(AColor: TColor): integer; |
| 27 |
function GetSValue(AColor: TColor): integer; |
| 28 |
function GetLValue(AColor: TColor): integer; |
| 29 |
|
| 30 |
function HSLToRGBTriple(H, S, L : integer) : TRGBTriple; |
| 31 |
function HSLToRGBQuad(H, S, L: integer): TRGBQuad; |
| 32 |
procedure RGBTripleToHSL(RGBTriple : TRGBTriple; var h, s, l: integer); |
| 33 |
|
| 34 |
|
| 35 |
implementation |
| 36 |
|
| 37 |
uses |
| 38 |
mbUtils; |
| 39 |
(* |
| 40 |
procedure ColorToHSL(AColor: TColor; var H, S, L: Double); |
| 41 |
|
| 42 |
function RGBMaxValue(r, g, b: Double): Double; |
| 43 |
begin |
| 44 |
Result := r; |
| 45 |
if (Result < g) then Result := g; |
| 46 |
if (Result < b) then Result := b; |
| 47 |
end; |
| 48 |
|
| 49 |
function RGBMinValue(r, g, b: Double): Double; |
| 50 |
begin |
| 51 |
Result := r; |
| 52 |
if (Result > g) then Result := g; |
| 53 |
if (Result > b) then Result := b; |
| 54 |
end; |
| 55 |
|
| 56 |
var |
| 57 |
r, g, b: Double; |
| 58 |
delta, min: Double; |
| 59 |
begin |
| 60 |
r := GetRValue(AColor)/255; |
| 61 |
g := GetGValue(AColor)/255; |
| 62 |
b := GetBValue(AColor)/255; |
| 63 |
|
| 64 |
L := RGBMaxValue(r, g, b); |
| 65 |
min := RGBMinValue(r, g, b); |
| 66 |
delta := L - min; |
| 67 |
if (L = min) then |
| 68 |
begin |
| 69 |
H := 0.0; |
| 70 |
S := 0.0; |
| 71 |
end |
| 72 |
else |
| 73 |
begin |
| 74 |
S := delta / L; |
| 75 |
if r = L then |
| 76 |
H := 60 * (g - b)/delta |
| 77 |
else if g = L then |
| 78 |
H := 60 * (b - r)/delta + 120 |
| 79 |
else if b = L then |
| 80 |
H := 60 * (r - g)/delta + 240; |
| 81 |
if H < 0 then H := H + 360; |
| 82 |
H := H / 360; |
| 83 |
end; |
| 84 |
end; *) |
| 85 |
|
| 86 |
function HSLtoColor(H, S, L: Double): TColor; |
| 87 |
const |
| 88 |
Divisor = 255*60; |
| 89 |
var |
| 90 |
hTemp, f, LS, p, q, r: integer; |
| 91 |
intH, intS, intL: Integer; |
| 92 |
begin |
| 93 |
intH := round(H*360); |
| 94 |
intS := round(S*255); |
| 95 |
intL := round(L*255); |
| 96 |
if intH > 360 then dec(intH, 360); |
| 97 |
if intH < 0 then inc(intH, 360); |
| 98 |
Clamp(intS, 0, 255); |
| 99 |
Clamp(intL, 0, 255); |
| 100 |
if (intS = 0) then |
| 101 |
Result := RGBtoColor(intL, intL, intL) |
| 102 |
else |
| 103 |
begin |
| 104 |
hTemp := intH mod 360; |
| 105 |
f := hTemp mod 60; |
| 106 |
hTemp := hTemp div 60; |
| 107 |
LS := intL * intS; |
| 108 |
p := intL - LS div 255; |
| 109 |
q := intL - (LS*f) div Divisor; |
| 110 |
r := intL - (LS*(60 - f)) div Divisor; |
| 111 |
case hTemp of |
| 112 |
0: Result := RGBtoColor(intL, r, p); |
| 113 |
1: Result := RGBtoColor(q, intL, p); |
| 114 |
2: Result := RGBtoColor(p, intL, r); |
| 115 |
3: Result := RGBtoColor(p, q, intL); |
| 116 |
4: Result := RGBtoColor(r, p, intL); |
| 117 |
5: Result := RGBtoColor(intL, p, q); |
| 118 |
else |
| 119 |
Result := RGBtoColor(0, 0, 0); |
| 120 |
end; |
| 121 |
end; |
| 122 |
end; |
| 123 |
|
| 124 |
// ============================================================================= |
| 125 |
|
| 126 |
function HSLtoRGB(H, S, L: double): TColor; |
| 127 |
var |
| 128 |
M1, M2: double; |
| 129 |
|
| 130 |
function HueToColorValue(Hue: double): byte; |
| 131 |
var |
| 132 |
V : double; |
| 133 |
begin |
| 134 |
if Hue > 10 then |
| 135 |
Hue := Hue + 1; |
| 136 |
if Hue < 0 then |
| 137 |
Hue := Hue + 1 |
| 138 |
else if Hue > 1 then |
| 139 |
Hue := Hue - 1; |
| 140 |
if 6 * Hue < 1 then |
| 141 |
V := M1 + (M2 - M1) * Hue * 6 |
| 142 |
else if 2 * Hue < 1 then |
| 143 |
V := M2 |
| 144 |
else if 3 * Hue < 2 then |
| 145 |
V := M1 + (M2 - M1) * (2/3 - Hue) * 6 |
| 146 |
else |
| 147 |
V := M1; |
| 148 |
Result := round(255 * V) |
| 149 |
end; |
| 150 |
|
| 151 |
var |
| 152 |
R, G, B: byte; |
| 153 |
begin |
| 154 |
if S = 0 then |
| 155 |
begin |
| 156 |
R := round(255 * L); |
| 157 |
G := R; |
| 158 |
B := R |
| 159 |
end |
| 160 |
else |
| 161 |
begin |
| 162 |
if L <= 0.5 then |
| 163 |
M2 := L * (1 + S) |
| 164 |
else |
| 165 |
M2 := L + S - L * S; |
| 166 |
M1 := 2 * L - M2; |
| 167 |
R := HueToColorValue(H + 1/3); |
| 168 |
G := HueToColorValue(H); |
| 169 |
B := HueToColorValue(H - 1/3) |
| 170 |
end; |
| 171 |
Result := RGB(R, G, B) |
| 172 |
end; |
| 173 |
|
| 174 |
function HSLRangeToRGB(H, S, L: integer): TColor; |
| 175 |
begin |
| 176 |
Clamp(H, 0, MaxHue); |
| 177 |
Clamp(S, 0, MaxSat); |
| 178 |
Clamp(L, 0, MaxLum); |
| 179 |
Result := HSLToRGB(H / MaxHue, S / MaxSat, L / MaxLum); |
| 180 |
end; |
| 181 |
|
| 182 |
//============================================================================== |
| 183 |
|
| 184 |
procedure RGBtoHSL(RGB: TColor; out H, S, L: Double); |
| 185 |
var |
| 186 |
R, G, B, D, Cmax, Cmin: double; |
| 187 |
begin |
| 188 |
R := GetRValue(RGB) / 255; |
| 189 |
G := GetGValue(RGB) / 255; |
| 190 |
B := GetBValue(RGB) / 255; |
| 191 |
Cmax := Max(R, Max(G, B)); |
| 192 |
Cmin := Min(R, Min(G, B)); |
| 193 |
L := (Cmax + Cmin) / 2; |
| 194 |
if Cmax = Cmin then |
| 195 |
begin |
| 196 |
H := 0; |
| 197 |
S := 0; |
| 198 |
end |
| 199 |
else |
| 200 |
begin |
| 201 |
D := Cmax - Cmin; |
| 202 |
//calc S |
| 203 |
if L < 0.5 then |
| 204 |
S := D / (Cmax + Cmin) |
| 205 |
else |
| 206 |
S := D / (2 - Cmax - Cmin); |
| 207 |
//calc H |
| 208 |
if R = Cmax then |
| 209 |
H := (G - B) / D |
| 210 |
else if G = Cmax then |
| 211 |
H := 2 + (B - R) /D |
| 212 |
else |
| 213 |
H := 4 + (R - G) / D; |
| 214 |
H := H / 6; |
| 215 |
if H < 0 then |
| 216 |
H := H + 1; |
| 217 |
end; |
| 218 |
end; |
| 219 |
|
| 220 |
procedure RGBtoHSLRange(RGB: TColor; out H1, S1, L1: integer); |
| 221 |
var |
| 222 |
R, G, B, D, Cmax, Cmin, h, s, l: double; |
| 223 |
begin |
| 224 |
R := GetRValue(RGB) / 255; |
| 225 |
G := GetGValue(RGB) / 255; |
| 226 |
B := GetBValue(RGB) / 255; |
| 227 |
Cmax := Max(R, Max (G, B)); |
| 228 |
Cmin := Min(R, Min (G, B)); |
| 229 |
L := (Cmax + Cmin) / 2; |
| 230 |
if Cmax = Cmin then |
| 231 |
begin |
| 232 |
H := 0; |
| 233 |
S := 0; |
| 234 |
end |
| 235 |
else |
| 236 |
begin |
| 237 |
D := Cmax - Cmin; |
| 238 |
//calc L |
| 239 |
if L < 0.5 then |
| 240 |
S := D / (Cmax + Cmin) |
| 241 |
else |
| 242 |
S := D / (2 - Cmax - Cmin); |
| 243 |
//calc H |
| 244 |
if R = Cmax then |
| 245 |
H := (G - B) / D |
| 246 |
else if G = Cmax then |
| 247 |
H := 2 + (B - R) /D |
| 248 |
else |
| 249 |
H := 4 + (R - G) / D; |
| 250 |
H := H / 6; |
| 251 |
if H < 0 then |
| 252 |
H := H + 1; |
| 253 |
end; |
| 254 |
H1 := round(H * MaxHue); |
| 255 |
S1 := round(S * MaxSat); |
| 256 |
L1 := round(L * MaxLum); |
| 257 |
end; |
| 258 |
|
| 259 |
// ============================================================================= |
| 260 |
|
| 261 |
function GetHValue(AColor: TColor): integer; |
| 262 |
var |
| 263 |
d, h: integer; |
| 264 |
begin |
| 265 |
RGBToHSLRange(AColor, h, d, d); |
| 266 |
Result := h; |
| 267 |
end; |
| 268 |
|
| 269 |
function GetSValue(AColor: TColor): integer; |
| 270 |
var |
| 271 |
d, s: integer; |
| 272 |
begin |
| 273 |
RGBToHSLRange(AColor, d, s, d); |
| 274 |
Result := s; |
| 275 |
end; |
| 276 |
|
| 277 |
function GetLValue(AColor: TColor): integer; |
| 278 |
var |
| 279 |
d, l: integer; |
| 280 |
begin |
| 281 |
RGBToHSLRange(AColor, d, d, l); |
| 282 |
Result := l; |
| 283 |
end; |
| 284 |
|
| 285 |
function HSLToRGBTriple(H, S, L: integer): TRGBTriple; |
| 286 |
const |
| 287 |
Divisor = 255*60; |
| 288 |
var |
| 289 |
hTemp, f, LS, p, q, r: integer; |
| 290 |
begin |
| 291 |
Clamp(H, 0, MaxHue); |
| 292 |
Clamp(S, 0, MaxSat); |
| 293 |
Clamp(L, 0, MaxLum); |
| 294 |
if (S = 0) then |
| 295 |
Result := RGBToRGBTriple(L, L, L) |
| 296 |
else |
| 297 |
begin |
| 298 |
hTemp := H mod MaxHue; |
| 299 |
f := hTemp mod 60; |
| 300 |
hTemp := hTemp div 60; |
| 301 |
LS := L*S; |
| 302 |
p := L - LS div MaxLum; |
| 303 |
q := L - (LS*f) div Divisor; |
| 304 |
r := L - (LS*(60 - f)) div Divisor; |
| 305 |
case hTemp of |
| 306 |
0: Result := RGBToRGBTriple(L, r, p); |
| 307 |
1: Result := RGBToRGBTriple(q, L, p); |
| 308 |
2: Result := RGBToRGBTriple(p, L, r); |
| 309 |
3: Result := RGBToRGBTriple(p, q, L); |
| 310 |
4: Result := RGBToRGBTriple(r, p, L); |
| 311 |
5: Result := RGBToRGBTriple(L, p, q); |
| 312 |
else |
| 313 |
Result := RGBToRGBTriple(0, 0, 0); |
| 314 |
end; |
| 315 |
end; |
| 316 |
end; |
| 317 |
|
| 318 |
function HSLToRGBQuad(H, S, L: integer): TRGBQuad; |
| 319 |
const |
| 320 |
Divisor = 255*60; |
| 321 |
var |
| 322 |
hTemp, f, LS, p, q, r: integer; |
| 323 |
begin |
| 324 |
Clamp(H, 0, MaxHue); |
| 325 |
Clamp(S, 0, MaxSat); |
| 326 |
Clamp(L, 0, MaxLum); |
| 327 |
if (S = 0) then |
| 328 |
Result := RGBToRGBQuad(L, L, L) |
| 329 |
else |
| 330 |
begin |
| 331 |
hTemp := H mod MaxHue; |
| 332 |
f := hTemp mod 60; |
| 333 |
hTemp := hTemp div 60; |
| 334 |
LS := L*S; |
| 335 |
p := L - LS div MaxLum; |
| 336 |
q := L - (LS*f) div Divisor; |
| 337 |
r := L - (LS*(60 - f)) div Divisor; |
| 338 |
case hTemp of |
| 339 |
0: Result := RGBToRGBQuad(L, r, p); |
| 340 |
1: Result := RGBToRGBQuad(q, L, p); |
| 341 |
2: Result := RGBToRGBQuad(p, L, r); |
| 342 |
3: Result := RGBToRGBQuad(p, q, L); |
| 343 |
4: Result := RGBToRGBQuad(r, p, L); |
| 344 |
5: Result := RGBToRGBQuad(L, p, q); |
| 345 |
else |
| 346 |
Result := RGBToRGBQuad(0, 0, 0); |
| 347 |
end; |
| 348 |
end; |
| 349 |
end; |
| 350 |
|
| 351 |
procedure RGBTripleToHSL(RGBTriple: TRGBTriple; var h, s, l: integer); |
| 352 |
|
| 353 |
function RGBMaxValue(RGB: TRGBTriple): byte; |
| 354 |
begin |
| 355 |
Result := RGB.rgbtRed; |
| 356 |
if (Result < RGB.rgbtGreen) then Result := RGB.rgbtGreen; |
| 357 |
if (Result < RGB.rgbtBlue) then Result := RGB.rgbtBlue; |
| 358 |
end; |
| 359 |
|
| 360 |
function RGBMinValue(RGB: TRGBTriple) : byte; |
| 361 |
begin |
| 362 |
Result := RGB.rgbtRed; |
| 363 |
if (Result > RGB.rgbtGreen) then Result := RGB.rgbtGreen; |
| 364 |
if (Result > RGB.rgbtBlue) then Result := RGB.rgbtBlue; |
| 365 |
end; |
| 366 |
|
| 367 |
var |
| 368 |
Delta, Min: byte; |
| 369 |
begin |
| 370 |
L := RGBMaxValue(RGBTriple); |
| 371 |
Min := RGBMinValue(RGBTriple); |
| 372 |
Delta := L-Min; |
| 373 |
if (L = Min) then |
| 374 |
begin |
| 375 |
H := 0; |
| 376 |
S := 0; |
| 377 |
end |
| 378 |
else |
| 379 |
begin |
| 380 |
S := MulDiv(Delta, 255, L); |
| 381 |
with RGBTriple do |
| 382 |
begin |
| 383 |
if (rgbtRed = L) then |
| 384 |
H := MulDiv(60, rgbtGreen-rgbtBlue, Delta) |
| 385 |
else if (rgbtGreen = L) then |
| 386 |
H := MulDiv(60, rgbtBlue-rgbtRed, Delta) + 120 |
| 387 |
else if (rgbtBlue = L) then |
| 388 |
H := MulDiv(60, rgbtRed-rgbtGreen, Delta) + 240; |
| 389 |
if (H < 0) then H := H + 360; |
| 390 |
end; |
| 391 |
end; |
| 392 |
end; |
| 393 |
|
| 394 |
end. |