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