| 1 |
unit mbColorConv; |
| 2 |
|
| 3 |
{$mode objfpc}{$H+} |
| 4 |
|
| 5 |
{ Compiling this causes a number of unavoidable FPC notes, |
| 6 |
|
| 7 |
Note: (6058) Call to subroutine "operator =(const op1:Variant;const op2:Variant):Boolean;" marked as inline is not inlined |
| 8 |
.... } |
| 9 |
{$notes off} |
| 10 |
|
| 11 |
interface |
| 12 |
|
| 13 |
uses |
| 14 |
Classes, SysUtils, Graphics; |
| 15 |
|
| 16 |
type |
| 17 |
TBrightnessMode = (bmLuminance, bmValue); |
| 18 |
|
| 19 |
{ HSL color model } |
| 20 |
|
| 21 |
function HSLtoColor(H, S, L: double): TColor; |
| 22 |
procedure HSLtoRGB(H, S, L: Double; out R, G, B: Integer); |
| 23 |
|
| 24 |
procedure ColortoHSL(c: TColor; out H, S, L: Double); |
| 25 |
procedure RGBtoHSL(R, G, B: Integer; out H, S, L: Double); |
| 26 |
|
| 27 |
{ HSV color model } |
| 28 |
|
| 29 |
procedure ColorToHSV(c: TColor; out H, S, V: Double); |
| 30 |
procedure RGBtoHSV(R, G, B: Integer; out H, S, V: Double); |
| 31 |
|
| 32 |
function HSVtoColor(H, S, V: Double): TColor; |
| 33 |
procedure HSVtoRGB(H, S, V: Double; out R, G, B: Integer); |
| 34 |
|
| 35 |
{ H, S, L, V extraction } |
| 36 |
|
| 37 |
function GetRelHValue(c: TColor): Double; |
| 38 |
function GetRelSValueHSL(c: TColor): Double; |
| 39 |
function GetRelSValueHSV(c: TColor): Double; |
| 40 |
function GetRelLValue(c: TColor): Double; |
| 41 |
function GetRelVValue(c: TColor): Double; |
| 42 |
|
| 43 |
|
| 44 |
implementation |
| 45 |
|
| 46 |
uses |
| 47 |
Math, LclIntf; |
| 48 |
|
| 49 |
function modulo(x, y: Double): Double; |
| 50 |
begin |
| 51 |
Result := x - floor(x / y) * y; |
| 52 |
end; |
| 53 |
|
| 54 |
//============================================================================== |
| 55 |
// HSL color model |
| 56 |
//============================================================================== |
| 57 |
|
| 58 |
function HSLToColor(H, S, L: Double): TColor; |
| 59 |
var |
| 60 |
R, G, B: Integer; |
| 61 |
begin |
| 62 |
HSLtoRGB(H, S, L, R, G, B); |
| 63 |
Result := RGBtoColor(R, G, B); |
| 64 |
end; |
| 65 |
(* |
| 66 |
procedure HSLtoRGB(H, S, L: double; out R, G, B: Integer); |
| 67 |
var |
| 68 |
C, X, m: Double; |
| 69 |
rr, gg, bb: Double; |
| 70 |
begin |
| 71 |
H := H * 360; |
| 72 |
C := (1 - abs(2*L - 1)) * S; |
| 73 |
X := C * (1 - abs(modulo(H / 60, 2) - 1)); |
| 74 |
m := L - C/2; |
| 75 |
if H < 60 then |
| 76 |
begin |
| 77 |
R := round((C + m) * 255); |
| 78 |
G := round((X + m) * 255); |
| 79 |
B := round(m * 255); |
| 80 |
end else |
| 81 |
if H < 120 then |
| 82 |
begin |
| 83 |
R := round((X + m) * 255); |
| 84 |
G := round((C + m) * 255); |
| 85 |
B := round(m * 255); |
| 86 |
end else |
| 87 |
if H < 180 then |
| 88 |
begin |
| 89 |
R := round(m * 255); |
| 90 |
G := round((C + m) * 255); |
| 91 |
B := round((X + m) * 255); |
| 92 |
end else |
| 93 |
if H < 240 then |
| 94 |
begin |
| 95 |
R := round(m * 255); |
| 96 |
G := round((X + m) * 255); |
| 97 |
B := round((C + m) * 255); |
| 98 |
end else |
| 99 |
if H < 300 then |
| 100 |
begin |
| 101 |
R := round((X + m) * 255); |
| 102 |
G := round(m * 255); |
| 103 |
B := round((C + m) * 255); |
| 104 |
end else |
| 105 |
begin |
| 106 |
R := round((C + m) * 255); |
| 107 |
G := round(m * 255); |
| 108 |
B := round((X + m) * 255); |
| 109 |
end; |
| 110 |
end; *) |
| 111 |
|
| 112 |
|
| 113 |
procedure HSLtoRGB(H, S, L: double; out R, G, B: Integer); |
| 114 |
var |
| 115 |
M1, M2: double; |
| 116 |
|
| 117 |
function HueToColorValue(Hue: double): byte; |
| 118 |
var |
| 119 |
V : double; |
| 120 |
begin |
| 121 |
if Hue > 10 then |
| 122 |
Hue := Hue + 1; |
| 123 |
if Hue < 0 then |
| 124 |
Hue := Hue + 1 |
| 125 |
else if Hue > 1 then |
| 126 |
Hue := Hue - 1; |
| 127 |
if 6 * Hue < 1 then |
| 128 |
V := M1 + (M2 - M1) * Hue * 6 |
| 129 |
else if 2 * Hue < 1 then |
| 130 |
V := M2 |
| 131 |
else if 3 * Hue < 2 then |
| 132 |
V := M1 + (M2 - M1) * (2/3 - Hue) * 6 |
| 133 |
else |
| 134 |
V := M1; |
| 135 |
Result := round(255 * V) |
| 136 |
end; |
| 137 |
|
| 138 |
begin |
| 139 |
if S = 0 then |
| 140 |
begin |
| 141 |
R := round(255 * L); |
| 142 |
G := R; |
| 143 |
B := R |
| 144 |
end |
| 145 |
else |
| 146 |
begin |
| 147 |
if L <= 0.5 then |
| 148 |
M2 := L * (1 + S) |
| 149 |
else |
| 150 |
M2 := L + S - L * S; |
| 151 |
M1 := 2 * L - M2; |
| 152 |
R := HueToColorValue(H + 1/3); |
| 153 |
G := HueToColorValue(H); |
| 154 |
B := HueToColorValue(H - 1/3) |
| 155 |
end; |
| 156 |
end; |
| 157 |
|
| 158 |
procedure ColorToHSL(c: TColor; out H, S, L: Double); |
| 159 |
begin |
| 160 |
RGBtoHSL(GetRValue(c), GetGValue(c), GetBValue(c), H, S, L); |
| 161 |
end; |
| 162 |
|
| 163 |
// From: http://www.rapidtables.com/convert/color/rgb-to-hsl.htm |
| 164 |
procedure RGBtoHSL(R, G, B: Integer; out H, S, L: Double); |
| 165 |
var |
| 166 |
rr, gg, bb, Cmax, Cmin, delta: double; |
| 167 |
begin |
| 168 |
rr := R / 255; |
| 169 |
gg := G / 255; |
| 170 |
bb := B / 255; |
| 171 |
Cmax := MaxValue([rr, gg, bb]); |
| 172 |
Cmin := MinValue([rr, gg, bb]); |
| 173 |
delta := (Cmax - Cmin); |
| 174 |
if delta = 0 then |
| 175 |
begin |
| 176 |
H := 0; |
| 177 |
S := 0; |
| 178 |
end else |
| 179 |
begin |
| 180 |
// Calculate L |
| 181 |
L := (Cmax + Cmin) / 2; |
| 182 |
|
| 183 |
// Calculate H |
| 184 |
if Cmax = rr then |
| 185 |
begin |
| 186 |
H := modulo((gg - bb) / delta, 6); |
| 187 |
{ |
| 188 |
H := ((gg - bb) / delta); |
| 189 |
H := H - floor(H / 6); |
| 190 |
} |
| 191 |
H := H * 60; |
| 192 |
end else |
| 193 |
if Cmax = gg then |
| 194 |
H := 60 * ((bb - rr) / delta + 2) |
| 195 |
else |
| 196 |
if Cmax = bb then |
| 197 |
H := 60 * ((rr - gg) / delta + 4) |
| 198 |
else |
| 199 |
H := 0; |
| 200 |
H := H / 360; |
| 201 |
|
| 202 |
// Calculate S |
| 203 |
S := delta / (1 - abs(2 * L - 1)); |
| 204 |
end; |
| 205 |
end; |
| 206 |
|
| 207 |
|
| 208 |
(* |
| 209 |
procedure RGBtoHSL(R, G, B: Integer; out H, S, L: Double); |
| 210 |
var |
| 211 |
rr, gg, bb, D, Cmax, Cmin: double; |
| 212 |
begin |
| 213 |
rr := R / 255; |
| 214 |
gg := G / 255; |
| 215 |
bb := B / 255; |
| 216 |
Cmax := MaxValue([rr, gg, bb]); |
| 217 |
Cmin := MinValue([rr, gg, bb]); |
| 218 |
L := (Cmax + Cmin) / 2; |
| 219 |
if Cmax = Cmin then |
| 220 |
begin |
| 221 |
H := 0; |
| 222 |
S := 0; |
| 223 |
end |
| 224 |
else |
| 225 |
begin |
| 226 |
D := Cmax - Cmin; |
| 227 |
//calc S |
| 228 |
if L < 0.5 then |
| 229 |
S := D / (Cmax + Cmin) |
| 230 |
else |
| 231 |
S := D / (2 - Cmax - Cmin); |
| 232 |
//calc H |
| 233 |
if R = Cmax then |
| 234 |
H := (gg - bb) / D |
| 235 |
else if G = Cmax then |
| 236 |
H := 2 + (bb - rr) /D |
| 237 |
else |
| 238 |
H := 4 + (rr - gg) / D; |
| 239 |
H := H / 6; |
| 240 |
if H < 0 then |
| 241 |
H := H + 1; |
| 242 |
end; |
| 243 |
end; |
| 244 |
*) |
| 245 |
|
| 246 |
//============================================================================== |
| 247 |
// HSV color model |
| 248 |
//============================================================================== |
| 249 |
|
| 250 |
{ Assumes H, S, V in the range 0..1 and calculates the R, G, B values which are |
| 251 |
returned to be in the range 0..255. |
| 252 |
From: http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c |
| 253 |
} |
| 254 |
procedure HSVtoRGB(H, S, V: Double; out R, G, B: Integer); |
| 255 |
var |
| 256 |
i: Integer; |
| 257 |
f: Double; |
| 258 |
p, q, t: Double; |
| 259 |
|
| 260 |
procedure MakeRgb(rr, gg, bb: Double); |
| 261 |
begin |
| 262 |
R := Round(rr * 255); |
| 263 |
G := Round(gg * 255); |
| 264 |
B := Round(bb * 255); |
| 265 |
end; |
| 266 |
|
| 267 |
begin |
| 268 |
i := floor(H * 6); |
| 269 |
f := H * 6 - i; |
| 270 |
p := V * (1 - S); |
| 271 |
q := V * (1 - f*S); |
| 272 |
t := V * (1 - (1 - f) * S); |
| 273 |
case i mod 6 of |
| 274 |
0: MakeRGB(V, t, p); |
| 275 |
1: MakeRGB(q, V, p); |
| 276 |
2: MakeRGB(p, V, t); |
| 277 |
3: MakeRGB(p, q, V); |
| 278 |
4: MakeRGB(t, p, V); |
| 279 |
5: MakeRGB(V, p, q); |
| 280 |
else MakeRGB(0, 0, 0); |
| 281 |
end; |
| 282 |
end; |
| 283 |
|
| 284 |
function HSVToColor(H, S, V: Double): TColor; |
| 285 |
var |
| 286 |
r, g, b: Integer; |
| 287 |
begin |
| 288 |
HSVtoRGB(H, S, V, r, g, b); |
| 289 |
Result := RgbToColor(r, g, b); |
| 290 |
end; |
| 291 |
|
| 292 |
{ Assumes R, G, B to be in range 0..255. Calculates H, S, V in range 0..1 |
| 293 |
From: http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c } |
| 294 |
|
| 295 |
procedure ColorToHSV(c: TColor; out H, S, V: Double); |
| 296 |
begin |
| 297 |
RGBToHSV(GetRValue(c), GetGValue(c), GetBValue(c), H, S, V); |
| 298 |
end; |
| 299 |
|
| 300 |
procedure RGBToHSV(R, G, B: Integer; out H, S, V: Double); |
| 301 |
var |
| 302 |
rr, gg, bb: Double; |
| 303 |
cmax, cmin, delta: Double; |
| 304 |
begin |
| 305 |
rr := R / 255; |
| 306 |
gg := G / 255; |
| 307 |
bb := B / 255; |
| 308 |
cmax := MaxValue([rr, gg, bb]); |
| 309 |
cmin := MinValue([rr, gg, bb]); |
| 310 |
delta := cmax - cmin; |
| 311 |
if delta = 0 then |
| 312 |
begin |
| 313 |
H := 0; |
| 314 |
S := 0; |
| 315 |
end else |
| 316 |
begin |
| 317 |
if cmax = rr then |
| 318 |
H := (gg - bb) / delta + IfThen(gg < bb, 6, 0) |
| 319 |
else if cmax = gg then |
| 320 |
H := (bb - rr) / delta + 2 |
| 321 |
else if (cmax = bb) then |
| 322 |
H := (rr -gg) / delta + 4; |
| 323 |
H := H / 6; |
| 324 |
S := delta / cmax; |
| 325 |
end; |
| 326 |
V := cmax; |
| 327 |
end; |
| 328 |
|
| 329 |
|
| 330 |
//============================================================================== |
| 331 |
// H, S, L, V extraction |
| 332 |
//============================================================================== |
| 333 |
|
| 334 |
function GetRelHValue(c: TColor): Double; |
| 335 |
var |
| 336 |
H, S, L: Double; |
| 337 |
begin |
| 338 |
ColorToHSL(c, H, S, L); // Could also use HSV - H is the same in both models |
| 339 |
Result := H; |
| 340 |
end; |
| 341 |
|
| 342 |
function GetRelSValueHSL(c: TColor): Double; |
| 343 |
var |
| 344 |
H, S, L: Double; |
| 345 |
begin |
| 346 |
ColorToHSL(c, H, S, L); |
| 347 |
Result := S; |
| 348 |
end; |
| 349 |
|
| 350 |
function GetRelSValueHSV(c: TColor): Double; |
| 351 |
var |
| 352 |
H, S, V: Double; |
| 353 |
begin |
| 354 |
ColorToHSV(c, H, S, V); |
| 355 |
Result := S; |
| 356 |
end; |
| 357 |
|
| 358 |
function GetRelLValue(c: TColor): Double; |
| 359 |
var |
| 360 |
H, S, L: Double; |
| 361 |
begin |
| 362 |
ColorToHSL(c, H, S, L); |
| 363 |
result := L; |
| 364 |
end; |
| 365 |
|
| 366 |
function GetRelVValue(c: TColor): Double; |
| 367 |
var |
| 368 |
H, S, V: Double; |
| 369 |
begin |
| 370 |
ColorToHSV(c, H, S, V); |
| 371 |
Result := V; |
| 372 |
end; |
| 373 |
|
| 374 |
end. |
| 375 |
|