Debian Codegraph @

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

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