| 1 |
unit CIEAColorPicker; |
| 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, RGBCIEUtils, mbColorPickerControl; |
| 12 |
|
| 13 |
type |
| 14 |
TCIEAColorPicker = class(TmbColorPickerControl) |
| 15 |
private |
| 16 |
FL, FA, FB: integer; |
| 17 |
procedure SetLValue(l: integer); |
| 18 |
procedure SetAValue(a: integer); |
| 19 |
procedure SetBValue(b: integer); |
| 20 |
protected |
| 21 |
procedure CorrectCoords(var x, y: integer); |
| 22 |
procedure CreateWnd; override; |
| 23 |
procedure DrawMarker(x, y: integer); |
| 24 |
function GetGradientColor2D(x, y: Integer): TColor; override; |
| 25 |
procedure KeyDown(var Key: Word; Shift: TShiftState); override; |
| 26 |
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; |
| 27 |
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override; |
| 28 |
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; |
| 29 |
procedure Paint; override; |
| 30 |
procedure Resize; override; |
| 31 |
procedure SelectColor(x, y: Integer); |
| 32 |
procedure SetSelectedColor(c: TColor); override; |
| 33 |
public |
| 34 |
constructor Create(AOwner: TComponent); override; |
| 35 |
function GetColorAtPoint(x, y: Integer): TColor; override; |
| 36 |
published |
| 37 |
property SelectedColor default clFuchsia; |
| 38 |
property LValue: integer read FL write SetLValue default 100; |
| 39 |
property AValue: integer read FA write SetAValue default 127; |
| 40 |
property BValue: integer read FB write SetBValue default -128; |
| 41 |
property MarkerStyle default msCircle; |
| 42 |
property OnChange; |
| 43 |
end; |
| 44 |
|
| 45 |
|
| 46 |
implementation |
| 47 |
|
| 48 |
uses |
| 49 |
Math, mbUtils; |
| 50 |
|
| 51 |
{TCIEAColorPicker} |
| 52 |
|
| 53 |
constructor TCIEAColorPicker.Create(AOwner: TComponent); |
| 54 |
begin |
| 55 |
inherited; |
| 56 |
FGradientWidth := 256; |
| 57 |
FGradientHeight := 256; |
| 58 |
SetInitialBounds(0, 0, 256, 256); |
| 59 |
HintFormat := 'L: %cieL B: %cieB'#13'Hex: %hex'; |
| 60 |
FSelected := clFuchsia; |
| 61 |
FL := 100; |
| 62 |
FA := 127; |
| 63 |
FB := -128; |
| 64 |
MarkerStyle := msCircle; |
| 65 |
end; |
| 66 |
|
| 67 |
procedure TCIEAColorPicker.CorrectCoords(var x, y: integer); |
| 68 |
begin |
| 69 |
Clamp(x, 0, Width - 1); |
| 70 |
Clamp(y, 0, Height - 1); |
| 71 |
end; |
| 72 |
|
| 73 |
procedure TCIEAColorPicker.CreateWnd; |
| 74 |
begin |
| 75 |
inherited; |
| 76 |
CreateGradient; |
| 77 |
end; |
| 78 |
|
| 79 |
procedure TCIEAColorPicker.DrawMarker(x, y: integer); |
| 80 |
var |
| 81 |
c: TColor; |
| 82 |
begin |
| 83 |
CorrectCoords(x, y); |
| 84 |
FL := Round(GetCIELValue(FSelected)); |
| 85 |
FA := Round(GetCIEAValue(FSelected)); |
| 86 |
FB := Round(GetCIEBValue(FSelected)); |
| 87 |
if Focused or (csDesigning in ComponentState) then |
| 88 |
c := clBlack |
| 89 |
else |
| 90 |
c := clWhite; |
| 91 |
InternalDrawMarker(x, y, c); |
| 92 |
end; |
| 93 |
|
| 94 |
{ |
| 95 |
function TCIEAColorPicker.GetColorAtPoint(x, y: Integer): TColor; |
| 96 |
var |
| 97 |
l, a, b: Integer; |
| 98 |
begin |
| 99 |
l := round(100 * (1 - y / (Height-1))); |
| 100 |
a := FA; |
| 101 |
b := round(255 * (x / (Width - 1))) - 128; |
| 102 |
Result := LabToRGB(l, a, b); |
| 103 |
end; |
| 104 |
} |
| 105 |
function TCIEAColorPicker.GetColorAtPoint(x, y: Integer): TColor; |
| 106 |
var |
| 107 |
l, b: Integer; |
| 108 |
begin |
| 109 |
l := round((1 - y / (Height - 1)) * 100); |
| 110 |
b := round((x / (Width - 1) - 0.5) * 255); |
| 111 |
Result := LabToRGB(l, FA, b); |
| 112 |
end; |
| 113 |
|
| 114 |
// In the original code: for L ... for B ... LabToRGB(Round(100-L*100/255), FA, B-128); |
| 115 |
// --> x is B, y is L |
| 116 |
function TCIEAColorPicker.GetGradientColor2D(x, y: Integer): TColor; |
| 117 |
begin |
| 118 |
Result := LabToRGB(Round(100 - y*100/255), FA, x - 128); |
| 119 |
end; |
| 120 |
|
| 121 |
procedure TCIEAColorPicker.KeyDown(var Key: Word; Shift: TShiftState); |
| 122 |
var |
| 123 |
eraseKey: Boolean; |
| 124 |
delta: Integer; |
| 125 |
begin |
| 126 |
eraseKey := true; |
| 127 |
delta := IfThen(ssCtrl in Shift, 10, 1); |
| 128 |
|
| 129 |
case Key of |
| 130 |
VK_LEFT : SelectColor(mx - delta, my); |
| 131 |
VK_RIGHT : SelectColor(mx + delta, my); |
| 132 |
VK_UP : SelectColor(mx, my - delta); |
| 133 |
VK_DOWN : SelectColor(mx, my + delta); |
| 134 |
else eraseKey := false; |
| 135 |
end; |
| 136 |
|
| 137 |
if eraseKey then |
| 138 |
Key := 0; |
| 139 |
|
| 140 |
inherited; |
| 141 |
end; |
| 142 |
|
| 143 |
procedure TCIEAColorPicker.MouseDown(Button: TMouseButton; Shift: TShiftState; |
| 144 |
X, Y: Integer); |
| 145 |
begin |
| 146 |
inherited; |
| 147 |
if Button = mbLeft then |
| 148 |
SelectColor(x, y); |
| 149 |
SetFocus; |
| 150 |
end; |
| 151 |
|
| 152 |
procedure TCIEAColorPicker.MouseMove(Shift: TShiftState; X, Y: Integer); |
| 153 |
begin |
| 154 |
inherited; |
| 155 |
if ssLeft in Shift then |
| 156 |
SelectColor(X, Y); |
| 157 |
end; |
| 158 |
|
| 159 |
procedure TCIEAColorPicker.MouseUp(Button: TMouseButton; Shift: TShiftState; |
| 160 |
X, Y: Integer); |
| 161 |
begin |
| 162 |
inherited; |
| 163 |
if Button = mbLeft then |
| 164 |
SelectColor(X, Y); |
| 165 |
end; |
| 166 |
|
| 167 |
procedure TCIEAColorPicker.Paint; |
| 168 |
begin |
| 169 |
Canvas.StretchDraw(ClientRect, FBufferBmp); |
| 170 |
DrawMarker(mx, my); |
| 171 |
end; |
| 172 |
|
| 173 |
procedure TCIEAColorPicker.Resize; |
| 174 |
begin |
| 175 |
mx := Round((FB + 128) / 255 * Width); |
| 176 |
// myy := Round(((100 - FL) * 255 / 100) * Height / 255); |
| 177 |
my := Round((100 - FL) / 100 * Height); |
| 178 |
inherited; |
| 179 |
end; |
| 180 |
|
| 181 |
procedure TCIEAColorPicker.SelectColor(x, y: Integer); |
| 182 |
var |
| 183 |
c: TColor; |
| 184 |
L, a, b: Integer; |
| 185 |
needNewGradient: Boolean; |
| 186 |
begin |
| 187 |
CorrectCoords(x, y); |
| 188 |
c := GetColorAtPoint(x, y); |
| 189 |
if WebSafe then |
| 190 |
c := GetWebSafe(c); |
| 191 |
if c = FSelected then |
| 192 |
exit; |
| 193 |
|
| 194 |
mx := x; |
| 195 |
my := y; |
| 196 |
L := Round(GetCIELValue(c)); |
| 197 |
a := Round(GetCIEAValue(c)); |
| 198 |
b := Round(GetCIEBValue(c)); |
| 199 |
needNewGradient := a <> FA; |
| 200 |
FSelected := c; |
| 201 |
FL := L; |
| 202 |
FA := a; |
| 203 |
FB := b; |
| 204 |
if needNewGradient then |
| 205 |
CreateGradient; |
| 206 |
Invalidate; |
| 207 |
DoChange; |
| 208 |
end; |
| 209 |
|
| 210 |
procedure TCIEAColorPicker.SetAValue(a: integer); |
| 211 |
begin |
| 212 |
Clamp(a, -128, 127); |
| 213 |
FA := a; |
| 214 |
SetSelectedColor(LabToRGB(FL, FA, FB)); |
| 215 |
end; |
| 216 |
|
| 217 |
procedure TCIEAColorPicker.SetBValue(b: integer); |
| 218 |
begin |
| 219 |
Clamp(b, -128, 127); |
| 220 |
FB := b; |
| 221 |
SetSelectedColor(LabToRGB(FL, FA, FB)); |
| 222 |
end; |
| 223 |
|
| 224 |
procedure TCIEAColorPicker.SetLValue(l: integer); |
| 225 |
begin |
| 226 |
Clamp(L, 0, 100); |
| 227 |
FL := L; |
| 228 |
SetSelectedColor(LabToRGB(FL, FA, FB)); |
| 229 |
end; |
| 230 |
|
| 231 |
procedure TCIEAColorPicker.SetSelectedColor(c: TColor); |
| 232 |
var |
| 233 |
l, a, b: Integer; |
| 234 |
needNewGradient: Boolean; |
| 235 |
begin |
| 236 |
if WebSafe then |
| 237 |
c := GetWebSafe(c); |
| 238 |
if c = FSelected then |
| 239 |
exit; |
| 240 |
|
| 241 |
l := Round(GetCIELValue(c)); |
| 242 |
a := Round(GetCIEAValue(c)); |
| 243 |
b := Round(GetCIEBValue(c)); |
| 244 |
needNewGradient := a <> FA; |
| 245 |
FL := l; |
| 246 |
FA := a; |
| 247 |
FB := b; |
| 248 |
FSelected := c; |
| 249 |
mx := Round((FB + 128) * Width / 255); |
| 250 |
// my := Round((100 - FL) * 255 / 100 * Height / 255); |
| 251 |
my := Round((100 - FL) / 100 * Height); |
| 252 |
if needNewGradient then |
| 253 |
CreateGradient; |
| 254 |
Invalidate; |
| 255 |
DoChange; |
| 256 |
end; |
| 257 |
|
| 258 |
end. |