| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleColors" -*- |
| 2 |
{ |
| 3 |
Copyright 2011-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Test CastleColors unit. } |
| 18 |
unit TestCastleColors; |
| 19 |
|
| 20 |
{ $define SPEED_TESTS} |
| 21 |
|
| 22 |
interface |
| 23 |
|
| 24 |
uses |
| 25 |
Classes, SysUtils, |
| 26 |
CastleTester, CastleVectors, CastleColors; |
| 27 |
|
| 28 |
type |
| 29 |
TTestCastleColors = class(TCastleTestCase) |
| 30 |
published |
| 31 |
procedure TestHsv; |
| 32 |
procedure TestHsvRgba; |
| 33 |
procedure TestLerpInHsv; |
| 34 |
procedure TestColorPickerCodeRgb; |
| 35 |
procedure TestColorPickerCodeRgba; |
| 36 |
procedure TestColorHsvAndBack; |
| 37 |
end; |
| 38 |
|
| 39 |
implementation |
| 40 |
|
| 41 |
uses CastleUtils, CastleStringUtils, CastleTimeUtils, CastleScene, CastleControls; |
| 42 |
|
| 43 |
procedure TTestCastleColors.TestHsv; |
| 44 |
var |
| 45 |
RGB: TVector3Byte; |
| 46 |
HSV: TVector3; |
| 47 |
R, G, B: Integer; |
| 48 |
{$ifdef SPEED_TESTS} |
| 49 |
Operations: Integer; |
| 50 |
Time: Double; |
| 51 |
{$endif} |
| 52 |
begin |
| 53 |
{$ifdef SPEED_TESTS} |
| 54 |
ProcessTimerBegin; |
| 55 |
{$endif} |
| 56 |
|
| 57 |
for R := 0 to 255 div 5 do |
| 58 |
for G := 0 to 255 div 5 do |
| 59 |
for B := 0 to 255 div 5 do |
| 60 |
begin |
| 61 |
RGB.X := R * 5; |
| 62 |
RGB.Y := G * 5; |
| 63 |
RGB.Z := B * 5; |
| 64 |
HSV := RgbToHsv(RGB); |
| 65 |
{ test trip to HSV and back returns the same } |
| 66 |
AssertTrue(TVector3Byte.Equals(RGB, HsvToRgbByte(HSV))); |
| 67 |
{ test HSV components are in appropriate ranges } |
| 68 |
AssertTrue(Between(HSV.X, 0, 6)); |
| 69 |
AssertTrue(Between(HSV.Y, 0, 1)); |
| 70 |
AssertTrue(Between(HSV.Z, 0, 1)); |
| 71 |
end; |
| 72 |
|
| 73 |
{$ifdef SPEED_TESTS} |
| 74 |
Operations := 255 div 5 + 1; |
| 75 |
Operations := Sqr(Operations) * Operations; |
| 76 |
Time := ProcessTimerEnd; |
| 77 |
|
| 78 |
{ With FPC 2.4.4, speed is quite amazing: |
| 79 |
with -dRELEASE: |
| 80 |
HSV trip (RGB and back): average time is 0.00 secs per 1 operation (total 0.00 secs for 140608 operations) |
| 81 |
with -dDEBUG: |
| 82 |
HSV trip (RGB and back): average time is 0.00 secs per 1 operation (total 0.03 secs for 140608 operations) |
| 83 |
} |
| 84 |
|
| 85 |
Writeln(Format('HSV trip (RGB and back): average time is %f secs per 1 operation (total %f secs for %d operations)', [Time / Operations, Time, Operations])); |
| 86 |
{$endif} |
| 87 |
end; |
| 88 |
|
| 89 |
procedure TTestCastleColors.TestLerpInHsv; |
| 90 |
const |
| 91 |
PureRed : TVector3 = (X: 1; Y: 0; Z: 0); |
| 92 |
PureBlue: TVector3 = (X: 0; Y: 0; Z: 1); |
| 93 |
var |
| 94 |
I: Integer; |
| 95 |
C: TVector3; |
| 96 |
H: Single; |
| 97 |
begin |
| 98 |
for I := 1 to 10 do |
| 99 |
begin |
| 100 |
C := LerpRgbInHsv(I / 10, BlackRGB, PureBlue); |
| 101 |
{ interpolating from pure black to blue, |
| 102 |
all colors along the way should keep hue = blue } |
| 103 |
H := RgbToHsv(C).X; |
| 104 |
AssertTrue(RgbToHsv(PureBlue).X = H); |
| 105 |
// Writeln(C.ToString, ' ', VectorToNiceStr(RgbToHsv(C))); |
| 106 |
end; |
| 107 |
|
| 108 |
for I := 0 to 10 do |
| 109 |
begin |
| 110 |
C := LerpRgbInHsv(I / 10, PureRed, PureBlue); |
| 111 |
{ interpolate from hue 0 to hue 4 --- go down through 0 } |
| 112 |
H := RgbToHsv(C).X; |
| 113 |
AssertTrue((H = 0.0) or Between(H, 4, 6)); |
| 114 |
// Writeln(C.ToString, ' ', VectorToNiceStr(RgbToHsv(C))); |
| 115 |
end; |
| 116 |
|
| 117 |
for I := 0 to 10 do |
| 118 |
begin |
| 119 |
C := LerpRgbInHsv(I / 10, PureBlue, PureRed); |
| 120 |
{ interpolate from hue 4 to hue 0 --- go up through 6 } |
| 121 |
H := RgbToHsv(C).X; |
| 122 |
AssertTrue((H = 0.0) or Between(H, 4, 6)); |
| 123 |
// Writeln(C.ToString, ' ', VectorToNiceStr(RgbToHsv(C))); |
| 124 |
end; |
| 125 |
|
| 126 |
AssertVectorEquals(LerpRgbInHsv(0, PureBlue, PureRed), PureBlue); |
| 127 |
AssertVectorEquals(LerpRgbInHsv(1, PureBlue, PureRed), PureRed); |
| 128 |
AssertVectorEquals(LerpRgbInHsv(0, PureRed, PureBlue), PureRed); |
| 129 |
AssertVectorEquals(LerpRgbInHsv(1, PureRed, PureBlue), PureBlue); |
| 130 |
AssertVectorEquals(LerpRgbInHsv(0, BlackRGB, PureRed), BlackRGB); |
| 131 |
AssertVectorEquals(LerpRgbInHsv(1, BlackRGB, PureRed), PureRed); |
| 132 |
end; |
| 133 |
|
| 134 |
procedure TTestCastleColors.TestHsvRgba; |
| 135 |
var |
| 136 |
RGBA, NewRGBA: TCastleColor; |
| 137 |
Hsv: TVector3; |
| 138 |
begin |
| 139 |
RGBA := Vector4(0.1, 0.5, 0.7, 0.9); |
| 140 |
Hsv := RgbToHsv(RGBA.XYZ); |
| 141 |
NewRGBA := HsvToRgba(Hsv, 0.9); |
| 142 |
AssertVectorEquals(RGBA, NewRGBA); |
| 143 |
end; |
| 144 |
|
| 145 |
procedure TTestCastleColors.TestColorPickerCodeRgb; |
| 146 |
var |
| 147 |
MyControl: TCastleBackground; |
| 148 |
// Define a constant with hard-coded color value like this: |
| 149 |
const |
| 150 |
MyColor: TCastleColorRGB = (X: 0.125; Y: 0.125; Z: 0.373); |
| 151 |
begin |
| 152 |
MyControl := TCastleBackground.Create(nil); |
| 153 |
try |
| 154 |
// Set colors from a hard-coded value like this: |
| 155 |
MyControl.SkyTopColor := MyColor; |
| 156 |
MyControl.SkyTopColor := Vector3(0.125, 0.125, 0.373); |
| 157 |
MyControl.SkyTopColor := HsvToRgb(Vector3(4.000, 0.667, 0.373)); |
| 158 |
MyControl.SkyTopColor := HexToColorRGB('20205F'); |
| 159 |
finally FreeAndNil(MyControl) end; |
| 160 |
end; |
| 161 |
|
| 162 |
procedure TTestCastleColors.TestColorPickerCodeRgba; |
| 163 |
var |
| 164 |
MyControl: TCastleImageControl; |
| 165 |
// Define a constant with hard-coded color value like this: |
| 166 |
const |
| 167 |
MyColor: TCastleColor = (X: 0.318; Y: 0.286; Z: 0.000; W: 0.598); |
| 168 |
begin |
| 169 |
MyControl := TCastleImageControl.Create(nil); |
| 170 |
try |
| 171 |
// Set colors from a hard-coded value like this: |
| 172 |
MyControl.Color := MyColor; |
| 173 |
MyControl.Color := Vector4(0.318, 0.286, 0.000, 0.598); |
| 174 |
MyControl.Color := HsvToRgba(Vector3(0.901, 1.000, 0.318), 0.598); |
| 175 |
MyControl.Color := HexToColor('51490099'); |
| 176 |
finally FreeAndNil(MyControl) end; |
| 177 |
end; |
| 178 |
|
| 179 |
procedure TTestCastleColors.TestColorHsvAndBack; |
| 180 |
var |
| 181 |
Col: TCastleColorRGB; |
| 182 |
ColHsv: TVector3; |
| 183 |
begin |
| 184 |
Col := RedRGB; |
| 185 |
ColHsv := RgbToHsv(Col); |
| 186 |
ColHsv.Z := 0.5; // half brightness |
| 187 |
Col := HsvToRgb(ColHsv); |
| 188 |
//Writeln('Red color with half brightness ', Col.ToString); |
| 189 |
AssertVectorEquals(Vector3(0.5, 0, 0), Col); |
| 190 |
end; |
| 191 |
|
| 192 |
initialization |
| 193 |
RegisterTest(TTestCastleColors); |
| 194 |
end. |