| 1 |
unit HexaColorPicker; |
| 2 |
|
| 3 |
{$IFDEF FPC} |
| 4 |
{$MODE DELPHI} |
| 5 |
{$ENDIF} |
| 6 |
|
| 7 |
interface |
| 8 |
|
| 9 |
//{$I mxs.inc} |
| 10 |
|
| 11 |
uses |
| 12 |
LCLIntf, LCLType, LMessages, SysUtils, Classes, Controls, Graphics, StdCtrls, |
| 13 |
Forms, Themes, Math, |
| 14 |
HTMLColors, mbBasicPicker; |
| 15 |
|
| 16 |
const |
| 17 |
CustomCell = -2; |
| 18 |
NoCell = -1; |
| 19 |
|
| 20 |
type |
| 21 |
TMarker = (smArrow, smRect); |
| 22 |
|
| 23 |
TCombEntry = record |
| 24 |
Position: TPoint; |
| 25 |
Color: COLORREF; |
| 26 |
TabIndex: integer; |
| 27 |
end; |
| 28 |
|
| 29 |
TCombArray = array of TCombEntry; |
| 30 |
|
| 31 |
TFloatPoint = record |
| 32 |
X, Y: Extended; |
| 33 |
end; |
| 34 |
|
| 35 |
TRGBrec = record |
| 36 |
Red, Green, Blue: Single; |
| 37 |
end; |
| 38 |
|
| 39 |
TSelectionMode = (smNone, smColor, smBW, smRamp); |
| 40 |
|
| 41 |
THexaColorPicker = class(TmbBasicPicker) |
| 42 |
private |
| 43 |
FIncrement: integer; |
| 44 |
FSelectedCombIndex: integer; |
| 45 |
mX, mY: integer; |
| 46 |
FHintFormat: string; |
| 47 |
FUnderCursor: TColor; |
| 48 |
//FOnChange, |
| 49 |
FOnIntensityChange: TNotifyEvent; |
| 50 |
FCurrentColor: TColor; |
| 51 |
FSelectedIndex: Integer; |
| 52 |
FColorCombRect, FBWCombRect, FSliderRect, FCustomColorRect: TRect; |
| 53 |
FCombSize, FLevels: Integer; |
| 54 |
FBWCombs, FColorCombs: TCombArray; |
| 55 |
FCombCorners: array[0..5] of TFloatPoint; |
| 56 |
FCenterColor: TRGBrec; |
| 57 |
FCenterIntensity: Single; |
| 58 |
FSliderWidth: integer; |
| 59 |
FCustomIndex: Integer; // If FSelectedIndex contains CustomCell then this index shows |
| 60 |
// which index in the custom area has been selected. |
| 61 |
// Positive values indicate the color comb and negative values |
| 62 |
// indicate the B&W combs (complement). This value is offset with |
| 63 |
// 1 to use index 0 to show no selection. |
| 64 |
FRadius: Integer; |
| 65 |
FSelectionMode: TSelectionMode; |
| 66 |
FSliderVisible: boolean; |
| 67 |
FMarker: TMarker; |
| 68 |
FNewArrowStyle: boolean; |
| 69 |
FIntensityText: string; |
| 70 |
procedure CalculateCombLayout; |
| 71 |
procedure ChangeIntensity(increase: boolean); |
| 72 |
procedure DrawAll; |
| 73 |
procedure DrawComb(ACanvas: TCanvas; X, Y, Size: Integer); |
| 74 |
procedure DrawCombControls(ACanvas: TCanvas); |
| 75 |
procedure EndSelection; |
| 76 |
procedure EnumerateCombs; |
| 77 |
function FindBWArea(X, Y: Integer): Integer; |
| 78 |
function FindColorArea(X, Y: Integer): Integer; |
| 79 |
function GetIntensity: integer; |
| 80 |
function GetNextCombIndex(i: integer): integer; |
| 81 |
function GetPreviousCombIndex(i: integer): integer; |
| 82 |
procedure HandleCustomColors(var Message: TLMMouse); |
| 83 |
function HandleBWArea(const Message: TLMMouse): Boolean; |
| 84 |
function HandleColorComb(const Message: TLMMouse): Boolean; |
| 85 |
function HandleSlider(const Message: TLMMouse): Boolean; |
| 86 |
procedure Initialize; |
| 87 |
function PtInComb(Comb: TCombEntry; P: TPoint; Scale: Integer): Boolean; |
| 88 |
procedure SetIntensity(v: integer); |
| 89 |
procedure SetNewArrowStyle(Value: boolean); |
| 90 |
procedure SetMarker(Value: TMarker); |
| 91 |
procedure SetRadius(r: integer); |
| 92 |
procedure SetSliderVisible(Value: boolean); |
| 93 |
procedure SetSliderWidth(w: integer); |
| 94 |
function SelectAvailableColor(Color: TColor): boolean; |
| 95 |
procedure SelectColor(Color: TColor); |
| 96 |
protected |
| 97 |
function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override; |
| 98 |
procedure KeyDown(var Key: Word; Shift: TShiftState); override; |
| 99 |
procedure Paint; override; |
| 100 |
procedure Resize; override; |
| 101 |
procedure SetSelectedColor(Value: TColor); override; |
| 102 |
procedure CMHintShow(var Message: TLMessage); message CM_HINTSHOW; |
| 103 |
procedure WMLButtonDown(var Message: TLMLButtonDown); message LM_LBUTTONDOWN; |
| 104 |
procedure WMLButtonUp(var Message: TLMLButtonUp); message LM_LBUTTONUP; |
| 105 |
procedure WMMouseMove(var Message: TLMMouseMove); message LM_MOUSEMOVE; |
| 106 |
public |
| 107 |
constructor Create(AOwner: TComponent); override; |
| 108 |
destructor Destroy; override; |
| 109 |
function GetColorAtPoint(X, Y: integer): TColor; override; |
| 110 |
function GetColorUnderCursor: TColor; override; |
| 111 |
function GetHexColorUnderCursor: string; override; |
| 112 |
function GetHexColorAtPoint(X, Y: integer): string; |
| 113 |
function GetSelectedCombIndex: integer; |
| 114 |
procedure SelectCombIndex(i: integer); |
| 115 |
property ColorUnderCursor: TColor read GetColorUnderCursor; |
| 116 |
published |
| 117 |
property Align; |
| 118 |
property Anchors; |
| 119 |
property HintFormat: string read FHintFormat write FHintFormat; |
| 120 |
property Intensity: integer read GetIntensity write SetIntensity default 100; |
| 121 |
property IntensityIncrement: integer read FIncrement write FIncrement default 1; |
| 122 |
property IntensityText: string read FIntensityText write FIntensityText; |
| 123 |
property NewArrowStyle: boolean read FNewArrowStyle write SetNewArrowStyle default false; |
| 124 |
property SelectedColor: TColor read FCurrentColor write SetSelectedColor default clBlack; |
| 125 |
property SliderVisible: boolean read FSliderVisible write SetSliderVisible default true; |
| 126 |
property SliderWidth: integer read FSliderWidth write SetSliderWidth default 12; |
| 127 |
property SliderMarker: TMarker read FMarker write SetMarker default smArrow; |
| 128 |
property ShowHint default true; |
| 129 |
property TabStop default true; |
| 130 |
property Visible; |
| 131 |
property Enabled; |
| 132 |
property PopupMenu; |
| 133 |
property TabOrder; |
| 134 |
property Color; |
| 135 |
property ParentColor; |
| 136 |
property DragCursor; |
| 137 |
property DragMode; |
| 138 |
property DragKind; |
| 139 |
property Constraints; |
| 140 |
property OnChange; //: TNotifyEvent read FOnChange write FOnChange; |
| 141 |
property OnIntensityChange: TNotifyEvent read FOnIntensityChange write FOnIntensityChange; |
| 142 |
property OnDblClick; |
| 143 |
property OnContextPopup; |
| 144 |
property OnMouseDown; |
| 145 |
property OnMouseMove; |
| 146 |
property OnMouseUp; |
| 147 |
property OnMouseWheel; |
| 148 |
property OnMouseWheelUp; |
| 149 |
property OnMouseWheelDown; |
| 150 |
property OnKeyDown; |
| 151 |
property OnKeyPress; |
| 152 |
property OnKeyUp; |
| 153 |
property OnDragDrop; |
| 154 |
property OnDragOver; |
| 155 |
property OnEndDrag; |
| 156 |
property OnEnter; |
| 157 |
property OnExit; |
| 158 |
property OnResize; |
| 159 |
property OnStartDrag; |
| 160 |
end; |
| 161 |
|
| 162 |
const |
| 163 |
DefCenterColor: TRGBrec = (Red: 1; Green: 1; Blue: 1); // White |
| 164 |
DefColors: array[0..5] of TRGBrec = ( |
| 165 |
(Red: 1; Green: 0; Blue: 1), // Magenta |
| 166 |
(Red: 1; Green: 0; Blue: 0), // Red |
| 167 |
(Red: 1; Green: 1; Blue: 0), // Yellow |
| 168 |
(Red: 0; Green: 1; Blue: 0), // Green |
| 169 |
(Red: 0; Green: 1; Blue: 1), // Cyan |
| 170 |
(Red: 0; Green: 0; Blue: 1) // Blue |
| 171 |
); |
| 172 |
DefCenter: TFloatPoint = (X: 0; Y: 0); |
| 173 |
|
| 174 |
|
| 175 |
implementation |
| 176 |
|
| 177 |
uses |
| 178 |
PalUtils, mbUtils; |
| 179 |
|
| 180 |
{ THexaColorPicker } |
| 181 |
|
| 182 |
constructor THexaColorPicker.Create(AOwner: TComponent); |
| 183 |
begin |
| 184 |
inherited; |
| 185 |
//ControlStyle := ControlStyle - [csAcceptsControls] + [csOpaque]; |
| 186 |
FRadius := 90; |
| 187 |
FSliderWidth := 12; |
| 188 |
DoubleBuffered := true; |
| 189 |
SetInitialBounds(0, 0, 204, 204); |
| 190 |
Constraints.MinHeight := 85; |
| 191 |
Constraints.MinWidth := 93; |
| 192 |
TabStop := true; |
| 193 |
FSelectedCombIndex := 0; |
| 194 |
FHintFormat := 'RGB(%r, %g, %b)'#13'Hex: #%hex'; |
| 195 |
ShowHint := True; |
| 196 |
FSliderVisible := true; |
| 197 |
FMarker := smArrow; |
| 198 |
FNewArrowStyle := false; |
| 199 |
Initialize; |
| 200 |
DrawAll; |
| 201 |
FIntensityText := 'Intensity'; |
| 202 |
{ |
| 203 |
MaxHue := 360; |
| 204 |
MaxLum := 255; |
| 205 |
MaxSat := 255; |
| 206 |
} |
| 207 |
end; |
| 208 |
|
| 209 |
destructor THexaColorPicker.Destroy; |
| 210 |
begin |
| 211 |
FBWCombs := nil; |
| 212 |
FColorCombs := nil; |
| 213 |
// FBufferBmp.Free; is already destroyed by ancestor TmbBasicPicker |
| 214 |
inherited; |
| 215 |
end; |
| 216 |
|
| 217 |
procedure THexaColorPicker.ChangeIntensity(increase: boolean); |
| 218 |
var |
| 219 |
i: integer; |
| 220 |
begin |
| 221 |
i := round(FCenterIntensity * 100); |
| 222 |
if increase then |
| 223 |
begin |
| 224 |
Inc(i, FIncrement); |
| 225 |
if i > 100 then i := 100; |
| 226 |
SetIntensity(i); |
| 227 |
end |
| 228 |
else |
| 229 |
begin |
| 230 |
Dec(i, FIncrement); |
| 231 |
if i < 0 then i := 0; |
| 232 |
SetIntensity(i); |
| 233 |
end; |
| 234 |
end; |
| 235 |
|
| 236 |
function THexaColorPicker.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; |
| 237 |
MousePos: TPoint): Boolean; |
| 238 |
begin |
| 239 |
Result := inherited DoMouseWheel(Shift, WheelDelta, MousePos); |
| 240 |
if not Result then |
| 241 |
begin |
| 242 |
Result := True; |
| 243 |
ChangeIntensity(WheelDelta > 0); |
| 244 |
end; |
| 245 |
end; |
| 246 |
|
| 247 |
procedure THexaColorPicker.DrawComb(ACanvas: TCanvas; X, Y: Integer; Size: Integer); |
| 248 |
var |
| 249 |
I: Integer; |
| 250 |
P: array[0..5] of TPoint; |
| 251 |
begin |
| 252 |
for I := 0 to 5 do |
| 253 |
begin |
| 254 |
P[I].X := Round(FCombCorners[I].X * Size + X); |
| 255 |
P[I].Y := Round(FCombCorners[I].Y * Size + Y); |
| 256 |
end; |
| 257 |
ACanvas.Polygon(P); |
| 258 |
end; |
| 259 |
|
| 260 |
procedure THexaColorPicker.DrawCombControls(ACanvas: TCanvas); |
| 261 |
var |
| 262 |
I, Index: Integer; |
| 263 |
XOffs, YOffs, Count: Integer; |
| 264 |
OffScreen: TBitmap; |
| 265 |
R: TRect; |
| 266 |
begin |
| 267 |
OffScreen := TBitmap.Create; |
| 268 |
try |
| 269 |
OffScreen.Width := Width; |
| 270 |
OffScreen.Height := HeightOfRect(FColorCombRect) + HeightOfRect(FBWCombRect); |
| 271 |
|
| 272 |
//Parent background |
| 273 |
if Color = clDefault then |
| 274 |
begin |
| 275 |
Offscreen.Transparent := true; |
| 276 |
Offscreen.TransparentColor := clForm; |
| 277 |
Offscreen.Canvas.Brush.Color := clForm |
| 278 |
end else |
| 279 |
OffScreen.Canvas.Brush.Color := Color; |
| 280 |
OffScreen.Canvas.FillRect(OffScreen.Canvas.ClipRect); |
| 281 |
|
| 282 |
with OffScreen.Canvas do |
| 283 |
begin |
| 284 |
Pen.Style := psClear; |
| 285 |
|
| 286 |
// draw color combs from FColorCombs array |
| 287 |
XOffs := FRadius + FColorCombRect.Left; |
| 288 |
YOffs := FRadius + FColorCombRect.Top; |
| 289 |
|
| 290 |
// draw the combs |
| 291 |
for I := 0 to High(FColorCombs) do |
| 292 |
begin |
| 293 |
Brush.Color := FColorCombs[I].Color; |
| 294 |
Pen.Mode := pmCopy; // the pen is set here so there are no gaps between the combs |
| 295 |
Pen.Style := psSolid; |
| 296 |
Pen.Color := FColorCombs[I].Color; |
| 297 |
DrawComb(OffScreen.Canvas, FColorCombs[I].Position.X + XOffs, FColorCombs[I].Position.Y + YOffs, FCombSize); |
| 298 |
end; |
| 299 |
|
| 300 |
// mark selected comb |
| 301 |
if FCustomIndex > 0 then |
| 302 |
begin |
| 303 |
Index := FCustomIndex - 1; |
| 304 |
FSelectedCombIndex := index; |
| 305 |
Pen.Style := psSolid; |
| 306 |
{ |
| 307 |
Pen.Mode := pmXOR; |
| 308 |
Pen.Color := clWhite; |
| 309 |
} |
| 310 |
Pen.Color := HighContrastColor(FColorCombs[Index].Color); |
| 311 |
Pen.Width := 2; |
| 312 |
Brush.Style := bsClear; |
| 313 |
DrawComb(OffScreen.Canvas, FColorCombs[Index].Position.X + XOffs, FColorCombs[Index].Position.Y + YOffs, FCombSize); |
| 314 |
Pen.Style := psClear; |
| 315 |
Pen.Mode := pmCopy; |
| 316 |
Pen.Width := 1; |
| 317 |
end; |
| 318 |
|
| 319 |
// draw white-to-black combs |
| 320 |
XOffs := FColorCombRect.Left; |
| 321 |
YOffs := FColorCombRect.Bottom - 4; |
| 322 |
// brush is automatically reset to bsSolid |
| 323 |
for I := 0 to High(FBWCombs) do |
| 324 |
begin |
| 325 |
Pen.Mode := pmCopy; // the pen is set here so there are no gaps between the combs |
| 326 |
Pen.Style := psSolid; |
| 327 |
Pen.Color := FBWCombs[I].Color; |
| 328 |
Brush.Color := FBWCombs[I].Color; |
| 329 |
if I in [0, High(FBWCombs)] then |
| 330 |
begin |
| 331 |
if Pen.Color = clWhite then // "white" needs a border if background is white as well |
| 332 |
Pen.Color := clGray; |
| 333 |
DrawComb(OffScreen.Canvas, FBWCombs[I].Position.X + XOffs, FBWCombs[I].Position.Y + YOffs, 2 * FCombSize) |
| 334 |
end else |
| 335 |
DrawComb(OffScreen.Canvas, FBWCombs[I].Position.X + XOffs, FBWCombs[I].Position.Y + YOffs, FCombSize); |
| 336 |
end; |
| 337 |
|
| 338 |
// mark selected comb |
| 339 |
if FCustomIndex < 0 then |
| 340 |
begin |
| 341 |
Index := -(FCustomIndex + 1); |
| 342 |
if index < 0 then |
| 343 |
FSelectedCombIndex := Index |
| 344 |
else |
| 345 |
FSelectedCombIndex := -index; |
| 346 |
Pen.Style := psSolid; |
| 347 |
{ |
| 348 |
Pen.Mode := pmXOR; |
| 349 |
Pen.Color := clWhite; |
| 350 |
} |
| 351 |
Pen.Mode := pmCopy; |
| 352 |
Pen.Color := HighContrastColor(FBWCombs[Index].Color); |
| 353 |
Pen.Width := 2; |
| 354 |
Brush.Style := bsClear; |
| 355 |
if Index in [0, High(FBWCombs)] then |
| 356 |
begin |
| 357 |
if Index = High(FBWCombs) then begin |
| 358 |
Pen.Color := rgb(254, 254, 254); //clWhite; |
| 359 |
Pen.Mode := pmXOR; |
| 360 |
end; |
| 361 |
if ((FColorCombs[0].Color = Cardinal(clWhite)) and (Index = 0)) or |
| 362 |
((FColorCombs[0].Color = Cardinal(clBlack)) and (Index = High(FBWCombs))) |
| 363 |
then |
| 364 |
DrawComb(OffScreen.Canvas, FRadius + FColorCombRect.Left, FRadius + FColorCombRect.Top, FCombSize); // mark white or black center |
| 365 |
DrawComb(OffScreen.Canvas, FBWCombs[Index].Position.X + XOffs, FBWCombs[Index].Position.Y + YOffs, 2 * FCombSize); |
| 366 |
end |
| 367 |
else |
| 368 |
DrawComb(OffScreen.Canvas, FBWCombs[Index].Position.X + XOffs, FBWCombs[Index].Position.Y + YOffs, FCombSize); |
| 369 |
Pen.Style := psClear; |
| 370 |
Pen.Mode := pmCopy; |
| 371 |
Pen.Width := 1; |
| 372 |
end; |
| 373 |
|
| 374 |
// Slider |
| 375 |
if FSliderVisible then |
| 376 |
begin |
| 377 |
// center-color trackbar |
| 378 |
R := FSliderRect; |
| 379 |
R.Right := R.Left + FSliderWidth; |
| 380 |
Pen.Style := psSolid; |
| 381 |
GradientFill(R, clWhite, clBlack, gdVertical); |
| 382 |
|
| 383 |
// draw marker |
| 384 |
Count := FSliderRect.Bottom - FSliderRect.Top - 1; |
| 385 |
XOffs := FSliderRect.Left + FSliderWidth + 1; |
| 386 |
YOffs := FSliderRect.Top + Round(Count * (1 - FCenterIntensity));; |
| 387 |
Pen.Color := clBlack; |
| 388 |
case FMarker of |
| 389 |
smArrow: |
| 390 |
begin |
| 391 |
if not FNewArrowStyle then |
| 392 |
begin |
| 393 |
Brush.Color := clBlack; |
| 394 |
Polygon([ |
| 395 |
Point(XOffs, YOffs), |
| 396 |
Point(XOffs + 6, YOffs - 4), |
| 397 |
Point(XOffs + 6, YOffs + 4) |
| 398 |
]) |
| 399 |
end |
| 400 |
else |
| 401 |
begin |
| 402 |
Brush.Color := clWhite; |
| 403 |
Pen.Color := clBtnShadow; |
| 404 |
Polygon([ |
| 405 |
Point(XOffs, YOffs), |
| 406 |
Point(XOffs + 4, YOffs - 4), |
| 407 |
Point(XOffs + 6, YOffs - 4), |
| 408 |
Point(XOffs + 7, YOffs - 3), |
| 409 |
Point(XOffs + 7, YOffs + 3), |
| 410 |
Point(XOffs + 6, YOffs + 4), |
| 411 |
Point(XOffs + 4, YOffs + 4)]); |
| 412 |
end; |
| 413 |
end; |
| 414 |
smRect: |
| 415 |
begin |
| 416 |
Brush.Style := bsClear; |
| 417 |
Pen.Mode := pmNot; |
| 418 |
Rectangle(XOffs - FSliderWidth - 4, YOffs - 3, XOffs + 2, YOffs + 3); |
| 419 |
Pen.Mode := pmCopy; |
| 420 |
Brush.Style := bsSolid; |
| 421 |
end; |
| 422 |
end; // case |
| 423 |
Pen.Style := psClear; |
| 424 |
end; |
| 425 |
end; |
| 426 |
ACanvas.Draw(0, 0, OffScreen); |
| 427 |
finally |
| 428 |
Offscreen.Free; |
| 429 |
end; |
| 430 |
EnumerateCombs; |
| 431 |
end; |
| 432 |
|
| 433 |
// Looks for a comb at position (X, Y) in the black&white area. |
| 434 |
// Result is -1 if nothing could be found else the index of the particular comb |
| 435 |
// into FBWCombs. |
| 436 |
function THexaColorPicker.FindBWArea(X, Y: Integer): Integer; |
| 437 |
var |
| 438 |
I, Scale: Integer; |
| 439 |
Pt: TPoint; |
| 440 |
begin |
| 441 |
Result := -1; |
| 442 |
Pt := Point(X - FBWCombRect.Left, Y - FBWCombRect.Top); |
| 443 |
for I := 0 to High(FBWCombs) do |
| 444 |
begin |
| 445 |
if I in [0, High(FBWCombs)] then |
| 446 |
Scale := FCombSize |
| 447 |
else |
| 448 |
Scale := FCombSize div 2; |
| 449 |
if PtInComb(FBWCombs[I], Pt, Scale) then |
| 450 |
begin |
| 451 |
Result := I; |
| 452 |
Break; |
| 453 |
end; |
| 454 |
end; |
| 455 |
end; |
| 456 |
|
| 457 |
// Looks for a comb at position (X, Y) in the custom color area. |
| 458 |
// Result is -1 if nothing could be found else the index of the particular comb |
| 459 |
// into FColorCombs. |
| 460 |
function THexaColorPicker.FindColorArea(X, Y: Integer): Integer; |
| 461 |
var |
| 462 |
I: Integer; |
| 463 |
Pt: TPoint; |
| 464 |
begin |
| 465 |
Result := -1; |
| 466 |
Pt := Point(X - (FRadius + FColorCombRect.Left), Y - (FRadius + FColorCombRect.Top)); |
| 467 |
for I := 0 to High(FColorCombs) do |
| 468 |
begin |
| 469 |
if PtInComb(FColorCombs[I], Pt, FCombSize div 2) then |
| 470 |
begin |
| 471 |
Result := I; |
| 472 |
Break; |
| 473 |
end; |
| 474 |
end; |
| 475 |
end; |
| 476 |
|
| 477 |
function THexaColorPicker.GetIntensity: integer; |
| 478 |
begin |
| 479 |
Result := round(FCenterIntensity * 100); |
| 480 |
end; |
| 481 |
|
| 482 |
function THexaColorPicker.GetNextCombIndex(i: integer): integer; |
| 483 |
begin |
| 484 |
if i = 127 then |
| 485 |
Result := -1 |
| 486 |
else |
| 487 |
if i = -15 then |
| 488 |
Result := 1 |
| 489 |
else |
| 490 |
if i > 0 then |
| 491 |
Result := i + 1 |
| 492 |
else |
| 493 |
Result := i - 1; |
| 494 |
end; |
| 495 |
|
| 496 |
function THexaColorPicker.GetPreviousCombIndex(i: integer): integer; |
| 497 |
begin |
| 498 |
if i = 1 then |
| 499 |
Result := -15 |
| 500 |
else |
| 501 |
if i = -1 then |
| 502 |
Result := 127 |
| 503 |
else |
| 504 |
if i > 0 then |
| 505 |
Result := i - 1 |
| 506 |
else |
| 507 |
Result := i + 1; |
| 508 |
end; |
| 509 |
|
| 510 |
function THexaColorPicker.GetSelectedCombIndex: integer; |
| 511 |
begin |
| 512 |
if FSelectedCombIndex < 0 then |
| 513 |
Result := FBWCombs[-FSelectedCombIndex].TabIndex |
| 514 |
else |
| 515 |
Result := FColorCombs[FSelectedCombIndex].TabIndex; |
| 516 |
end; |
| 517 |
|
| 518 |
// determines whether the mouse position is within the B&W comb area and acts accordingly |
| 519 |
function THexaColorPicker.HandleBWArea(const Message: TLMMouse): Boolean; |
| 520 |
var |
| 521 |
Index: Integer; |
| 522 |
Shift: TShiftState; |
| 523 |
begin |
| 524 |
Result := PtInRect(FBWCombRect, Point(Message.XPos, Message.YPos)) and (FSelectionMode in [smNone, smBW]); |
| 525 |
if Result then |
| 526 |
begin |
| 527 |
Shift := KeysToShiftState(Message.Keys); |
| 528 |
if ssLeft in Shift then |
| 529 |
begin |
| 530 |
FSelectionMode := smBW; |
| 531 |
Index := FindBWArea(Message.XPos, Message.YPos); |
| 532 |
if Index > -1 then |
| 533 |
begin |
| 534 |
// remove selection comb if it was previously in color comb |
| 535 |
if FCustomIndex > 0 then InvalidateRect(Handle, @FColorCombRect, False); |
| 536 |
if FCustomIndex <> -(Index + 1) then |
| 537 |
begin |
| 538 |
FCustomIndex := -(Index + 1); |
| 539 |
InvalidateRect(Handle, @FBWCombRect, False); |
| 540 |
InvalidateRect(Handle, @FCustomColorRect, False); |
| 541 |
EndSelection; |
| 542 |
end; |
| 543 |
end |
| 544 |
else |
| 545 |
Result := False; |
| 546 |
end; |
| 547 |
end; |
| 548 |
end; |
| 549 |
|
| 550 |
// determines whether the mouse position is within the color comb area and acts accordingly |
| 551 |
function THexaColorPicker.HandleColorComb(const Message: TLMMouse): Boolean; |
| 552 |
var |
| 553 |
Index: Integer; |
| 554 |
Shift: TShiftState; |
| 555 |
begin |
| 556 |
Result := PtInRect(FColorCombRect, Point(Message.XPos, Message.YPos)) and (FSelectionMode in [smNone, smColor]); |
| 557 |
if Result then |
| 558 |
begin |
| 559 |
Shift := KeysToShiftState(Message.Keys); |
| 560 |
if ssLeft in Shift then |
| 561 |
begin |
| 562 |
FSelectionMode := smColor; |
| 563 |
Index := FindColorArea(Message.XPos, Message.YPos); |
| 564 |
if Index > -1 then |
| 565 |
begin |
| 566 |
// remove selection comb if it was previously in b&w comb |
| 567 |
if FCustomIndex < 0 then InvalidateRect(Handle, @FBWCombRect, False); |
| 568 |
if FCustomIndex <> (Index + 1) then |
| 569 |
begin |
| 570 |
FCustomIndex := Index + 1; |
| 571 |
InvalidateRect(Handle, @FColorCombRect, False); |
| 572 |
InvalidateRect(Handle, @FCustomColorRect, False); |
| 573 |
EndSelection; |
| 574 |
end; |
| 575 |
end |
| 576 |
else |
| 577 |
Result := False; |
| 578 |
end; |
| 579 |
end; |
| 580 |
end; |
| 581 |
|
| 582 |
procedure THexaColorPicker.HandleCustomColors( |
| 583 |
var Message: {$IFDEF FPC}TLMMouse{$ELSE}TWMMouse{$ENDIF}); |
| 584 |
begin |
| 585 |
if not HandleSlider(Message) then |
| 586 |
if not HandleBWArea(Message) then |
| 587 |
HandleColorComb(Message); |
| 588 |
end; |
| 589 |
|
| 590 |
// determines whether the mouse position is within the slider area and acts accordingly |
| 591 |
function THexaColorPicker.HandleSlider(const Message: TLMMouse): Boolean; |
| 592 |
var |
| 593 |
Shift: TShiftState; |
| 594 |
dY: Integer; |
| 595 |
R: TRect; |
| 596 |
begin |
| 597 |
if not FSliderVisible then |
| 598 |
begin |
| 599 |
Result := false; |
| 600 |
Exit; |
| 601 |
end; |
| 602 |
|
| 603 |
Result := |
| 604 |
(PtInRect(FSliderRect, Point(Message.XPos, Message.YPos)) |
| 605 |
and (FSelectionMode = smNone)) |
| 606 |
or |
| 607 |
((Message.XPos >= FSliderRect.Left) and (Message.XPos <= FSliderRect.Right) |
| 608 |
and (FSelectionMode = smRamp)); |
| 609 |
|
| 610 |
if Result then |
| 611 |
begin |
| 612 |
Shift := KeysToShiftState(Message.Keys); |
| 613 |
if ssLeft in Shift then |
| 614 |
begin |
| 615 |
FSelectionMode := smRamp; |
| 616 |
dY := FSliderRect.Bottom - FSliderRect.Top; |
| 617 |
FCenterIntensity := 1 - (Message.YPos - FSliderRect.Top) / dY; |
| 618 |
if FCenterIntensity < 0 then FCenterIntensity := 0; |
| 619 |
if FCenterIntensity > 1 then FCenterIntensity := 1; |
| 620 |
FCenterColor.Red := DefCenterColor.Red * FCenterIntensity; |
| 621 |
FCenterColor.Green := DefCenterColor.Green * FCenterIntensity; |
| 622 |
FCenterColor.Blue := DefCenterColor.Blue * FCenterIntensity; |
| 623 |
R := FSliderRect; |
| 624 |
Dec(R.Top, 3); |
| 625 |
Inc(R.Bottom, 3); |
| 626 |
Inc(R.Left, 10); |
| 627 |
InvalidateRect(Handle, @R, False); |
| 628 |
FColorCombs := nil; |
| 629 |
InvalidateRect(Handle, @FColorCombRect, False); |
| 630 |
InvalidateRect(Handle, @FCustomColorRect, False); |
| 631 |
CalculateCombLayout; |
| 632 |
EndSelection; |
| 633 |
if Assigned(FOnIntensityChange) then |
| 634 |
FOnIntensityChange(Self); |
| 635 |
end; |
| 636 |
end; |
| 637 |
end; |
| 638 |
|
| 639 |
procedure THexaColorPicker.Initialize; |
| 640 |
var |
| 641 |
I: Integer; |
| 642 |
begin |
| 643 |
FSelectedIndex := NoCell; |
| 644 |
for I := 0 to 5 do |
| 645 |
begin |
| 646 |
FCombCorners[I].X := 0.5 * cos(Pi * (90 - I * 60) / 180); |
| 647 |
FCombCorners[I].Y := 0.5 * sin(Pi * (90 - I * 60) / 180); |
| 648 |
end; |
| 649 |
FLevels := 7; |
| 650 |
FCombSize := Round(FRadius / (FLevels - 1)); |
| 651 |
FCenterColor := DefCenterColor; |
| 652 |
FIncrement := 1; |
| 653 |
FCenterIntensity := 1; |
| 654 |
end; |
| 655 |
|
| 656 |
procedure THexaColorPicker.KeyDown(var Key: Word; Shift: TShiftState); |
| 657 |
var |
| 658 |
eraseKey: Boolean; |
| 659 |
begin |
| 660 |
eraseKey := true; |
| 661 |
if ssCtrl in Shift then |
| 662 |
case Key of |
| 663 |
VK_LEFT: SetSelectedColor(clWhite); |
| 664 |
VK_RIGHT: SetSelectedColor(clBlack); |
| 665 |
VK_UP: if FSliderVisible then SetIntensity(100); |
| 666 |
VK_DOWN: if FSliderVisible then SetIntensity(0); |
| 667 |
else |
| 668 |
eraseKey := false; |
| 669 |
end |
| 670 |
else |
| 671 |
case Key of |
| 672 |
VK_LEFT: SelectCombIndex(GetPreviousCombIndex(GetSelectedCombIndex)); |
| 673 |
VK_RIGHT: SelectCombIndex(GetNextCombIndex(GetSelectedCombIndex)); |
| 674 |
VK_UP: if FSliderVisible then ChangeIntensity(true); |
| 675 |
VK_DOWN: if FSliderVisible then ChangeIntensity(false); |
| 676 |
else |
| 677 |
eraseKey := false; |
| 678 |
end; |
| 679 |
if eraseKey then |
| 680 |
Key := 0; |
| 681 |
inherited; |
| 682 |
end; |
| 683 |
|
| 684 |
procedure THexaColorPicker.Paint; |
| 685 |
begin |
| 686 |
PaintParentBack(Canvas); |
| 687 |
if FColorCombs = nil then |
| 688 |
CalculateCombLayout; |
| 689 |
DrawCombControls(Canvas); |
| 690 |
end; |
| 691 |
|
| 692 |
function THexaColorPicker.PtInComb(Comb: TCombEntry; P: TPoint; Scale: Integer): Boolean; |
| 693 |
begin |
| 694 |
Result := (Sqr(Comb.Position.X - P.X) + Sqr(Comb.Position.Y - P.Y)) <= Scale * Scale; |
| 695 |
end; |
| 696 |
|
| 697 |
procedure THexaColorPicker.DrawAll; |
| 698 |
var |
| 699 |
WinTop: integer; |
| 700 |
begin |
| 701 |
WinTop := - FRadius div 8; // use 10 instead of 8 if the top has been cut |
| 702 |
FCombSize := Round(1 + FRadius / (FLevels - 1)); |
| 703 |
FColorCombRect := Rect(0, WinTop, 2 * FRadius, 2 * FRadius + WinTop); |
| 704 |
FBWCombRect := Rect( |
| 705 |
FColorCombRect.Left, |
| 706 |
FColorCombRect.Bottom - 4, |
| 707 |
Round(17 * FCombSize * cos(Pi / 6) / 2) {%H-}+ 6 * FCombSize, |
| 708 |
FColorCombRect.Bottom + 2 * FCombSize |
| 709 |
); |
| 710 |
if FSliderVisible then |
| 711 |
FSliderRect := Rect(FColorCombRect.Right, FCombSize, FColorCombRect.Right + 10 + FSliderWidth, FColorCombRect.Bottom - FCombSize) |
| 712 |
// FSliderRect := Rect(FColorCombRect.Right, FColorCombRect.Top, FColorCombRect.Right + 10 + FSliderWidth, FColorCombRect.Bottom) |
| 713 |
else |
| 714 |
FSliderRect := Rect(-1, -1, -1, -1); |
| 715 |
end; |
| 716 |
|
| 717 |
// fills arrays with centers and colors for the custom color and black & white combs, |
| 718 |
// these arrays are used to quickly draw the combx and do hit tests |
| 719 |
|
| 720 |
function RGBFromFloat(Color: TRGBrec): COLORREF; |
| 721 |
begin |
| 722 |
Result := RGB(Round(255 * Color.Red), Round(255 * Color.Green), Round(255 * Color.Blue)); |
| 723 |
end; |
| 724 |
|
| 725 |
{function TRGBrecFromTColor(Color: TColor): TRGBrec; |
| 726 |
begin |
| 727 |
Result.Red := GetRValue(Color)/255; |
| 728 |
Result.Green := GetGValue(Color)/255; |
| 729 |
Result.Blue := GetBValue(Color)/255; |
| 730 |
end;} |
| 731 |
|
| 732 |
procedure THexaColorPicker.CalculateCombLayout; |
| 733 |
|
| 734 |
function GrayFromIntensity(Intensity: Byte): COLORREF; |
| 735 |
begin |
| 736 |
Result := RGB(Intensity, Intensity, Intensity); |
| 737 |
end; |
| 738 |
|
| 739 |
var |
| 740 |
I, J, Level, CurrentIndex, CombCount: Cardinal; |
| 741 |
CurrentColor: TRGBrec; |
| 742 |
CurrentPos: TFloatPoint; |
| 743 |
Scale: Extended; |
| 744 |
// triangle vars |
| 745 |
Pos1, Pos2, dPos1, dPos2, dPos: TFloatPoint; |
| 746 |
Color1, Color2, dColor1, dColor2, dColor: TRGBrec; |
| 747 |
begin |
| 748 |
// this ensures the radius and comb size is set correctly |
| 749 |
// HandleNeeded; |
| 750 |
if FLevels < 1 then FLevels := 1; |
| 751 |
// To draw perfectly aligned combs we split the final comb into six triangles (sextants) |
| 752 |
// and calculate each separately. The center comb is stored as first entry in the array |
| 753 |
// and will not considered twice (as with the other shared combs too). |
| 754 |
// |
| 755 |
// The way used here for calculation of the layout seems a bit complicated, but works |
| 756 |
// correctly for all cases (even if the comb corners are rotated). |
| 757 |
// initialization |
| 758 |
CurrentIndex := 0; |
| 759 |
CurrentColor := FCenterColor; |
| 760 |
// number of combs can be calculated by: |
| 761 |
// 1 level: 1 comb (the center) |
| 762 |
// 2 levels: 1 comb + 6 combs |
| 763 |
// 3 levels: 1 comb + 1 * 6 combs + 2 * 6 combs |
| 764 |
// n levels: 1 combs + 1 * 6 combs + 2 * 6 combs + .. + (n-1) * 6 combs |
| 765 |
// this equals to 1 + 6 * (1 + 2 + 3 + .. + (n-1)), by using Gauss' famous formula we get: |
| 766 |
// Count = 1 + 6 * (((n-1) * n) / 2) |
| 767 |
// Because there's always an even number involved (either n or n-1) we can use an integer div |
| 768 |
// instead of a float div here... |
| 769 |
CombCount := 1 + 6 * (((FLevels - 1) * FLevels) div 2); |
| 770 |
SetLength(FColorCombs, CombCount); |
| 771 |
// store center values |
| 772 |
FColorCombs[CurrentIndex].Position := Point(0, 0); |
| 773 |
FColorCombs[CurrentIndex].Color := RGBFromFloat(CurrentColor); |
| 774 |
Inc(CurrentIndex); |
| 775 |
// go out off here if there are not further levels to draw |
| 776 |
if FLevels < 2 then Exit; |
| 777 |
// now go for each sextant, the generic corners have been calculated already at creation |
| 778 |
// time for a comb with diameter 1 |
| 779 |
// ------ |
| 780 |
// /\ 1 /\ |
| 781 |
// / \ / \ |
| 782 |
// / 2 \/ 0 \ |
| 783 |
// ----------- |
| 784 |
// \ 3 /\ 5 / |
| 785 |
// \ / \ / |
| 786 |
// \/ 4 \/ |
| 787 |
// ------ |
| 788 |
for I := 0 to 5 do |
| 789 |
begin |
| 790 |
// initialize triangle corner values |
| 791 |
// |
| 792 |
// center (always at 0,0) |
| 793 |
// /\ |
| 794 |
// dPos1 / \ dPos2 |
| 795 |
// dColor1 / \ dColor2 |
| 796 |
// / dPos \ |
| 797 |
// /--------\ (span) |
| 798 |
// / dColor \ |
| 799 |
// /____________\ |
| 800 |
// comb corner 1 comb corner 2 |
| 801 |
// |
| 802 |
// Pos1, Pos2, Color1, Color2 are running terms for both sides of the triangle |
| 803 |
// incremented by dPos1/2 and dColor1/2. |
| 804 |
// dPos and dColor are used to interpolate a span between the values just mentioned. |
| 805 |
// |
| 806 |
// The small combs are actually oriented with corner 0 at top (i.e. mirrored at y = x, |
| 807 |
// compared with the values in FCombCorners), we can achieve that by simply exchanging |
| 808 |
// X and Y values. |
| 809 |
Scale := 2 * FRadius * cos(Pi / 6); |
| 810 |
Pos1.X := FCombCorners[I].Y * Scale; |
| 811 |
Pos1.Y := FCombCorners[I].X * Scale; |
| 812 |
Color1 := DefColors[I]; |
| 813 |
if I = 5 then |
| 814 |
begin |
| 815 |
Pos2.X := FCombCorners[0].Y * Scale; |
| 816 |
Pos2.Y := FCombCorners[0].X * Scale; |
| 817 |
Color2 := DefColors[0]; |
| 818 |
end |
| 819 |
else |
| 820 |
begin |
| 821 |
Pos2.X := FCombCorners[I + 1].Y * Scale; |
| 822 |
Pos2.Y := FCombCorners[I + 1].X * Scale; |
| 823 |
Color2 := DefColors[I + 1]; |
| 824 |
end; |
| 825 |
dPos1.X := Pos1.X / (FLevels - 1); |
| 826 |
dPos1.Y := Pos1.Y / (FLevels - 1); |
| 827 |
dPos2.X := Pos2.X / (FLevels - 1); |
| 828 |
dPos2.Y := Pos2.Y / (FLevels - 1); |
| 829 |
dColor1.Red := (Color1.Red - FCenterColor.Red) / (FLevels - 1); |
| 830 |
dColor1.Green := (Color1.Green - FCenterColor.Green) / (FLevels - 1); |
| 831 |
dColor1.Blue := (Color1.Blue - FCenterColor.Blue) / (FLevels - 1); |
| 832 |
|
| 833 |
dColor2.Red := (Color2.Red - FCenterColor.Red) / (FLevels - 1); |
| 834 |
dColor2.Green := (Color2.Green - FCenterColor.Green) / (FLevels - 1); |
| 835 |
dColor2.Blue := (Color2.Blue - FCenterColor.Blue) / (FLevels - 1); |
| 836 |
|
| 837 |
Pos1 := DefCenter; |
| 838 |
Pos2 := DefCenter; |
| 839 |
Color1 := FCenterColor; |
| 840 |
Color2 := FCenterColor; |
| 841 |
|
| 842 |
// Now that we have finished the initialization for this step we'll go |
| 843 |
// through a loop for each level to calculate the spans. |
| 844 |
// We can ignore level 0 (as this is the center we already have determined) as well |
| 845 |
// as the last step of each span (as this is the start value in the next triangle and will |
| 846 |
// be calculated there). We have, though, take them into the calculation of the running terms. |
| 847 |
for Level := 0 to FLevels - 1 do |
| 848 |
begin |
| 849 |
if Level > 0 then |
| 850 |
begin |
| 851 |
// initialize span values |
| 852 |
dPos.X := (Pos2.X - Pos1.X) / Level; |
| 853 |
dPos.Y := (Pos2.Y - Pos1.Y) / Level; |
| 854 |
dColor.Red := (Color2.Red - Color1.Red) / Level; |
| 855 |
dColor.Green := (Color2.Green - Color1.Green) / Level; |
| 856 |
dColor.Blue := (Color2.Blue - Color1.Blue) / Level; |
| 857 |
CurrentPos := Pos1; |
| 858 |
CurrentColor := Color1; |
| 859 |
for J := 0 to Level - 1 do |
| 860 |
begin |
| 861 |
// store current values in the array |
| 862 |
FColorCombs[CurrentIndex].Position.X := Round(CurrentPos.X); |
| 863 |
FColorCombs[CurrentIndex].Position.Y := Round(CurrentPos.Y); |
| 864 |
FColorCombs[CurrentIndex].Color := RGBFromFloat(CurrentColor); |
| 865 |
Inc(CurrentIndex); |
| 866 |
|
| 867 |
// advance in span |
| 868 |
CurrentPos.X := CurrentPos.X + dPos.X; |
| 869 |
CurrentPos.Y := CurrentPos.Y + dPos.Y; |
| 870 |
|
| 871 |
CurrentColor.Red := CurrentColor.Red + dColor.Red; |
| 872 |
CurrentColor.Green := CurrentColor.Green + dColor.Green; |
| 873 |
CurrentColor.Blue := CurrentColor.Blue + dColor.Blue; |
| 874 |
end; |
| 875 |
end; |
| 876 |
// advance running terms |
| 877 |
Pos1.X := Pos1.X + dPos1.X; |
| 878 |
Pos1.Y := Pos1.Y + dPos1.Y; |
| 879 |
Pos2.X := Pos2.X + dPos2.X; |
| 880 |
Pos2.Y := Pos2.Y + dPos2.Y; |
| 881 |
|
| 882 |
Color1.Red := Color1.Red + dColor1.Red; |
| 883 |
Color1.Green := Color1.Green + dColor1.Green; |
| 884 |
Color1.Blue := Color1.Blue + dColor1.Blue; |
| 885 |
|
| 886 |
Color2.Red := Color2.Red + dColor2.Red; |
| 887 |
Color2.Green := Color2.Green + dColor2.Green; |
| 888 |
Color2.Blue := Color2.Blue + dColor2.Blue; |
| 889 |
end; |
| 890 |
end; |
| 891 |
|
| 892 |
// second step is to build a list for the black & white area |
| 893 |
// 17 entries from pure white to pure black |
| 894 |
// the first and last are implicitely of double comb size |
| 895 |
SetLength(FBWCombs, 17); |
| 896 |
CurrentIndex := 0; |
| 897 |
FBWCombs[CurrentIndex].Color := GrayFromIntensity(255); |
| 898 |
FBWCombs[CurrentIndex].Position := Point(FCombSize, FCombSize); |
| 899 |
Inc(CurrentIndex); |
| 900 |
|
| 901 |
CurrentPos.X := 3 * FCombSize; |
| 902 |
CurrentPos.Y := 3 * (FCombSize div 4); |
| 903 |
dPos.X := Round(FCombSize * cos(Pi / 6) / 2); |
| 904 |
dPos.Y := Round(FCombSize * (1 + sin(Pi / 6)) / 2); |
| 905 |
for I := 0 to 14 do |
| 906 |
begin |
| 907 |
FBWCombs[CurrentIndex].Color := GrayFromIntensity((16 - CurrentIndex) * 15); |
| 908 |
if Odd(I) then |
| 909 |
FBWCombs[CurrentIndex].Position := Point(Round(CurrentPos.X + I * dPos.X), Round(CurrentPos.Y + dPos.Y)) |
| 910 |
else |
| 911 |
FBWCombs[CurrentIndex].Position := Point(Round(CurrentPos.X + I * dPos.X), Round(CurrentPos.Y)); |
| 912 |
Inc(CurrentIndex); |
| 913 |
end; |
| 914 |
FBWCombs[CurrentIndex].Color := 0; |
| 915 |
FBWCombs[CurrentIndex].Position := Point(Round(CurrentPos.X + 16 * dPos.X + FCombSize), FCombSize); |
| 916 |
EnumerateCombs; |
| 917 |
end; |
| 918 |
|
| 919 |
// determine hint message and out-of-hint rect |
| 920 |
procedure THexaColorPicker.CMHintShow( |
| 921 |
var Message: {$IFDEF FPC}TLMessage{$ELSE}TMessage{$ENDIF} ); |
| 922 |
var |
| 923 |
Index: Integer; |
| 924 |
Colors: TCombArray; |
| 925 |
cp: TPoint; |
| 926 |
begin |
| 927 |
Colors := nil; |
| 928 |
if (GetColorUnderCursor <> clNone) or PtInRect(FSliderRect, Point(mX, mY)) then |
| 929 |
with TCMHintShow(Message) do |
| 930 |
begin |
| 931 |
if not ShowHint then |
| 932 |
Message.Result := 1 |
| 933 |
else |
| 934 |
begin |
| 935 |
with HintInfo^ do |
| 936 |
begin |
| 937 |
// show that we want a hint |
| 938 |
Result := 0; |
| 939 |
cp := CursorPos; |
| 940 |
ReshowTimeout := 0; //1; |
| 941 |
HideTimeout := 5000; |
| 942 |
HintInfo^.CursorRect := Rect(cp.X, cp.Y, cp.X+1, cp.Y+1); |
| 943 |
if PtInRect(FSliderRect, cp) and FSliderVisible then |
| 944 |
begin |
| 945 |
// in case of the intensity slider we show the current intensity |
| 946 |
HintStr := FIntensityText + Format(': %d%%', [Round(100 * FCenterIntensity)]); |
| 947 |
HintPos := ClientToScreen(Point(FSliderRect.Right, CursorPos.Y - 8)); |
| 948 |
end |
| 949 |
else |
| 950 |
begin |
| 951 |
Index := -1; |
| 952 |
if PtInRect(FBWCombRect, Point(CursorPos.X, CursorPos.Y)) then |
| 953 |
begin |
| 954 |
// considering black&white area... |
| 955 |
if csLButtonDown in ControlState then |
| 956 |
Index := -(FCustomIndex + 1) |
| 957 |
else |
| 958 |
Index := FindBWArea(CursorPos.X, CursorPos.Y); |
| 959 |
Colors := FBWCombs; |
| 960 |
end |
| 961 |
else |
| 962 |
if PtInRect(FColorCombRect, Point(CursorPos.X, CursorPos.Y)) then |
| 963 |
begin |
| 964 |
// considering color comb area... |
| 965 |
if csLButtonDown in ControlState then |
| 966 |
Index := FCustomIndex - 1 |
| 967 |
else |
| 968 |
Index := FindColorArea(CursorPos.X, CursorPos.Y); |
| 969 |
Colors := FColorCombs; |
| 970 |
end; |
| 971 |
if (Index > -1) and (Colors <> nil) then |
| 972 |
HintStr := FormatHint(FHintFormat, Colors[Index].Color); |
| 973 |
end; |
| 974 |
end; |
| 975 |
end; |
| 976 |
end; |
| 977 |
end; |
| 978 |
|
| 979 |
procedure THexaColorPicker.SetSelectedColor(Value: TColor); |
| 980 |
begin |
| 981 |
FCurrentColor := Value; |
| 982 |
SelectColor(Value); |
| 983 |
Invalidate; |
| 984 |
end; |
| 985 |
|
| 986 |
procedure THexaColorPicker.EndSelection; |
| 987 |
begin |
| 988 |
if FCustomIndex < 0 then |
| 989 |
SetSelectedColor(FBWCombs[-(FCustomIndex + 1)].Color) |
| 990 |
else |
| 991 |
if FCustomIndex > 0 then |
| 992 |
SetSelectedColor(FColorCombs[FCustomIndex - 1].Color) |
| 993 |
else |
| 994 |
SetSelectedColor(clNone); |
| 995 |
end; |
| 996 |
|
| 997 |
function THexaColorPicker.GetColorUnderCursor: TColor; |
| 998 |
begin |
| 999 |
Result := FUnderCursor; |
| 1000 |
end; |
| 1001 |
|
| 1002 |
function THexaColorPicker.GetColorAtPoint(X, Y: integer): TColor; |
| 1003 |
var |
| 1004 |
Index: Integer; |
| 1005 |
Colors: TCombArray; |
| 1006 |
begin |
| 1007 |
Colors := nil; |
| 1008 |
Index := -1; |
| 1009 |
if PtInRect(FBWCombRect, Point(X, Y)) then |
| 1010 |
begin |
| 1011 |
Index := FindBWArea(X, Y); |
| 1012 |
Colors := FBWCombs; |
| 1013 |
end |
| 1014 |
else |
| 1015 |
if PtInRect(FColorCombRect, Point(X, Y)) then |
| 1016 |
begin |
| 1017 |
Index := FindColorArea(X, Y); |
| 1018 |
Colors := FColorCombs; |
| 1019 |
end; |
| 1020 |
if (Index > -1) and (Colors <> nil) then |
| 1021 |
Result := Colors[Index].Color |
| 1022 |
else |
| 1023 |
Result := clNone; |
| 1024 |
end; |
| 1025 |
|
| 1026 |
function THexaColorPicker.GetHexColorUnderCursor: string; |
| 1027 |
begin |
| 1028 |
Result := ColorToHex(GetColorUnderCursor); |
| 1029 |
end; |
| 1030 |
|
| 1031 |
function THexaColorPicker.GetHexColorAtPoint(X, Y: integer): string; |
| 1032 |
begin |
| 1033 |
Result := ColorToHex(GetColorAtPoint(X, Y)); |
| 1034 |
end; |
| 1035 |
|
| 1036 |
procedure THexaColorPicker.EnumerateCombs; |
| 1037 |
var |
| 1038 |
i, k: integer; |
| 1039 |
begin |
| 1040 |
k := 0; |
| 1041 |
if FBWCombs <> nil then |
| 1042 |
for i := 1 to High(FBWCombs) do |
| 1043 |
begin |
| 1044 |
case i of |
| 1045 |
// b & w comb indices |
| 1046 |
1: k := -1; |
| 1047 |
2: k := -9; |
| 1048 |
3: k := -2; |
| 1049 |
4: k := -10; |
| 1050 |
5: k := -3; |
| 1051 |
6: k := -11; |
| 1052 |
7: k := -4; |
| 1053 |
8: k := -12; |
| 1054 |
9: k := -5; |
| 1055 |
10: k := -13; |
| 1056 |
11: k := -6; |
| 1057 |
12: k := -14; |
| 1058 |
13: k := -7; |
| 1059 |
14: k := -15; |
| 1060 |
15: k := -8; |
| 1061 |
// big black comb index (match center comb) |
| 1062 |
16: K := 64; |
| 1063 |
end; |
| 1064 |
FBWCombs[i].TabIndex := k; |
| 1065 |
end; |
| 1066 |
if FColorCombs <> nil then |
| 1067 |
for i := 0 to High(FColorCombs) do |
| 1068 |
begin |
| 1069 |
case i of |
| 1070 |
// center comb index |
| 1071 |
0: k := 64; |
| 1072 |
// color comb indices |
| 1073 |
1: k := 65; |
| 1074 |
2: k := 66; |
| 1075 |
3: k := 78; |
| 1076 |
4: k := 67; |
| 1077 |
5: k := 79; |
| 1078 |
6: k := 90; |
| 1079 |
7: k := 68; |
| 1080 |
8: k := 80; |
| 1081 |
9: k := 91; |
| 1082 |
10: k := 101; |
| 1083 |
11: k := 69; |
| 1084 |
12: k := 81; |
| 1085 |
13: k := 92; |
| 1086 |
14: k := 102; |
| 1087 |
15: k := 111; |
| 1088 |
16: k := 70; |
| 1089 |
17: k := 82; |
| 1090 |
18: k := 93; |
| 1091 |
19: k := 103; |
| 1092 |
20: k := 112; |
| 1093 |
21: k := 120; |
| 1094 |
22: k := 77; |
| 1095 |
23: k := 89; |
| 1096 |
24: k := 88; |
| 1097 |
25: k := 100; |
| 1098 |
26: k := 99; |
| 1099 |
27: k := 98; |
| 1100 |
28: k := 110; |
| 1101 |
29: k := 109; |
| 1102 |
30: k := 108; |
| 1103 |
31: k := 107; |
| 1104 |
32: k := 119; |
| 1105 |
33: k := 118; |
| 1106 |
34: k := 117; |
| 1107 |
35: k := 116; |
| 1108 |
36: k := 115; |
| 1109 |
37: k := 127; |
| 1110 |
38: k := 126; |
| 1111 |
39: k := 125; |
| 1112 |
40: k := 124; |
| 1113 |
41: k := 123; |
| 1114 |
42: k := 122; |
| 1115 |
43: k := 76; |
| 1116 |
44: k := 87; |
| 1117 |
45: k := 75; |
| 1118 |
46: k := 97; |
| 1119 |
47: k := 86; |
| 1120 |
48: k := 74; |
| 1121 |
49: k := 106; |
| 1122 |
50: k := 96; |
| 1123 |
51: k := 85; |
| 1124 |
52: k := 73; |
| 1125 |
53: k := 114; |
| 1126 |
54: k := 105; |
| 1127 |
55: k := 95; |
| 1128 |
56: k := 84; |
| 1129 |
57: k := 72; |
| 1130 |
58: k := 121; |
| 1131 |
59: k := 113; |
| 1132 |
60: k := 104; |
| 1133 |
61: k := 94; |
| 1134 |
62: k := 83; |
| 1135 |
63: k := 71; |
| 1136 |
64: k := 63; |
| 1137 |
65: k := 62; |
| 1138 |
66: k := 50; |
| 1139 |
67: k := 61; |
| 1140 |
68: k := 49; |
| 1141 |
69: k := 38; |
| 1142 |
70: k := 60; |
| 1143 |
71: k := 48; |
| 1144 |
72: k := 37; |
| 1145 |
73: k := 27; |
| 1146 |
74: k := 59; |
| 1147 |
75: k := 47; |
| 1148 |
76: k := 36; |
| 1149 |
77: k := 26; |
| 1150 |
78: k := 17; |
| 1151 |
79: k := 58; |
| 1152 |
80: k := 46; |
| 1153 |
81: k := 35; |
| 1154 |
82: k := 25; |
| 1155 |
83: k := 16; |
| 1156 |
84: k := 8; |
| 1157 |
85: k := 51; |
| 1158 |
86: k := 39; |
| 1159 |
87: k := 40; |
| 1160 |
88: k := 28; |
| 1161 |
89: k := 29; |
| 1162 |
90: k := 30; |
| 1163 |
91: k := 18; |
| 1164 |
92: k := 19; |
| 1165 |
93: k := 20; |
| 1166 |
94: k := 21; |
| 1167 |
95: k := 9; |
| 1168 |
96: k := 10; |
| 1169 |
97: k := 11; |
| 1170 |
98: k := 12; |
| 1171 |
99: k := 13; |
| 1172 |
100: k := 1; |
| 1173 |
101: k := 2; |
| 1174 |
102: k := 3; |
| 1175 |
103: k := 4; |
| 1176 |
104: k := 5; |
| 1177 |
105: k := 6; |
| 1178 |
106: k := 52; |
| 1179 |
107: k := 41; |
| 1180 |
108: k := 53; |
| 1181 |
109: k := 31; |
| 1182 |
110: k := 42; |
| 1183 |
111: k := 54; |
| 1184 |
112: k := 22; |
| 1185 |
113: k := 32; |
| 1186 |
114: k := 43; |
| 1187 |
115: k := 55; |
| 1188 |
116: k := 14; |
| 1189 |
117: k := 23; |
| 1190 |
118: k := 33; |
| 1191 |
119: k := 44; |
| 1192 |
120: k := 56; |
| 1193 |
121: k := 7; |
| 1194 |
122: k := 15; |
| 1195 |
123: k := 24; |
| 1196 |
124: k := 34; |
| 1197 |
125: k := 45; |
| 1198 |
126: k := 57; |
| 1199 |
end; |
| 1200 |
FColorCombs[i].TabIndex := k; |
| 1201 |
end; |
| 1202 |
end; |
| 1203 |
|
| 1204 |
procedure THexaColorPicker.SelectCombIndex(i: integer); |
| 1205 |
var |
| 1206 |
j: integer; |
| 1207 |
begin |
| 1208 |
if i > 0 then |
| 1209 |
begin |
| 1210 |
if FColorCombs <> nil then |
| 1211 |
for j := 0 to High(FColorCombs) do |
| 1212 |
begin |
| 1213 |
if FColorCombs[j].TabIndex = i then |
| 1214 |
begin |
| 1215 |
SetSelectedColor(FColorCombs[j].Color); |
| 1216 |
Break; |
| 1217 |
end; |
| 1218 |
end; |
| 1219 |
end |
| 1220 |
else |
| 1221 |
if FBWCombs <> nil then |
| 1222 |
for j := 1 to High(FBWCombs) - 1 do |
| 1223 |
begin |
| 1224 |
if FBWCombs[j].TabIndex = i then |
| 1225 |
begin |
| 1226 |
SetSelectedColor(FBWCombs[j].Color); |
| 1227 |
Break; |
| 1228 |
end; |
| 1229 |
end; |
| 1230 |
end; |
| 1231 |
|
| 1232 |
procedure THexaColorPicker.Resize; |
| 1233 |
var |
| 1234 |
rw, rh: integer; |
| 1235 |
begin |
| 1236 |
if (Width >= 93) and (Height >= 85) then |
| 1237 |
begin |
| 1238 |
if FSliderVisible then |
| 1239 |
rw := Round((Width - 10 - FSliderWidth)/2) |
| 1240 |
else |
| 1241 |
rw := Round(Width/2 - 5); |
| 1242 |
rh := Round((24/53)*(Height - 6)); |
| 1243 |
SetRadius(Min(rw, rh)); |
| 1244 |
end; |
| 1245 |
inherited; |
| 1246 |
end; |
| 1247 |
|
| 1248 |
function THexaColorPicker.SelectAvailableColor(Color: TColor): boolean; |
| 1249 |
var |
| 1250 |
I: integer; |
| 1251 |
C: COLORREF; |
| 1252 |
found: Boolean; |
| 1253 |
begin |
| 1254 |
found := False; |
| 1255 |
Result := false; |
| 1256 |
C := ColorToRGB(Color); |
| 1257 |
if FColorCombs = nil then CalculateCombLayout; |
| 1258 |
FCustomIndex := 0; |
| 1259 |
FSelectedIndex := NoCell; |
| 1260 |
for I := 0 to High(FBWCombs) do |
| 1261 |
if FBWCombs[I].Color = C then |
| 1262 |
begin |
| 1263 |
FSelectedIndex := CustomCell; |
| 1264 |
FCustomIndex := -(I + 1); |
| 1265 |
found := True; |
| 1266 |
Result := true; |
| 1267 |
Break; |
| 1268 |
end; |
| 1269 |
if not found then |
| 1270 |
for I := 0 to High(FColorCombs) do |
| 1271 |
if FColorCombs[I].Color = C then |
| 1272 |
begin |
| 1273 |
FSelectedIndex := CustomCell; |
| 1274 |
FCustomIndex := I + 1; |
| 1275 |
Result := true; |
| 1276 |
Break; |
| 1277 |
end; |
| 1278 |
end; |
| 1279 |
|
| 1280 |
procedure THexaColorPicker.SelectColor(Color: TColor); |
| 1281 |
begin |
| 1282 |
SelectAvailableColor(Color); |
| 1283 |
Invalidate; |
| 1284 |
if Assigned(OnChange) then OnChange(Self); |
| 1285 |
end; |
| 1286 |
|
| 1287 |
procedure THexaColorPicker.SetIntensity(v: integer); |
| 1288 |
var |
| 1289 |
R: TRect; |
| 1290 |
begin |
| 1291 |
FCenterIntensity := EnsureRange(v/100, 0, 1); |
| 1292 |
FCenterColor.Red := DefCenterColor.Red * FCenterIntensity; |
| 1293 |
FCenterColor.Green := DefCenterColor.Green * FCenterIntensity; |
| 1294 |
FCenterColor.Blue := DefCenterColor.Blue * FCenterIntensity; |
| 1295 |
R := FSliderRect; |
| 1296 |
Dec(R.Top, 3); |
| 1297 |
Inc(R.Bottom, 3); |
| 1298 |
Inc(R.Left, 10); |
| 1299 |
InvalidateRect(Handle, @R, False); |
| 1300 |
FColorCombs := nil; |
| 1301 |
InvalidateRect(Handle, @FColorCombRect, False); |
| 1302 |
InvalidateRect(Handle, @FCustomColorRect, False); |
| 1303 |
CalculateCombLayout; |
| 1304 |
EndSelection; |
| 1305 |
if Assigned(FOnIntensityChange) then |
| 1306 |
FOnIntensityChange(Self); |
| 1307 |
end; |
| 1308 |
|
| 1309 |
procedure THexaColorPicker.SetMarker(Value: TMarker); |
| 1310 |
begin |
| 1311 |
if FMarker <> Value then |
| 1312 |
begin |
| 1313 |
FMarker := Value; |
| 1314 |
DrawAll; |
| 1315 |
CalculateCombLayout; |
| 1316 |
Invalidate; |
| 1317 |
end; |
| 1318 |
end; |
| 1319 |
|
| 1320 |
procedure THexaColorPicker.SetNewArrowStyle(Value: boolean); |
| 1321 |
begin |
| 1322 |
if FNewArrowStyle <> Value then |
| 1323 |
begin |
| 1324 |
FNewArrowStyle := Value; |
| 1325 |
DrawAll; |
| 1326 |
CalculateCombLayout; |
| 1327 |
Invalidate; |
| 1328 |
end; |
| 1329 |
end; |
| 1330 |
|
| 1331 |
procedure THexaColorPicker.SetRadius(r: integer); |
| 1332 |
begin |
| 1333 |
if Parent = nil then |
| 1334 |
exit; |
| 1335 |
FRadius := r; |
| 1336 |
DrawAll; |
| 1337 |
CalculateCombLayout; |
| 1338 |
Invalidate; |
| 1339 |
end; |
| 1340 |
|
| 1341 |
procedure THexaColorPicker.SetSliderVisible(Value: boolean); |
| 1342 |
begin |
| 1343 |
if FSliderVisible <> Value then |
| 1344 |
begin |
| 1345 |
FSliderVisible := Value; |
| 1346 |
DrawAll; |
| 1347 |
CalculateCombLayout; |
| 1348 |
Invalidate; |
| 1349 |
end; |
| 1350 |
end; |
| 1351 |
|
| 1352 |
procedure THexaColorPicker.SetSliderWidth(w: integer); |
| 1353 |
begin |
| 1354 |
if (FSliderWidth <> w) and FSliderVisible then |
| 1355 |
begin |
| 1356 |
FSliderWidth := w; |
| 1357 |
DrawAll; |
| 1358 |
Width := FSliderRect.Right + 2; |
| 1359 |
CalculateCombLayout; |
| 1360 |
Invalidate; |
| 1361 |
end; |
| 1362 |
end; |
| 1363 |
|
| 1364 |
procedure THexaColorPicker.WMLButtonDown( |
| 1365 |
var Message: {$IFDEF FPC}TLMLButtonDown{$ELSE}TWMLButtonDown{$ENDIF} ); |
| 1366 |
begin |
| 1367 |
inherited; |
| 1368 |
SetFocus; // needed so the key events work |
| 1369 |
if PtInRect(ClientRect, Point(Message.XPos, Message.YPos)) then |
| 1370 |
HandleCustomColors(Message); |
| 1371 |
end; |
| 1372 |
|
| 1373 |
procedure THexaColorPicker.WMLButtonUp( |
| 1374 |
var Message: {$IFDEF FPC}TLMLButtonUp{$ELSE}TWMLButtonUp{$ENDIF} ); |
| 1375 |
var |
| 1376 |
LastMode: TSelectionMode; |
| 1377 |
begin |
| 1378 |
inherited; |
| 1379 |
LastMode := FSelectionMode; |
| 1380 |
FSelectionMode := smNone; |
| 1381 |
if (FSelectedIndex = CustomCell) and (FCustomIndex <> 0) then |
| 1382 |
begin |
| 1383 |
if ((FSelectedIndex = CustomCell) and (LastMode in [smColor, smBW])) or |
| 1384 |
(FSelectedIndex <> NoCell) and (FSelectedIndex <> CustomCell) |
| 1385 |
then |
| 1386 |
EndSelection |
| 1387 |
end; |
| 1388 |
end; |
| 1389 |
|
| 1390 |
procedure THexaColorPicker.WMMouseMove( |
| 1391 |
var Message: {$IFDEF FPC}TLMMouseMove{$ELSE}TWMMouseMove{$ENDIF} ); |
| 1392 |
var |
| 1393 |
Shift: TShiftState; |
| 1394 |
Index: Integer; |
| 1395 |
Colors: TCombArray; |
| 1396 |
begin |
| 1397 |
inherited; |
| 1398 |
mX := Message.XPos; |
| 1399 |
mY := Message.YPos; |
| 1400 |
//get color under cursor |
| 1401 |
Colors := nil; |
| 1402 |
FUnderCursor := clNone; |
| 1403 |
if PtInRect(FBWCombRect, Point(Message.XPos, Message.YPos)) then |
| 1404 |
begin |
| 1405 |
Index := FindBWArea(Message.XPos, Message.YPos); |
| 1406 |
Colors := FBWCombs; |
| 1407 |
if (Index > -1) and (Colors <> nil) then |
| 1408 |
FUnderCursor := Colors[Index].Color; |
| 1409 |
end |
| 1410 |
else |
| 1411 |
if PtInRect(FColorCombRect, Point(Message.XPos, Message.YPos)) then |
| 1412 |
begin |
| 1413 |
Index := FindColorArea(Message.XPos, Message.YPos); |
| 1414 |
Colors := FColorCombs; |
| 1415 |
if (Index > -1) and (Colors <> nil) then |
| 1416 |
FUnderCursor := Colors[Index].Color; |
| 1417 |
end |
| 1418 |
else |
| 1419 |
FUnderCursor := clNone; |
| 1420 |
// further process message |
| 1421 |
Shift := KeysToShiftState(Message.Keys); |
| 1422 |
if ssLeft in Shift then |
| 1423 |
HandleCustomColors(Message); |
| 1424 |
end; |
| 1425 |
|
| 1426 |
end. |