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