Debian Codegraph @

src/base/castlecolors.pas

raw
1 {
2 Copyright 2003-2024 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Color utilities, including HSV <-> RGB conversion. }
17 unit CastleColors;
18
19 {$include castleconf.inc}
20
21 interface
22
23 uses Math, Classes,
24 CastleVectors, CastleClassUtils;
25
26 type
27 TCastleColor = TVector4;
28 PCastleColor = PVector4;
29 TCastleColorRGB = TVector3;
30 PCastleColorRGB = PVector3;
31
32 TCastleColorHelper = record helper for TVector4
33 { Get first 3 color components, ignore alpha. }
34 function RGB: TCastleColorRGB;
35 { Return color converted into grayscale. Alpha is left unchanged. }
36 function Grayscale: TCastleColor;
37 end;
38
39 TCastleColorRGBHelper = record helper for TVector3
40 { Return color converted into grayscale. }
41 function Grayscale: TCastleColorRGB;
42 end;
43
44 const
45 { Common color constants, for comfort.
46 They follow the CSS colors constants
47 [http://www.w3.org/TR/CSS21/syndata.html#color-units].
48 @groupBegin }
49 Maroon : TCastleColor = (X: 0.5; Y: 0.0; Z: 0.0; W: 1.0);
50 Red : TCastleColor = (X: 1.0; Y: 0.0; Z: 0.0; W: 1.0);
51 Orange : TCastleColor = (X: 1.0; Y: 0.65; Z: 0.0; W: 1.0);
52 Yellow : TCastleColor = (X: 1.0; Y: 1.0; Z: 0.0; W: 1.0);
53 Olive : TCastleColor = (X: 0.5; Y: 0.5; Z: 0.0; W: 1.0);
54 Purple : TCastleColor = (X: 0.5; Y: 0.0; Z: 0.5; W: 1.0);
55 Fuchsia: TCastleColor = (X: 1.0; Y: 0.0; Z: 1.0; W: 1.0);
56 White : TCastleColor = (X: 1.0; Y: 1.0; Z: 1.0; W: 1.0);
57 Lime : TCastleColor = (X: 0.0; Y: 1.0; Z: 0.0; W: 1.0);
58 Green : TCastleColor = (X: 0.0; Y: 1.0; Z: 0.0; W: 1.0);
59 Navy : TCastleColor = (X: 0.0; Y: 0.0; Z: 0.5; W: 1.0);
60 Blue : TCastleColor = (X: 0.0; Y: 0.0; Z: 1.0; W: 1.0);
61 Aqua : TCastleColor = (X: 0.0; Y: 1.0; Z: 1.0; W: 1.0);
62 Teal : TCastleColor = (X: 0.0; Y: 0.5; Z: 0.5; W: 1.0);
63 Black : TCastleColor = (X: 0.0; Y: 0.0; Z: 0.0; W: 1.0);
64 Silver : TCastleColor = (X: 0.75; Y: 0.75; Z: 0.75; W: 1.0);
65 Gray : TCastleColor = (X: 0.5; Y: 0.5; Z: 0.5; W: 1.0);
66 { @groupEnd }
67
68 { Additional color constants. } { }
69 LightRed : TCastleColor = (X: 1.0; Y: 0.33; Z: 0.33; W: 1.0);
70 LightGreen: TCastleColor = (X: 0.33; Y: 1.0; Z: 0.33; W: 1.0);
71 LightBlue : TCastleColor = (X: 0.33; Y: 0.33; Z: 1.0; W: 1.0);
72
73 WhiteRGB : TCastleColorRGB = (X: 1.0; Y: 1.0; Z: 1.0);
74 BlackRGB : TCastleColorRGB = (X: 0.0; Y: 0.0; Z: 0.0);
75 RedRGB : TCastleColorRGB = (X: 1.0; Y: 0.0; Z: 0.0);
76 YellowRGB : TCastleColorRGB = (X: 1.0; Y: 1.0; Z: 0.0);
77 GreenRGB : TCastleColorRGB = (X: 0.0; Y: 1.0; Z: 0.0);
78 BlueRGB : TCastleColorRGB = (X: 0.0; Y: 0.0; Z: 1.0);
79 GrayRGB : TCastleColorRGB = (X: 0.5; Y: 0.5; Z: 0.5);
80
81 { Deprecated, use WhiteRGB. @deprecated }
82 White3Single : TCastleColorRGB = (X: 1.0; Y: 1.0; Z: 1.0) deprecated;
83 { Deprecated, use BlackRGB. @deprecated }
84 Black3Single : TCastleColorRGB = (X: 0.0; Y: 0.0; Z: 0.0) deprecated;
85 { Deprecated, use RedRGB. @deprecated }
86 Red3Single : TCastleColorRGB = (X: 1.0; Y: 0.0; Z: 0.0) deprecated;
87 { Deprecated, use GreenRGB. @deprecated }
88 Green3Single : TCastleColorRGB = (X: 0.0; Y: 1.0; Z: 0.0) deprecated;
89 { Deprecated, use BlueRGB. @deprecated }
90 Blue3Single : TCastleColorRGB = (X: 0.0; Y: 0.0; Z: 1.0) deprecated;
91
92 { Calculate color luminance (intensity), for converting color to grayscale.
93 @groupBegin }
94 function GrayscaleValue(const v: TCastleColorRGB): Single; {$ifdef SUPPORTS_INLINE} inline; {$endif} overload;
95 function GrayscaleValue(const v: TCastleColor): Single; {$ifdef SUPPORTS_INLINE} inline; {$endif} overload;
96 function GrayscaleValue(const v: TVector3Byte): Byte; {$ifdef SUPPORTS_INLINE} inline; {$endif} overload;
97 function GrayscaleValue(const v: TVector4Byte): Byte; {$ifdef SUPPORTS_INLINE} inline; {$endif} overload;
98 { @groupEnd }
99
100 function Grayscale(const v: TCastleColorRGB): TCastleColorRGB; {$ifdef SUPPORTS_INLINE} inline; {$endif} overload;
101 function Grayscale(const v: TVector3Byte): TVector3Byte; {$ifdef SUPPORTS_INLINE} inline; {$endif} overload;
102 function Grayscale(const v: TCastleColor): TCastleColor; {$ifdef SUPPORTS_INLINE} inline; {$endif} overload;
103
104 type
105 { Function that processes RGB colors, used by TCastleImage.ModulateRGB. }
106 TColorModulatorByteFunc = function (const Color: TVector3Byte): TVector3Byte;
107
108 { Some functions matching TColorModulatorByteFunc type. }
109
110 { Convert color to grayscale.
111 @groupBegin }
112 function ColorGrayscaleByte(const Color: TVector3Byte): TVector3Byte;
113 { @groupEnd }
114
115 { Place color luminance (intensity) (calculated like for grayscale)
116 into the given color component. Set the other components zero.
117 @groupBegin }
118 function ColorRedConvertByte(const Color: TVector3Byte): TVector3Byte;
119 function ColorGreenConvertByte(const Color: TVector3Byte): TVector3Byte;
120 function ColorBlueConvertByte(const Color: TVector3Byte): TVector3Byte;
121 { @groupEnd }
122
123 { Set color values for two other channels to 0.
124 Note that it's something entirely different than
125 ImageConvertToChannelVar: here we preserve original channel values,
126 and remove values on two other channels.
127
128 @groupBegin }
129 function ColorRedStripByte(const Color: TVector3Byte): TVector3Byte;
130 function ColorGreenStripByte(const Color: TVector3Byte): TVector3Byte;
131 function ColorBlueStripByte(const Color: TVector3Byte): TVector3Byte;
132 { @groupEnd }
133
134 { Converting between RGB and HSV.
135
136 For HSV, we keep components as floating-point values,
137 with hue in 0..6 range, saturation and value in 0..1.
138
139 For RGB, we keep components as floating-point values (0..1 range).
140
141 @groupBegin }
142 function HsvToRgb(const Value: TVector3): TCastleColorRGB;
143 function HsvToRgba(const Value: TVector3; const Alpha: Single): TCastleColor;
144 function RgbToHsv(const Value: TVector3): TVector3; overload;
145 { @groupEnd }
146
147 function RgbToHsv(const Value: TVector3Byte): TVector3; overload; deprecated 'use float-based colors, like TCastleColorRGB and TCastleColor, not Byte-based like TVector3Byte';
148 function HsvToRgbByte(const Value: TVector3): TVector3Byte; deprecated 'use float-based colors, like TCastleColorRGB and TCastleColor, not Byte-based like TVector3Byte';
149
150 { Given two colors in RGB, interpolate them in HSV space. }
151 function LerpRgbInHsv(const A: Single; const V1, V2: TVector3): TVector3;
152
153 { Change color into a hexadecimal notation of it (like in HTML).
154 This color includes an alpha channel (as 4th component),
155 and so the output contains the alpha value at the end (so it's 8 hex digits),
156 unless alpha is opaque in which case it's not written (and result is 6
157 hex digits). }
158 function ColorToHex(const V: TCastleColor): String;
159
160 { Change color into a hexadecimal notation of it (like in HTML).
161 This color has no alpha channel,
162 so it's always 6 hex digits. }
163 function ColorRGBToHex(const V: TCastleColorRGB): string;
164
165 { Convert hexadecimal color notation (like in HTML) into an RGBA color.
166 Handles 8 or 6 digit color (RGB or RGBA with 2 letters per component;
167 for 6 digits, alpha is assumed to be 1.0 (opaque)).
168
169 @raises EConvertError In case of invalid color as string. }
170 function HexToColor(const S: string): TCastleColor;
171
172 { Convert hexadecimal color notation (like in HTML) into an RGB color.
173 Handles 8 or 6 digit color (RGB or RGBA with 2 letters per component;
174 for 8 digits, alpha is ignored).
175
176 @raises EConvertError In case of invalid color as string. }
177 function HexToColorRGB(const S: string): TCastleColorRGB;
178
179 { Change color opacity (alpha). }
180 function ColorOpacity(const Color: TCastleColor; const Opacity: Single): TCastleColor;
181
182 function FadeDarkColor(const Color: TCastleColor;
183 const FadeIntensity: Single): TCastleColor;
184 function FadeColor(const Color: TCastleColor;
185 const FadeIntensity: Single): TCastleColor;
186
187 {$define read_interface}
188 {$I castlecolors_persistent.inc}
189 {$undef read_interface}
190
191 implementation
192
193 uses SysUtils, CastleUtils, CastleStringUtils;
194
195 {$define read_implementation}
196 {$I castlecolors_persistent.inc}
197 {$undef read_implementation}
198
199 { TCastleColorHelper --------------------------------------------------------------- }
200
201 function TCastleColorHelper.RGB: TCastleColorRGB;
202 begin
203 Result := XYZ;
204 end;
205
206 function TCastleColorHelper.Grayscale: TCastleColor;
207 begin
208 Result := CastleColors.Grayscale(Self);
209 end;
210
211 { TCastleColorRGBHelper ------------------------------------------------------------ }
212
213 function TCastleColorRGBHelper.Grayscale: TCastleColorRGB;
214 begin
215 Result := CastleColors.Grayscale(Self);
216 end;
217
218 { grayscale ------------------------------------------------------------------ }
219
220 function GrayscaleValue(const v: TCastleColor): Single;
221 begin
222 { Weights to change RGB color to grayscale.
223
224 Grayscale color is just a color with red = green = blue.
225 So the simplest conversion of RGB to grayscale is just to set
226 all three R, G, B components to the average (R + G + B) / 3.
227 But, since human eye is most sensitive to green, then to red,
228 and least sensitive to blue, it's better to calculate this
229 with some non-uniform weights.
230
231 These weights are copied from libpng manual. }
232
233 Result := (0.212671 * V.X +
234 0.715160 * V.Y +
235 0.072169 * V.Z);
236 end;
237
238 function GrayscaleValue(const v: TCastleColorRGB): Single;
239 begin
240 Result := 0.212671 * V.X +
241 0.715160 * V.Y +
242 0.072169 * V.Z;
243 end;
244
245 function GrayscaleValue(const v: TVector3Byte): Byte;
246 begin
247 // force multiplication as Word
248 Result := (Word(54 ) * V.X +
249 Word(183) * V.Y +
250 Word(19 ) * V.Z) shr 8; //div 256;
251 end;
252
253 function GrayscaleValue(const v: TVector4Byte): Byte;
254 begin
255 Result := (Word(54 ) * V.X +
256 Word(183) * V.Y +
257 Word(19 ) * V.Z) shr 8; //div 256;
258 end;
259
260 function Grayscale(const v: TCastleColor): TCastleColor;
261 var
262 V3: TVector3 absolute V;
263 begin
264 Result.X := GrayscaleValue(V3);
265 Result.Y := Result.X;
266 Result.Z := Result.X;
267 Result.W := V.W;
268 end;
269
270 function Grayscale(const v: TCastleColorRGB): TCastleColorRGB;
271 begin
272 Result.X := GrayscaleValue(V);
273 Result.Y := Result.X;
274 Result.Z := Result.X;
275 end;
276
277 function Grayscale(const v: TVector3Byte): TVector3Byte;
278 begin
279 Result.X := GrayscaleValue(V);
280 Result.Y := Result.X;
281 Result.Z := Result.X;
282 end;
283
284 { color changing ------------------------------------------------------------ }
285
286 function ColorGrayscaleByte(const Color: TVector3Byte): TVector3Byte;
287 begin
288 Result := Grayscale(Color);
289 end;
290
291 function ColorRedConvertByte(const Color: TVector3Byte): TVector3Byte;
292 begin
293 Result.X := GrayscaleValue(Color);
294 Result.Y := 0;
295 Result.Z := 0;
296 end;
297
298 function ColorGreenConvertByte(const Color: TVector3Byte): TVector3Byte;
299 begin
300 Result.Y := GrayscaleValue(Color);
301 Result.X := 0;
302 Result.Z := 0;
303 end;
304
305 function ColorBlueConvertByte(const Color: TVector3Byte): TVector3Byte;
306 begin
307 Result.Z := GrayscaleValue(Color);
308 Result.X := 0;
309 Result.Y := 0;
310 end;
311
312 function ColorRedStripByte(const Color: TVector3Byte): TVector3Byte;
313 begin
314 Result.X := Color.X;
315 Result.Y := 0;
316 Result.Z := 0;
317 end;
318
319 function ColorGreenStripByte(const Color: TVector3Byte): TVector3Byte;
320 begin
321 Result.X := 0;
322 Result.Y := Color.Y;
323 Result.Z := 0;
324 end;
325
326 function ColorBlueStripByte(const Color: TVector3Byte): TVector3Byte;
327 begin
328 Result.X := 0;
329 Result.Y := 0;
330 Result.Z := Color.Z;
331 end;
332
333 { HSV stuff ------------------------------------------------------------------ }
334
335 function RgbToHsv(const Value: TVector3): TVector3;
336 var
337 Chroma, V: Single;
338 begin
339 V := Value.Max;
340 Result.Z := V;
341 Chroma := V - Value.Min;
342
343 { Chroma and V are now in the same range as RGB components.
344 Which means 0..1 right now, so already Ok. }
345
346 if Chroma = 0 then
347 begin
348 Result.X := 0;
349 Result.Y := 0;
350 end else
351 begin
352 { calculate hue }
353 if V = Value.X then
354 begin
355 Result.X := (Value.Y - Value.Z) / Chroma;
356 if Result.X < 0 then
357 Result.X := Result.X + 6.0;
358 end else
359 if V = Value.Y then
360 Result.X := (Value.Z - Value.X) / Chroma + 2.0 else
361 Result.X := (Value.X - Value.Y) / Chroma + 4.0;
362
363 { calculate saturation }
364 Result.Y := Chroma / V;
365 end;
366 end;
367
368 function HsvToRgb(const Value: TVector3): TCastleColorRGB;
369 var
370 F, P, Q, T, V: Single;
371 begin
372 F := Frac(Value.X);
373
374 { RGB component candidates }
375 V := Value.Z;
376 P := V * (1 - Value.Y);
377 Q := V * (1 - (Value.Y * F));
378 T := V * (1 - (Value.Y * (1 - F)));
379
380 case Floor(Value.X) of
381 0, 6:begin Result.X := V; Result.Y := T; Result.Z := P; end;
382 1: begin Result.X := Q; Result.Y := V; Result.Z := P; end;
383 2: begin Result.X := P; Result.Y := V; Result.Z := T; end;
384 3: begin Result.X := P; Result.Y := Q; Result.Z := V; end;
385 4: begin Result.X := T; Result.Y := P; Result.Z := V; end;
386 else begin Result.X := V; Result.Y := P; Result.Z := Q; end;
387 end;
388 end;
389
390 function HsvToRgba(const Value: TVector3; const Alpha: Single): TCastleColor;
391 begin
392 Result := Vector4(HsvToRgb(Value), Alpha);
393 end;
394
395 function RgbToHsv(const Value: TVector3Byte): TVector3;
396 var
397 ValueFloat: TVector3;
398 begin
399 ValueFloat.X := Value.X / 255.0;
400 ValueFloat.Y := Value.Y / 255.0;
401 ValueFloat.Z := Value.Z / 255.0;
402 Result := RgbToHsv(ValueFloat);
403 end;
404
405 function HsvToRgbByte(const Value: TVector3): TVector3Byte;
406 var
407 ResultFloat: TVector3;
408 begin
409 ResultFloat := HsvToRgb(Value);
410 Result.X := RoundClamp255(ResultFloat.X * 255.0);
411 Result.Y := RoundClamp255(ResultFloat.Y * 255.0);
412 Result.Z := RoundClamp255(ResultFloat.Z * 255.0);
413 end;
414
415 function LerpRgbInHsv(const A: Single; const V1, V2: TVector3): TVector3;
416 var
417 H1, H2, HOut: TVector3;
418 HueDiff: Single;
419 begin
420 H1 := RgbToHsv(V1);
421 H2 := RgbToHsv(V2);
422
423 { if one of the colors has saturation = 0, then resulting hue is copied
424 from the other color, not interpolated. Otherwise,
425 colors with saturation = 0 get hue = 0, which causes
426 interpolation from something colorful (like blue) to black go through
427 weird hue. }
428 if H1.Y = 0 then
429 HOut.X := H2.X else
430 if H2.Y = 0 then
431 HOut.X := H1.X else
432 begin
433 HueDiff := H2.X - H1.X;
434 if HueDiff > 3 then
435 begin
436 { from hue 1 to hue 2 go down through 0.0 }
437 H2.X := H2.X - 6;
438 HOut.X := H1.X + A * (H2.X - H1.X);
439 if HOut.X < 0 then HOut.X := HOut.X + 6;
440 end else
441 if HueDiff < -3 then
442 begin
443 { from hue 1 to hue 2 go up through 6.0 }
444 H2.X := H2.X + 6;
445 HOut.X := H1.X + A * (H2.X - H1.X);
446 if HOut.X > 6 then HOut.X := HOut.X - 6;
447 end else
448 { normal lerp when HueDiff inside [-3, 3] }
449 HOut.X := H1.X + A * (H2.X - H1.X);
450 end;
451
452 { lerp on saturation and value is normal }
453 HOut.Y := H1.Y + A * (H2.Y - H1.Y);
454 HOut.Z := H1.Z + A * (H2.Z - H1.Z);
455
456 Result := HsvToRgb(HOut);
457 end;
458
459 function ColorToHex(const V: TCastleColor): String;
460 var
461 A: Byte;
462 begin
463 Result := IntToHex(RoundClamp255(V.X * 255), 2) +
464 IntToHex(RoundClamp255(V.Y * 255), 2) +
465 IntToHex(RoundClamp255(V.Z * 255), 2);
466 A := RoundClamp255(V.W * 255);
467 if A <> 255 then
468 Result := Result + IntToHex(A, 2);
469 end;
470
471 function ColorRGBToHex(const V: TCastleColorRGB): String;
472 begin
473 Result := IntToHex(RoundClamp255(V.X * 255), 2) +
474 IntToHex(RoundClamp255(V.Y * 255), 2) +
475 IntToHex(RoundClamp255(V.Z * 255), 2);
476 end;
477
478 function HexToColor(const S: String): TCastleColor;
479 begin
480 if Length(S) = 8 then
481 Result := Vector4(
482 StrHexToInt(Copy(S, 1, 2)) / 255,
483 StrHexToInt(Copy(S, 3, 2)) / 255,
484 StrHexToInt(Copy(S, 5, 2)) / 255,
485 StrHexToInt(Copy(S, 7, 2)) / 255) else
486 if Length(S) = 6 then
487 Result := Vector4(
488 StrHexToInt(Copy(S, 1, 2)) / 255,
489 StrHexToInt(Copy(S, 3, 2)) / 255,
490 StrHexToInt(Copy(S, 5, 2)) / 255,
491 1.0) else
492 raise EConvertError.CreateFmt('Invalid color hex string: "%s"', [S]);
493 end;
494
495 function HexToColorRGB(const S: String): TCastleColorRGB;
496 begin
497 if (Length(S) = 8) or
498 (Length(S) = 6) then
499 Result := Vector3(
500 StrHexToInt(Copy(S, 1, 2)) / 255,
501 StrHexToInt(Copy(S, 3, 2)) / 255,
502 StrHexToInt(Copy(S, 5, 2)) / 255) else
503 raise EConvertError.CreateFmt('Invalid color hex string: "%s"', [S]);
504 end;
505
506 function ColorOpacity(const Color: TCastleColor; const Opacity: Single): TCastleColor;
507 begin
508 Result := Color;
509 Result.W := Opacity;
510 end;
511
512 function FadeDarkColor(const Color: TCastleColor;
513 const FadeIntensity: Single): TCastleColor;
514 const
515 FullWhiteEnd = 0.9;
516 FullBlack = 0.3;
517 { We assume that MinScale is small enough that difference between
518 "Color * MinScale * screen color" and
519 "MinScale * screen color" is not noticeable. }
520 MinScale = 0.1;
521 begin
522 if FadeIntensity > 0 then
523 begin
524 { for FadeIntensity in 1...FullWhiteEnd (going down):
525 screen color := Color * original screen color }
526 if FadeIntensity > FullWhiteEnd then
527 Result := Color else
528 { for FadeIntensity in FullWhiteEnd...FullBlack (going down):
529 final screen color changes:
530 - from screen color := Color * original screen color
531 - to screen color := Color * MinScale * original screen color }
532 if FadeIntensity > FullBlack then
533 Result := Color * MapRange(FadeIntensity, FullWhiteEnd, FullBlack, 1, MinScale) else
534 { for FadeIntensity in FullBlack...0 (going down):
535 final screen color changes:
536 - from screen color := MinScale * original screen color
537 - to screen color := original screen color }
538 Result := White * MapRange(FadeIntensity, FullBlack, 0, MinScale, 1);
539
540 Result.W := 1.0; { alpha always 1.0 in this case }
541 end else
542 Result := TVector4.Zero;
543 end;
544
545 function FadeColor(const Color: TCastleColor;
546 const FadeIntensity: Single): TCastleColor;
547 const
548 FullTime = 0.9;
549 var
550 Intensity: Single;
551 begin
552 if FadeIntensity > 0 then
553 begin
554 if FadeIntensity < FullTime then
555 Intensity := MapRange(FadeIntensity, 0, FullTime, 0, 1) else
556 Intensity := MapRange(FadeIntensity, FullTime, 1, 1, 0);
557 Result := Color;
558 Result.W := Intensity;
559 end else
560 Result := TVector4.Zero;
561 end;
562
563 end.