Debian Codegraph @

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

raw
1 unit HColorPicker;
2
3 {$IFDEF FPC}
4 {$MODE DELPHI}
5 {$ENDIF}
6
7 interface
8
9 uses
10 LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics, Forms,
11 HTMLColors, mbColorConv, mbTrackBarPicker;
12
13 type
14 THColorPicker = class(TmbHSLVTrackBarPicker)
15 private
16 function ArrowPosFromHue(h: Double): integer;
17 function HueFromArrowPos(p: integer): Double;
18 protected
19 function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;
20 MousePos: TPoint): Boolean; override;
21 procedure Execute(tbaAction: integer); override;
22 function GetArrowPos: integer; override;
23 function GetGradientColor(AValue: Integer): TColor; override;
24 function GetSelectedValue: integer; override;
25 procedure SetMaxHue(H: Integer); override;
26 procedure SetRelHue(H: Double); override;
27 procedure SetSelectedColor(c: TColor); override;
28 public
29 constructor Create(AOwner: TComponent); override;
30 published
31 property Layout default lyHorizontal;
32 property Hue default 0;
33 property Saturation default 255;
34 property Luminance default 127;
35 property Value default 255;
36 property SelectedColor default clRed;
37 property HintFormat;
38 end;
39
40
41 implementation
42
43 uses
44 mbUtils;
45
46 {THColorPicker}
47
48 constructor THColorPicker.Create(AOwner: TComponent);
49 begin
50 inherited;
51 FGradientWidth := FMaxHue;
52 FGradientHeight := 1;
53 FSat := 1.0;
54 FVal := 1.0;
55 FLum := 0.5;
56 Hue := 0;
57 HintFormat := 'Hue: %value (selected)';
58 end;
59
60 function THColorPicker.ArrowPosFromHue(H: Double): integer;
61 var
62 a: integer;
63 begin
64 if Layout = lyHorizontal then
65 begin
66 a := Round((Width - 12) * H);
67 if a > Width - FLimit then a := Width - FLimit;
68 end
69 else
70 begin
71 a := Round((Height - 12) * H);
72 if a > Height - FLimit then a := Height - FLimit;
73 end;
74 if a < 0 then a := 0;
75 Result := a;
76 end;
77
78 function THColorPicker.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;
79 MousePos: TPoint): Boolean;
80 begin
81 if Layout = lyVertical then WheelDelta := -WheelDelta;
82 WheelDelta := WheelDelta * 3; // use larger steps
83 Result := inherited;
84 end;
85
86 procedure THColorPicker.Execute(tbaAction: integer);
87 var
88 dHue: Double;
89 begin
90 if FMaxHue = 0 then dHue := 0 else dHue := Increment / FMaxHue;
91 case tbaAction of
92 TBA_Resize:
93 SetRelHue(FHue); // wp: Is this working?
94 TBA_MouseMove:
95 SetRelHue(HueFromArrowPos(FArrowPos));
96 TBA_MouseDown:
97 SetRelHue(HueFromArrowPos(FArrowPos));
98 TBA_MouseUp:
99 SetRelHue(HueFromArrowPos(FArrowPos));
100 TBA_WheelUp:
101 SetRelHue(FHue + dHue);
102 TBA_WheelDown:
103 SetRelHue(FHue - dHue);
104 TBA_VKLeft:
105 SetRelHue(FHue - dHue);
106 TBA_VKCtrlLeft:
107 SetRelHue(0);
108 TBA_VKRight:
109 SetRelHue(FHue + dHue);
110 TBA_VKCtrlRight:
111 SetRelHue(1 - dHue); // go one step below 360, or the hue will flip back to 0
112 TBA_VKUp:
113 SetRelHue(FHue - dHue);
114 TBA_VKCtrlUp:
115 SetRelHue(0);
116 TBA_VKDown:
117 SetRelHue(FHue + dHue);
118 TBA_VKCtrlDown:
119 SetRelHue(1 - dHue);
120 else
121 inherited;
122 end;
123 end;
124
125 function THColorPicker.GetArrowPos: integer;
126 begin
127 if FMaxHue = 0 then
128 Result := inherited GetArrowPos
129 else
130 Result := ArrowPosFromHue(FHue);
131 end;
132
133 function THColorPicker.GetGradientColor(AValue: Integer): TColor;
134 var
135 h: Double;
136 begin
137 if Layout = lyVertical then AValue := FMaxHue - 1 - AValue;
138 // Width is FMaxHue --> last index is FMaxHue - 1
139 h := AValue / FMaxHue;
140 Result := HSLVtoColor(h, FSat, FLum, FVal);
141 end;
142
143 function THColorPicker.GetSelectedValue: integer;
144 begin
145 Result := Hue;
146 end;
147
148 function THColorPicker.HueFromArrowPos(p: integer): Double;
149 var
150 h: Double;
151 begin
152 case Layout of
153 lyHorizontal : h := p / (Width - 12);
154 lyVertical : h := p / (Height - 12)
155 end;
156 Clamp(h, 0, 1.0 - 1/FMaxHue);
157 Result := h;
158 end;
159
160 procedure THColorPicker.SetMaxHue(h: Integer);
161 begin
162 if h = FMaxHue then
163 exit;
164 FMaxHue := h;
165 FGradientWidth := FMaxHue; // we don't want to access H=360, i.e. don't use FMaxHue+1
166 CreateGradient;
167 Invalidate;
168 end;
169
170 procedure THColorPicker.SetRelHue(H: Double);
171 begin
172 if FMaxHue = 0 then
173 exit;
174 Clamp(H, 0, 1 - 1/FMaxHue); // don't go up to 360 because this will flip back to the start
175 if (FHue <> H) then
176 begin
177 FHue := H;
178 FArrowPos := ArrowPosFromHue(H);
179 Invalidate;
180 DoChange;
181 end;
182 end;
183
184 procedure THColorPicker.SetSelectedColor(c: TColor);
185 var
186 H: Double = 0;
187 S: Double = 0;
188 L: Double = 0;
189 V: Double = 0;
190 needNewGradient: Boolean;
191 begin
192 if WebSafe then
193 c := GetWebSafe(c);
194 if c = GetSelectedColor then
195 exit;
196
197 ColorToHSLV(c, H, S, L, V);
198 case BrightnessMode of
199 bmLuminance:
200 begin
201 needNewGradient := (S <> FSat) or (L <> FLum);
202 FLum := L;
203 end;
204 bmValue:
205 begin
206 needNewGradient := (S <> FSat) or (V <> FVal);
207 FVal := V;
208 end;
209 end;
210 FHue := H;
211 FSat := S;
212 if needNewGradient then
213 CreateGradient;
214 Invalidate;
215 DoChange;
216 end;
217
218 end.