| 1 |
unit BAxisColorPicker; |
| 2 |
|
| 3 |
{$IFDEF FPC} |
| 4 |
{$MODE DELPHI} |
| 5 |
{$ENDIF} |
| 6 |
|
| 7 |
interface |
| 8 |
|
| 9 |
uses |
| 10 |
LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics, |
| 11 |
HTMLColors, mbColorPickerControl; |
| 12 |
|
| 13 |
type |
| 14 |
TBAxisColorPicker = class(TmbColorPickerControl) |
| 15 |
private |
| 16 |
FR, FG, FB: integer; |
| 17 |
procedure SetRValue(r: integer); |
| 18 |
procedure SetGValue(g: 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 clBlue; |
| 38 |
property Red: integer read FR write SetRValue default 0; |
| 39 |
property Green: integer read FG write SetGValue default 0; |
| 40 |
property Blue: integer read FB write SetBValue default 255; |
| 41 |
property MarkerStyle default msCircle; |
| 42 |
property OnChange; |
| 43 |
end; |
| 44 |
|
| 45 |
|
| 46 |
implementation |
| 47 |
|
| 48 |
uses |
| 49 |
Math, mbUtils; |
| 50 |
|
| 51 |
|
| 52 |
{TBAxisColorPicker} |
| 53 |
|
| 54 |
constructor TBAxisColorPicker.Create(AOwner: TComponent); |
| 55 |
begin |
| 56 |
inherited; |
| 57 |
FGradientWidth := 256; |
| 58 |
FGradientHeight := 256; |
| 59 |
SetInitialBounds(0, 0, 255, 255); |
| 60 |
HintFormat := 'R: %r G: %g'#13'Hex: %hex'; |
| 61 |
FG := 0; |
| 62 |
FB := 255; |
| 63 |
FR := 0; |
| 64 |
FSelected := clBlue; |
| 65 |
MarkerStyle := msCircle; |
| 66 |
end; |
| 67 |
|
| 68 |
procedure TBAxisColorPicker.CorrectCoords(var x, y: integer); |
| 69 |
begin |
| 70 |
Clamp(x, 0, Width - 1); |
| 71 |
Clamp(y, 0, Height - 1); |
| 72 |
end; |
| 73 |
|
| 74 |
procedure TBAxisColorPicker.CreateWnd; |
| 75 |
begin |
| 76 |
inherited; |
| 77 |
CreateGradient; |
| 78 |
end; |
| 79 |
|
| 80 |
procedure TBAxisColorPicker.DrawMarker(x, y: integer); |
| 81 |
var |
| 82 |
c: TColor; |
| 83 |
begin |
| 84 |
CorrectCoords(x, y); |
| 85 |
FR := GetRValue(FSelected); |
| 86 |
FG := GetGValue(FSelected); |
| 87 |
FB := GetBValue(FSelected); |
| 88 |
if Focused or (csDesigning in ComponentState) then |
| 89 |
c := clBlack |
| 90 |
else |
| 91 |
c := clWhite; |
| 92 |
InternalDrawMarker(x, y, c); |
| 93 |
end; |
| 94 |
|
| 95 |
function TBAxisColorPicker.GetColorAtPoint(x, y: Integer): TColor; |
| 96 |
var |
| 97 |
r, g: Integer; |
| 98 |
begin |
| 99 |
r := round(x / (Width - 1) * 255); |
| 100 |
g := 255 - round(y / (Height - 1) * 255); |
| 101 |
Result := RGBtoColor(r, g, FB); |
| 102 |
end; |
| 103 |
|
| 104 |
{ x is RED, y is GREEN } |
| 105 |
function TBAxisColorPicker.GetGradientColor2D(x, y: Integer): TColor; |
| 106 |
begin |
| 107 |
Result := RGB(x, FBufferBmp.Height - 1 - y, FB); |
| 108 |
end; |
| 109 |
|
| 110 |
procedure TBAxisColorPicker.KeyDown(var Key: Word; Shift: TShiftState); |
| 111 |
var |
| 112 |
eraseKey: Boolean; |
| 113 |
delta: Integer; |
| 114 |
begin |
| 115 |
eraseKey := true; |
| 116 |
delta := IfThen(ssCtrl in Shift, 10, 1); |
| 117 |
|
| 118 |
case Key of |
| 119 |
VK_LEFT : SelectColor(mx - delta, my); |
| 120 |
VK_RIGHT : SelectColor(mx + delta, my); |
| 121 |
VK_UP : SelectColor(mx, my - delta); |
| 122 |
VK_DOWN : SelectColor(mx, my + delta); |
| 123 |
else eraseKey := false; |
| 124 |
end; |
| 125 |
|
| 126 |
if eraseKey then |
| 127 |
Key := 0; |
| 128 |
|
| 129 |
inherited; |
| 130 |
end; |
| 131 |
|
| 132 |
procedure TBAxisColorPicker.MouseDown(Button: TMouseButton; Shift: TShiftState; |
| 133 |
X, Y: Integer); |
| 134 |
begin |
| 135 |
inherited; |
| 136 |
if Button = mbLeft then |
| 137 |
SelectColor(x, y); |
| 138 |
SetFocus; |
| 139 |
end; |
| 140 |
|
| 141 |
procedure TBAxisColorPicker.MouseMove(Shift: TShiftState; X, Y: Integer); |
| 142 |
begin |
| 143 |
inherited; |
| 144 |
if ssLeft in Shift then |
| 145 |
SelectColor(x, y); |
| 146 |
end; |
| 147 |
|
| 148 |
procedure TBAxisColorPicker.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); |
| 149 |
begin |
| 150 |
inherited; |
| 151 |
if Button = mbLeft then |
| 152 |
SelectColor(x, y); |
| 153 |
end; |
| 154 |
|
| 155 |
procedure TBAxisColorPicker.Paint; |
| 156 |
begin |
| 157 |
Canvas.StretchDraw(ClientRect, FBufferBmp); |
| 158 |
DrawMarker(mx, my); |
| 159 |
end; |
| 160 |
|
| 161 |
procedure TBAxisColorPicker.Resize; |
| 162 |
begin |
| 163 |
mx := round(FR * Width / 255); |
| 164 |
my := round((255 - FG) * Height / 255); |
| 165 |
inherited; |
| 166 |
end; |
| 167 |
|
| 168 |
procedure TBAxisColorPicker.SelectColor(x, y: Integer); |
| 169 |
var |
| 170 |
c: TColor; |
| 171 |
r, g, b: Integer; |
| 172 |
needNewGradient: Boolean; |
| 173 |
begin |
| 174 |
CorrectCoords(x, y); |
| 175 |
mx := x; |
| 176 |
my := y; |
| 177 |
c := GetColorAtPoint(x, y); |
| 178 |
if c = FSelected then |
| 179 |
exit; |
| 180 |
FSelected := c; |
| 181 |
r := GetRValue(c); |
| 182 |
g := GetGValue(c); |
| 183 |
b := GetBValue(c); |
| 184 |
needNewGradient := b <> FB; |
| 185 |
FR := r; |
| 186 |
FG := g; |
| 187 |
FB := b; |
| 188 |
if needNewGradient then |
| 189 |
CreateGradient; |
| 190 |
Invalidate; |
| 191 |
DoChange; |
| 192 |
end; |
| 193 |
|
| 194 |
procedure TBAxisColorPicker.SetBValue(b: integer); |
| 195 |
begin |
| 196 |
Clamp(b, 0, 255); |
| 197 |
if b <> FB then |
| 198 |
SetSelectedColor(RGBToColor(FR, FG, b)); |
| 199 |
end; |
| 200 |
|
| 201 |
procedure TBAxisColorPicker.SetGValue(g: integer); |
| 202 |
begin |
| 203 |
Clamp(g, 0, 255); |
| 204 |
SetSelectedColor(RGBtoColor(FR, g, FB)); |
| 205 |
end; |
| 206 |
|
| 207 |
procedure TBAxisColorPicker.SetRValue(r: integer); |
| 208 |
begin |
| 209 |
Clamp(r, 0, 255); |
| 210 |
SetSelectedColor(RGBtoColor(r, FG, FB)); |
| 211 |
end; |
| 212 |
|
| 213 |
procedure TBAxisColorPicker.SetSelectedColor(c: TColor); |
| 214 |
var |
| 215 |
r, g, b: Integer; |
| 216 |
needNewGradient: Boolean; |
| 217 |
begin |
| 218 |
if WebSafe then |
| 219 |
c := GetWebSafe(c); |
| 220 |
if c = FSelected then |
| 221 |
exit; |
| 222 |
|
| 223 |
r := GetRValue(c); |
| 224 |
g := GetGValue(c); |
| 225 |
b := GetBValue(c); |
| 226 |
needNewGradient := (b <> FB); |
| 227 |
FR := r; |
| 228 |
FG := g; |
| 229 |
FB := b; |
| 230 |
FSelected := c; |
| 231 |
mx := Round(FR * Width / 255); // RED is on x |
| 232 |
my := Round((255 - FG) * Height / 255); // GREEN is on y |
| 233 |
if needNewGradient then |
| 234 |
CreateGradient; |
| 235 |
Invalidate; |
| 236 |
DoChange; |
| 237 |
end; |
| 238 |
|
| 239 |
end. |