Debian Codegraph @

tools/castle-editor/components/mbColorLib/RGBHSVUtils.pas

raw
1 unit RGBHSVUtils;
2
3 {$IFDEF FPC}
4 {$MODE DELPHI}
5 (* Hide unactionable FPC message
6 (6058) Call to subroutine "function MaxValue(const data:{Open} Array Of Double):Double;" marked as inline is not inlined
7 *)
8 {$notes off}
9 {$ENDIF}
10
11 interface
12
13 uses
14 LCLIntf, LCLType, SysUtils, Classes, Graphics, Math,
15 Scanlines;
16
17 { The next four procedures assume H, S, V to be in the range 0..1 }
18 //procedure ColorToHSV(c: TColor; out H, S, V: Double);
19 //procedure RGBtoHSV(R, G, B: Integer; out H, S, V: Double);
20
21 //function HSVtoColor(H, S, V: Double): TColor;
22 //procedure HSVtoRGB(H, S, V: Double; out R, G, B: Integer);
23
24 { These next procedure assume H to be in the range 0..360
25 and S, V in the range 0..255 }
26 procedure RGBtoHSVRange(R, G, B: integer; out H, S, V: integer);
27 procedure HSVtoRGBRange(H, S, V: Integer; out R, G, B: Integer);
28 function HSVRangeToColor(H, S, V: Integer): TColor;
29 function HSVtoRGBTriple(H, S, V: integer): TRGBTriple;
30 function HSVtoRGBQuad(H, S, V: integer): TRGBQuad;
31
32 function GetHValue(Color: TColor): integer;
33 function GetVValue(Color: TColor): integer;
34 function GetSValue(Color: TColor): integer;
35
36
37 implementation
38
39 { Assumes R, G, B to be in range 0..255. Calculates H, S, V in range 0..1
40 From: http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c }
41 procedure RGBToHSV(R, G, B: Integer; out H, S, V: Double);
42 var
43 rr, gg, bb: Double;
44 cmax, cmin, delta: Double;
45 begin
46 rr := R / 255;
47 gg := G / 255;
48 bb := B / 255;
49 cmax := MaxValue([rr, gg, bb]);
50 cmin := MinValue([rr, gg, bb]);
51 delta := cmax - cmin;
52 if delta = 0 then
53 begin
54 H := 0;
55 S := 0;
56 end else
57 begin
58 if cmax = rr then
59 H := (gg - bb) / delta + IfThen(gg < bb, 6, 0)
60 else if cmax = gg then
61 H := (bb - rr) / delta + 2
62 else if (cmax = bb) then
63 H := (rr -gg) / delta + 4;
64 H := H / 6;
65 S := delta / cmax;
66 end;
67 V := cmax;
68 end;
69
70 procedure ColorToHSV(c: TColor; out H, S, V: Double);
71 begin
72 RGBToHSV(GetRValue(c), GetGValue(c), GetBValue(c), H, S, V);
73 end;
74
75 { Assumes H, S, V in the range 0..1 and calculates the R, G, B values which are
76 returned to be in the range 0..255.
77 From: http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c
78 }
79 procedure HSVtoRGB(H, S, V: Double; out R, G, B: Integer);
80 var
81 i: Integer;
82 f: Double;
83 p, q, t: Double;
84
85 procedure MakeRgb(rr, gg, bb: Double);
86 begin
87 R := Round(rr * 255);
88 G := Round(gg * 255);
89 B := Round(bb * 255);
90 end;
91
92 begin
93 i := floor(H * 6);
94 f := H * 6 - i;
95 p := V * (1 - S);
96 q := V * (1 - f*S);
97 t := V * (1 - (1 - f) * S);
98 case i mod 6 of
99 0: MakeRGB(V, t, p);
100 1: MakeRGB(q, V, p);
101 2: MakeRGB(p, V, t);
102 3: MakeRGB(p, q, V);
103 4: MakeRGB(t, p, V);
104 5: MakeRGB(V, p, q);
105 else MakeRGB(0, 0, 0);
106 end;
107 end;
108
109 function HSVToColor(H, S, V: Double): TColor;
110 var
111 r, g, b: Integer;
112 begin
113 HSVtoRGB(H, S, V, r, g, b);
114 Result := RgbToColor(r, g, b);
115 end;
116
117
118 //------------------------------------------------------------------------------
119
120 procedure RGBToHSVRange(R, G, B: integer; out H, S, V: integer);
121 var
122 Delta, Min, H1, S1: double;
123 begin
124 Min := MinIntValue([R, G, B]);
125 V := MaxIntValue([R, G, B]);
126 Delta := V - Min;
127 if V = 0.0 then S1 := 0 else S1 := Delta / V;
128 if S1 = 0.0 then
129 H1 := 0
130 else
131 begin
132 if R = V then
133 H1 := 60.0 * (G - B) / Delta
134 else if G = V then
135 H1 := 120.0 + 60.0 * (B - R) / Delta
136 else if B = V then
137 H1 := 240.0 + 60.0 * (R - G) / Delta;
138 if H1 < 0.0 then H1 := H1 + 360.0;
139 end;
140 h := round(h1);
141 s := round(s1*255);
142 end;
143
144 procedure HSVtoRGBRange(H, S, V: Integer; out R, G, B: Integer);
145 var
146 t: TRGBTriple;
147 begin
148 t := HSVtoRGBTriple(H, S, V);
149 R := t.rgbtRed;
150 G := t.rgbtGreen;
151 B := t.rgbtBlue;
152 end;
153
154 function HSVtoRGBTriple(H, S, V: integer): TRGBTriple;
155 const
156 divisor: integer = 255*60;
157 var
158 f, hTemp, p, q, t, VS: integer;
159 begin
160 if H > 360 then H := H - 360;
161 if H < 0 then H := H + 360;
162 if s = 0 then
163 Result := RGBtoRGBTriple(V, V, V)
164 else
165 begin
166 if H = 360 then hTemp := 0 else hTemp := H;
167 f := hTemp mod 60;
168 hTemp := hTemp div 60;
169 VS := V*S;
170 p := V - VS div 255;
171 q := V - (VS*f) div divisor;
172 t := V - (VS*(60 - f)) div divisor;
173 case hTemp of
174 0: Result := RGBtoRGBTriple(V, t, p);
175 1: Result := RGBtoRGBTriple(q, V, p);
176 2: Result := RGBtoRGBTriple(p, V, t);
177 3: Result := RGBtoRGBTriple(p, q, V);
178 4: Result := RGBtoRGBTriple(t, p, V);
179 5: Result := RGBtoRGBTriple(V, p, q);
180 else Result := RGBtoRGBTriple(0,0,0)
181 end;
182 end;
183 end;
184
185 function HSVtoRGBQuad(H, S, V: integer): TRGBQuad;
186 const
187 divisor: integer = 255*60;
188 var
189 f, hTemp, p, q, t, VS: integer;
190 begin
191 if H > 360 then H := H - 360;
192 if H < 0 then H := H + 360;
193 if s = 0 then
194 Result := RGBtoRGBQuad(V, V, V)
195 else
196 begin
197 if H = 360 then hTemp := 0 else hTemp := H;
198 f := hTemp mod 60;
199 hTemp := hTemp div 60;
200 VS := V*S;
201 p := V - VS div 255;
202 q := V - (VS*f) div divisor;
203 t := V - (VS*(60 - f)) div divisor;
204 case hTemp of
205 0: Result := RGBtoRGBQuad(V, t, p);
206 1: Result := RGBtoRGBQuad(q, V, p);
207 2: Result := RGBtoRGBQuad(p, V, t);
208 3: Result := RGBtoRGBQuad(p, q, V);
209 4: Result := RGBtoRGBQuad(t, p, V);
210 5: Result := RGBtoRGBQuad(V, p, q);
211 else Result := RGBtoRGBQuad(0,0,0)
212 end;
213 end;
214 end;
215
216 function HSVRangetoColor(H, S, V: integer): TColor;
217 begin
218 Result := RGBTripleToColor(HSVtoRGBTriple(H, S, V));
219 end;
220
221 //------------------------------------------------------------------------------
222
223 function GetHValue(Color: TColor): integer;
224 var
225 s, v: integer;
226 begin
227 RGBToHSVRange(GetRValue(Color), GetGValue(Color), GetBValue(Color), Result, s, v);
228 end;
229
230 function GetSValue(Color: TColor): integer;
231 var
232 h, v: integer;
233 begin
234 RGBToHSVRange(GetRValue(Color), GetGValue(Color), GetBValue(Color), h, Result, v);
235 end;
236
237 function GetVValue(Color: TColor): integer;
238 var
239 h, s: integer;
240 begin
241 RGBToHSVRange(GetRValue(Color), GetGValue(Color), GetBValue(Color), h, s, Result);
242 end;
243
244 end.