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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Color.Rgb;
interface
uses
Boolean,
Comparator,
FloatingPoint._64,
Integer._32,
Integer._32.Positive,
Integer._8.Positive,
Mathematics.Sign,
Text;
type
PRgbColor = ^TRgbColor;
TRgbColor = packed record
private
function AsComparisonValue(): TPositiveInteger32;
public
Red: TPositiveInteger8;
Green: TPositiveInteger8;
Blue: TPositiveInteger8;
Alpha: TPositiveInteger8;
class function New(Red, Green, Blue: TPositiveInteger8; Alpha: TPositiveInteger8 = 255): TRgbColor; overload; static;
class function New(const Text: TText): TRgbColor; overload; static;
class function NewGray(Value: TPositiveInteger8): TRgbColor; static;
class operator Equal(const Left: TRgbColor; const Right: TRgbColor): TBoolean;
class operator NotEqual(const Left: TRgbColor; const Right: TRgbColor): TBoolean;
class operator GreaterThan(const Left: TRgbColor; const Right: TRgbColor): TBoolean;
function Darker(Factor: TFloatingPoint64): TRgbColor;
function Lighter(Factor: TFloatingPoint64): TRgbColor;
function Blend(const Background: TRgbColor): TRgbColor;
function AsInteger32: TInteger32;
function AsText(): TText;
end;
TRgbColorComparator = class(TComparator<TRgbColor>)
protected
function GetComparisonValue(const Value: TRgbColor): TPositiveInteger32;
public
function Compare(const Left: TRgbColor; const Right: TRgbColor): TSign; override;
end;
implementation
uses
Mathematics,
Numeral.System.Hexadecimal;
{ TRgbColor }
function TRgbColor.AsComparisonValue(): TPositiveInteger32;
begin
Result := Red or (Green shl 8) or (Blue shl 16) or (Alpha shl 24);
end;
function TRgbColor.AsInteger32(): TInteger32;
begin
Result := Red or (Green shl 8) or (Blue shl 16) {or (Alpha shl 24)};
end;
function TRgbColor.AsText(): TText;
begin
Result := ByteToHexadecimal(Red) + ByteToHexadecimal(Green) + ByteToHexadecimal(Blue);
end;
function TRgbColor.Blend(const Background: TRgbColor): TRgbColor;
var
R1, G1, B1: TFloatingPoint64;
R2, G2, B2: TFloatingPoint64;
R3, G3, B3: TFloatingPoint64;
begin
R1 := Red / 255;
G1 := Green / 255;
B1 := Blue / 255;
R2 := Background.Red / 255;
G2 := Background.Green / 255;
B2 := Background.Blue / 255;
if R1 + G1 + B1 > 1.5 then
begin
R3 := R1 * R2;
G3 := G1 * G2;
B3 := B1 * B2;
end
else
begin
R3 := 1 - ((1 - R1) * (1 - R2));
G3 := 1 - ((1 - G1) * (1 - G2));
B3 := 1 - ((1 - B1) * (1 - B2));
end;
Result.Red := RoundToNearest(R3 * 255);
Result.Green := RoundToNearest(G3 * 255);
Result.Blue := RoundToNearest(B3 * 255);
Result.Alpha := (Alpha + Background.Alpha) div 2;
end;
function TRgbColor.Darker(Factor: TFloatingPoint64): TRgbColor;
begin
Factor := 1 / (Factor + 1);
Result.Red := Minimum(255, RoundToNearest(Red * Factor));
Result.Green := Minimum(255, RoundToNearest(Green * Factor));
Result.Blue := Minimum(255, RoundToNearest(Blue * Factor));
Result.Alpha := 255;
end;
class operator TRgbColor.Equal(const Left: TRgbColor; const Right: TRgbColor): TBoolean;
begin
Result :=
(Left.Blue = Right.Blue)
and (Left.Green = Right.Green)
and (Left.Red = Right.Red)
and (Left.Alpha = Right.Alpha);
end;
class operator TRgbColor.GreaterThan(const Left: TRgbColor; const Right: TRgbColor): TBoolean;
begin
Result := Left.AsComparisonValue() > Right.AsComparisonValue();
end;
function TRgbColor.Lighter(Factor: TFloatingPoint64): TRgbColor;
begin
Factor := Factor + 1;
Result.Red := Minimum(255, RoundToNearest(Red * Factor));
Result.Green := Minimum(255, RoundToNearest(Green * Factor));
Result.Blue := Minimum(255, RoundToNearest(Blue * Factor));
Result.Alpha := 255;
end;
class function TRgbColor.New(Red, Green, Blue: TPositiveInteger8; Alpha: TPositiveInteger8 = 255): TRgbColor;
begin
Result.Red := Red;
Result.Green := Green;
Result.Blue := Blue;
Result.Alpha := Alpha;
end;
class function TRgbColor.New(const Text: TText): TRgbColor;
var
IntegerValue: TPositiveInteger32;
begin
IntegerValue := HexadecimalToPositiveInteger32(Text);
Result := TRgbColor.New(
(IntegerValue and $FF0000) shr 16,
(IntegerValue and $FF00) shr 8,
IntegerValue and $FF
);
end;
class function TRgbColor.NewGray(Value: TPositiveInteger8): TRgbColor;
begin
Result.Red := Value;
Result.Green := Value;
Result.Blue := Value;
Result.Alpha := $FF;
end;
class operator TRgbColor.NotEqual(const Left: TRgbColor; const Right: TRgbColor): TBoolean;
begin
Result :=
(Left.Blue <> Right.Blue)
or (Left.Green <> Right.Green)
or (Left.Red <> Right.Red)
or (Left.Alpha <> Right.Alpha);
end;
{ TRgbColorComparator }
function TRgbColorComparator.Compare(const Left: TRgbColor; const Right: TRgbColor): TSign;
var
LeftValue: TPositiveInteger32;
RightValue: TPositiveInteger32;
begin
LeftValue := GetComparisonValue(Left);
RightValue := GetComparisonValue(Right);
if LeftValue < RightValue then
Result := sNegative
else if LeftValue > RightValue then
Result := sPositive
else
Result := sZero;
end;
function TRgbColorComparator.GetComparisonValue(const Value: TRgbColor): TPositiveInteger32;
begin
Result := Value.Red or (Value.Green shl 8) or (Value.Blue shl 16) or (Value.Alpha shl 24);
end;
end.
|