| 1 |
unit SLHColorPicker; |
| 2 |
|
| 3 |
{$MODE DELPHI} |
| 4 |
|
| 5 |
interface |
| 6 |
|
| 7 |
uses |
| 8 |
LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics, Forms, Menus, Themes, |
| 9 |
RGBHSLUtils, mbTrackBarPicker, HTMLColors, SLColorPicker, HColorPicker, |
| 10 |
mbColorConv, mbBasicPicker; |
| 11 |
|
| 12 |
type |
| 13 |
TSLHColorPicker = class(TmbBasicPicker) |
| 14 |
private |
| 15 |
FSLPicker: TSLColorPicker; |
| 16 |
FHPicker: THColorPicker; |
| 17 |
FSelectedColor: TColor; |
| 18 |
// FHValue, FSValue, FLValue: Double; |
| 19 |
// FRed, FGreen, FBlue: integer; |
| 20 |
FSLHint, FHHint: string; |
| 21 |
FSLMenu, FHMenu: TPopupMenu; |
| 22 |
FSLCursor, FHCursor: TCursor; |
| 23 |
PBack: TBitmap; |
| 24 |
function GetBrightnessMode: TBrightnessMode; |
| 25 |
function GetHue: Integer; |
| 26 |
function GetSat: Integer; |
| 27 |
function GetLum: Integer; |
| 28 |
function GetVal: Integer; |
| 29 |
function GetMaxHue: Integer; |
| 30 |
function GetMaxLum: Integer; |
| 31 |
function GetMaxSat: Integer; |
| 32 |
function GetMaxVal: Integer; |
| 33 |
function GetRed: Integer; |
| 34 |
function GetGreen: Integer; |
| 35 |
function GetBlue: Integer; |
| 36 |
procedure SetBlue(B: integer); |
| 37 |
procedure SetBrightnessMode(bm: TBrightnessMode); |
| 38 |
procedure SetGreen(G: integer); |
| 39 |
procedure SetHue(H: integer); |
| 40 |
procedure SetLum(L: integer); |
| 41 |
procedure SetRed(R: integer); |
| 42 |
procedure SetSat(S: integer); |
| 43 |
procedure SetVal(V: Integer); |
| 44 |
procedure SetMaxHue(H: Integer); |
| 45 |
procedure SetMaxSat(S: Integer); |
| 46 |
procedure SetMaxLum(L: Integer); |
| 47 |
procedure SetMaxVal(V: Integer); |
| 48 |
procedure SetHHint(h: string); |
| 49 |
procedure SetSLHint(h: string); |
| 50 |
procedure SetSLMenu(m: TPopupMenu); |
| 51 |
procedure SetHMenu(m: TPopupMenu); |
| 52 |
procedure SetHCursor(c: TCursor); |
| 53 |
procedure SetSLCursor(c: TCursor); |
| 54 |
procedure HPickerChange(Sender: TObject); |
| 55 |
procedure SLPickerChange(Sender: TObject); |
| 56 |
protected |
| 57 |
procedure DoChange; override; |
| 58 |
procedure DoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); |
| 59 |
function GetColorUnderCursor: TColor; override; |
| 60 |
function GetSelectedColor: TColor; override; |
| 61 |
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; |
| 62 |
procedure Paint; override; |
| 63 |
procedure Resize; override; |
| 64 |
procedure SetSelectedColor(c: TColor); override; |
| 65 |
public |
| 66 |
constructor Create(AOwner: TComponent); override; |
| 67 |
destructor Destroy; override; |
| 68 |
// procedure BeginUpdate; override; |
| 69 |
// procedure EndUpdate(DoUpdate: Boolean = true); override; |
| 70 |
function GetHexColorUnderCursor: string; override; |
| 71 |
function GetSelectedHexColor: string; |
| 72 |
procedure SetFocus; override; |
| 73 |
property ColorUnderCursor; |
| 74 |
property Red: integer read GetRed write SetRed default 255; |
| 75 |
property Green: integer read GetGreen write SetGreen default 0; |
| 76 |
property Blue: integer read GetBlue write SetBlue default 0; |
| 77 |
published |
| 78 |
property BrightnessMode: TBrightnessMode read GetBrightnessMode |
| 79 |
write SetBrightnessMode default bmValue; |
| 80 |
property SelectedColor default clRed; |
| 81 |
property Hue: integer read GetHue write SetHue default 0; |
| 82 |
property Saturation: integer read GetSat write SetSat default 255; |
| 83 |
property Value: Integer read GetVal write SetVal default 255; |
| 84 |
property Luminance: integer read GetLum write SetLum default 127; |
| 85 |
property HPickerPopupMenu: TPopupMenu read FHMenu write SetHMenu; |
| 86 |
property SLPickerPopupMenu: TPopupMenu read FSLMenu write SetSLMenu; |
| 87 |
property HPickerHintFormat: string read FHHint write SetHHint; |
| 88 |
property SLPickerHintFormat: string read FSLHint write SetSLHint; |
| 89 |
property HPickerCursor: TCursor read FHCursor write SetHCursor default crDefault; |
| 90 |
property SLPickerCursor: TCursor read FSLCursor write SetSLCursor default crDefault; |
| 91 |
property MaxHue: Integer read GetMaxHue write SetMaxHue default 360; |
| 92 |
property MaxSaturation: Integer read GetMaxSat write SetMaxSat default 240; |
| 93 |
property MaxLuminance: Integer read GetMaxLum write SetMaxLum default 240; |
| 94 |
property TabStop default true; |
| 95 |
property ShowHint; |
| 96 |
property ParentShowHint; |
| 97 |
property Anchors; |
| 98 |
property Align; |
| 99 |
property Visible; |
| 100 |
property Enabled; |
| 101 |
property TabOrder; |
| 102 |
property Color; |
| 103 |
property ParentColor default true; |
| 104 |
property OnChange; |
| 105 |
property OnMouseMove; |
| 106 |
end; |
| 107 |
|
| 108 |
|
| 109 |
implementation |
| 110 |
|
| 111 |
const |
| 112 |
WSL = 255; |
| 113 |
HSL = 255; |
| 114 |
WH = 40; |
| 115 |
DIST = 2; |
| 116 |
VDELTA = 8; |
| 117 |
|
| 118 |
{TSLHColorPicker} |
| 119 |
|
| 120 |
constructor TSLHColorPicker.Create(AOwner: TComponent); |
| 121 |
begin |
| 122 |
inherited; |
| 123 |
//ControlStyle := ControlStyle - [csAcceptsControls] + [csOpaque]; |
| 124 |
|
| 125 |
PBack := TBitmap.Create; |
| 126 |
// PBack.PixelFormat := pf32bit; |
| 127 |
ParentColor := true; |
| 128 |
SetInitialBounds(0, 0, WSL + DIST + WH, HSL + 2*VDELTA); |
| 129 |
TabStop := true; |
| 130 |
FHCursor := crDefault; |
| 131 |
FSLCursor := crDefault; |
| 132 |
FHHint := 'Hue: %h'; |
| 133 |
FSLHint := 'S: %s L: %l'#13'Hex: %hex'; |
| 134 |
|
| 135 |
// Saturation-Lightness picker |
| 136 |
FSLPicker := TSLColorPicker.Create(Self); |
| 137 |
InsertControl(FSLPicker); |
| 138 |
with FSLPicker do |
| 139 |
begin |
| 140 |
SetInitialBounds(0, VDELTA, WSL, HSL); |
| 141 |
Cursor := FSLCursor; |
| 142 |
BrightnessMode := bmValue; |
| 143 |
OnChange := SLPickerChange; |
| 144 |
OnMouseMove := DoMouseMove; |
| 145 |
end; |
| 146 |
|
| 147 |
// Hue picker |
| 148 |
FHPicker := THColorPicker.Create(Self); |
| 149 |
InsertControl(FHPicker); |
| 150 |
with FHPicker do |
| 151 |
begin |
| 152 |
Cursor := FHCursor; |
| 153 |
Layout := lyVertical; // put before setting width and height |
| 154 |
SetInitialBounds(WSL + DIST, 0, WH, HSL + 2*VDELTA); |
| 155 |
BrightnessMode := bmValue; |
| 156 |
ArrowPlacement := spBoth; |
| 157 |
NewArrowStyle := true; |
| 158 |
OnChange := HPickerChange; |
| 159 |
OnMouseMove := DoMouseMove; |
| 160 |
end; |
| 161 |
|
| 162 |
// red |
| 163 |
SelectedColor := clRed; |
| 164 |
end; |
| 165 |
|
| 166 |
destructor TSLHColorPicker.Destroy; |
| 167 |
begin |
| 168 |
PBack.Free; |
| 169 |
inherited Destroy; |
| 170 |
end; |
| 171 |
|
| 172 |
procedure TSLHColorPicker.DoChange; |
| 173 |
begin |
| 174 |
FSelectedColor := FSLPicker.SelectedColor; |
| 175 |
inherited; |
| 176 |
end; |
| 177 |
|
| 178 |
procedure TSLHColorPicker.DoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); |
| 179 |
begin |
| 180 |
if Assigned(OnMouseMove) then |
| 181 |
OnMouseMove(Self, Shift, x, y); |
| 182 |
end; |
| 183 |
|
| 184 |
function TSLHColorPicker.GetBrightnessMode: TBrightnessMode; |
| 185 |
begin |
| 186 |
Result := FSLPicker.BrightnessMode; |
| 187 |
end; |
| 188 |
|
| 189 |
function TSLHColorPicker.GetColorUnderCursor: TColor; |
| 190 |
begin |
| 191 |
Result := FSLPicker.ColorUnderCursor; |
| 192 |
end; |
| 193 |
|
| 194 |
function TSLHColorPicker.GetBlue: Integer; |
| 195 |
begin |
| 196 |
Result := GetBValue(FSelectedColor); |
| 197 |
end; |
| 198 |
|
| 199 |
function TSLHColorPicker.GetGreen: Integer; |
| 200 |
begin |
| 201 |
Result := GetGValue(FSelectedColor); |
| 202 |
end; |
| 203 |
|
| 204 |
function TSLHColorPicker.GetHue: Integer; |
| 205 |
begin |
| 206 |
Result := FHPicker.Hue; |
| 207 |
end; |
| 208 |
|
| 209 |
function TSLHColorPicker.GetHexColorUnderCursor: string; |
| 210 |
begin |
| 211 |
Result := FSLPicker.GetHexColorUnderCursor; |
| 212 |
end; |
| 213 |
|
| 214 |
function TSLHColorPicker.GetLum: Integer; |
| 215 |
begin |
| 216 |
Result := FSLPicker.Luminance; |
| 217 |
end; |
| 218 |
|
| 219 |
function TSLHColorPicker.GetMaxHue: Integer; |
| 220 |
begin |
| 221 |
Result := FSLPicker.MaxHue; |
| 222 |
end; |
| 223 |
|
| 224 |
function TSLHColorPicker.GetMaxLum: Integer; |
| 225 |
begin |
| 226 |
Result := FSLPicker.MaxLuminance; |
| 227 |
end; |
| 228 |
|
| 229 |
function TSLHColorPicker.GetMaxSat: Integer; |
| 230 |
begin |
| 231 |
Result := FSLPicker.MaxSaturation; |
| 232 |
end; |
| 233 |
|
| 234 |
function TSLHColorPicker.GetMaxVal: Integer; |
| 235 |
begin |
| 236 |
Result := FSLPicker.MaxValue; |
| 237 |
end; |
| 238 |
|
| 239 |
function TSLHColorPicker.GetRed: Integer; |
| 240 |
begin |
| 241 |
Result := GetRValue(FSelectedColor); |
| 242 |
end; |
| 243 |
|
| 244 |
function TSLHColorPicker.GetSat: Integer; |
| 245 |
begin |
| 246 |
Result := FSLPicker.Saturation; |
| 247 |
end; |
| 248 |
|
| 249 |
function TSLHColorPicker.GetSelectedColor: TColor; |
| 250 |
begin |
| 251 |
Result := FSelectedColor; |
| 252 |
end; |
| 253 |
|
| 254 |
function TSLHColorPicker.GetSelectedHexColor: string; |
| 255 |
begin |
| 256 |
Result := ColorToHex(FSelectedColor); |
| 257 |
end; |
| 258 |
|
| 259 |
function TSLHColorPicker.GetVal: Integer; |
| 260 |
begin |
| 261 |
REsult := FSLPicker.Value; |
| 262 |
end; |
| 263 |
|
| 264 |
procedure TSLHColorPicker.HPickerChange(Sender: TObject); |
| 265 |
begin |
| 266 |
if FSLPicker.Hue = FHPicker.Hue then |
| 267 |
exit; |
| 268 |
FSLPicker.Hue := FHPicker.Hue; |
| 269 |
DoChange; |
| 270 |
end; |
| 271 |
|
| 272 |
procedure TSLHColorPicker.MouseDown(Button: TMouseButton; Shift: TShiftState; |
| 273 |
X, Y: Integer); |
| 274 |
begin |
| 275 |
SetFocus; |
| 276 |
inherited; |
| 277 |
end; |
| 278 |
|
| 279 |
procedure TSLHColorPicker.Paint; |
| 280 |
begin |
| 281 |
PaintParentBack(Canvas); |
| 282 |
end; |
| 283 |
|
| 284 |
procedure TSLHColorPicker.Resize; |
| 285 |
begin |
| 286 |
inherited; |
| 287 |
|
| 288 |
if (FSLPicker = nil) or (FHPicker = nil) then |
| 289 |
exit; |
| 290 |
|
| 291 |
FSLPicker.Width := Width - FHPicker.Width - DIST; |
| 292 |
FSLPicker.Height := Height - 2*VDELTA; |
| 293 |
|
| 294 |
FHPicker.Left := Width - FHPicker.Width; |
| 295 |
FHPicker.Height := Height; |
| 296 |
end; |
| 297 |
|
| 298 |
procedure TSLHColorPicker.SetFocus; |
| 299 |
begin |
| 300 |
inherited; |
| 301 |
FSLPicker.SetFocus; |
| 302 |
end; |
| 303 |
|
| 304 |
procedure TSLHColorPicker.SetBlue(B: integer); |
| 305 |
begin |
| 306 |
SetSelectedColor(RgbToColor(Red, Green, B)); |
| 307 |
end; |
| 308 |
|
| 309 |
procedure TSLHColorPicker.SetBrightnessMode(bm: TBrightnessMode); |
| 310 |
begin |
| 311 |
FSLPicker.BrightnessMode := bm; |
| 312 |
FHPicker.BrightnessMode := bm; |
| 313 |
end; |
| 314 |
|
| 315 |
procedure TSLHColorPicker.SetGreen(G: integer); |
| 316 |
begin |
| 317 |
SetSelectedColor(RgbToColor(Red, G, Blue)); |
| 318 |
end; |
| 319 |
|
| 320 |
procedure TSLHColorPicker.SetHCursor(c: TCursor); |
| 321 |
begin |
| 322 |
FHCursor := c; |
| 323 |
FHPicker.Cursor := c; |
| 324 |
end; |
| 325 |
|
| 326 |
procedure TSLHColorPicker.SetHHint(h: string); |
| 327 |
begin |
| 328 |
FHHint := h; |
| 329 |
FHPicker.HintFormat := h; |
| 330 |
end; |
| 331 |
|
| 332 |
procedure TSLHColorPicker.SetHMenu(m: TPopupMenu); |
| 333 |
begin |
| 334 |
FHMenu := m; |
| 335 |
FHPicker.PopupMenu := m; |
| 336 |
end; |
| 337 |
|
| 338 |
procedure TSLHColorPicker.SetHue(H: integer); |
| 339 |
begin |
| 340 |
FHPicker.Hue := H; |
| 341 |
FSLPicker.Hue := H; |
| 342 |
end; |
| 343 |
|
| 344 |
procedure TSLHColorPicker.SetLum(L: integer); |
| 345 |
begin |
| 346 |
FSLPicker.Luminance := L; |
| 347 |
end; |
| 348 |
|
| 349 |
procedure TSLHColorPicker.SetMaxHue(H: Integer); |
| 350 |
begin |
| 351 |
FSLPicker.MaxHue := H; |
| 352 |
FHPicker.MaxHue := H; |
| 353 |
end; |
| 354 |
|
| 355 |
procedure TSLHColorPicker.SetMaxLum(L: Integer); |
| 356 |
begin |
| 357 |
FSLPicker.MaxLuminance := L; |
| 358 |
FHPicker.MaxLuminance := L; |
| 359 |
end; |
| 360 |
|
| 361 |
procedure TSLHColorPicker.SetMaxSat(S: Integer); |
| 362 |
begin |
| 363 |
FSLPicker.MaxSaturation := S; |
| 364 |
FHPicker.MaxSaturation := S; |
| 365 |
end; |
| 366 |
|
| 367 |
procedure TSLHColorPicker.SetMaxVal(V: Integer); |
| 368 |
begin |
| 369 |
FSLPicker.MaxValue := V; |
| 370 |
FHPicker.MaxValue := V; |
| 371 |
end; |
| 372 |
|
| 373 |
procedure TSLHColorPicker.SetRed(R: integer); |
| 374 |
begin |
| 375 |
SetSelectedColor(RgbToColor(R, Green, Blue)); |
| 376 |
end; |
| 377 |
|
| 378 |
procedure TSLHColorPicker.SetSat(S: integer); |
| 379 |
begin |
| 380 |
FSLPicker.Saturation := S; |
| 381 |
end; |
| 382 |
|
| 383 |
procedure TSLHColorPicker.SetSelectedColor(c: TColor); |
| 384 |
begin |
| 385 |
FSelectedColor := c; |
| 386 |
FHPicker.Hue := GetHValue(c); |
| 387 |
FSLPicker.SelectedColor := c; |
| 388 |
end; |
| 389 |
|
| 390 |
procedure TSLHColorPicker.SetSLHint(h: string); |
| 391 |
begin |
| 392 |
FSLHint := h; |
| 393 |
FSLPicker.HintFormat := h; |
| 394 |
end; |
| 395 |
|
| 396 |
procedure TSLHColorPicker.SetSLCursor(c: TCursor); |
| 397 |
begin |
| 398 |
FSLCursor := c; |
| 399 |
FSLPicker.Cursor := c; |
| 400 |
end; |
| 401 |
|
| 402 |
procedure TSLHColorPicker.SetSLMenu(m: TPopupMenu); |
| 403 |
begin |
| 404 |
FSLMenu := m; |
| 405 |
FSLPicker.PopupMenu := m; |
| 406 |
end; |
| 407 |
|
| 408 |
procedure TSLHColorPicker.SetVal(V: Integer); |
| 409 |
begin |
| 410 |
FSLPicker.Value := V; |
| 411 |
end; |
| 412 |
|
| 413 |
procedure TSLHColorPicker.SLPickerChange(Sender: TObject); |
| 414 |
begin |
| 415 |
if FSelectedColor = FSLPicker.SelectedColor then |
| 416 |
exit; |
| 417 |
FSelectedColor := FSLPicker.SelectedColor; |
| 418 |
DoChange; |
| 419 |
end; |
| 420 |
|
| 421 |
|
| 422 |
end. |