| 1 |
unit HRingPicker; |
| 2 |
|
| 3 |
{$IFDEF FPC} |
| 4 |
{$MODE DELPHI} |
| 5 |
{$ENDIF} |
| 6 |
|
| 7 |
interface |
| 8 |
|
| 9 |
uses |
| 10 |
LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics, Math, Forms, |
| 11 |
HTMLColors, mbColorConv, mbColorPickerControl; |
| 12 |
|
| 13 |
type |
| 14 |
THRingPicker = class(TmbHSLVColorPickerControl) |
| 15 |
private |
| 16 |
FSelectedColor: TColor; |
| 17 |
FHueLineColor: TColor; |
| 18 |
FRadius: integer; |
| 19 |
procedure SetRadius(r: integer); |
| 20 |
procedure SetHueLineColor(c: TColor); |
| 21 |
protected |
| 22 |
procedure CreateGradient; override; |
| 23 |
procedure DrawHueLine; |
| 24 |
function GetGradientColor2D(X, Y: Integer): TColor; override; |
| 25 |
function GetSelectedColor: TColor; override; |
| 26 |
// function MouseOnPicker(X, Y: Integer): Boolean; |
| 27 |
procedure Paint; override; |
| 28 |
procedure Resize; override; |
| 29 |
procedure SelectColor(x, y: integer); override; |
| 30 |
procedure SetRelHue(H: Double); override; |
| 31 |
procedure SetSelectedColor(c: TColor); override; |
| 32 |
procedure UpdateCoords; |
| 33 |
public |
| 34 |
constructor Create(AOwner: TComponent); override; |
| 35 |
function GetColorAtPoint(x, y: integer): TColor; override; |
| 36 |
property ColorUnderCursor; |
| 37 |
published |
| 38 |
property Hue default 0; |
| 39 |
property Luminance default 127; |
| 40 |
property Saturation default 255; |
| 41 |
property Value default 255; |
| 42 |
property MaxHue default 360; |
| 43 |
property MaxLuminance default 255; |
| 44 |
property MaxSaturation default 255; |
| 45 |
property MaxValue default 255; |
| 46 |
property HueLineColor: TColor read FHueLineColor write SetHueLineColor default clGray; |
| 47 |
property Radius: integer read FRadius write SetRadius default 40; |
| 48 |
property SelectedColor default clRed; //clNone; |
| 49 |
property OnChange; |
| 50 |
end; |
| 51 |
|
| 52 |
|
| 53 |
implementation |
| 54 |
|
| 55 |
uses |
| 56 |
mbUtils; |
| 57 |
|
| 58 |
{ THRingPicker } |
| 59 |
|
| 60 |
constructor THRingPicker.Create(AOwner: TComponent); |
| 61 |
begin |
| 62 |
inherited; |
| 63 |
SetInitialBounds(0, 0, 204, 204); |
| 64 |
FHue := 0.0; |
| 65 |
FVal := 1.0; |
| 66 |
FLum := 0.5; |
| 67 |
FSat := 1.0; |
| 68 |
SetSelectedColor(clRed); |
| 69 |
FRadius := 40; |
| 70 |
FHueLineColor := clGray; |
| 71 |
HintFormat := 'Hue: %h (selected)'; |
| 72 |
TabStop := true; |
| 73 |
end; |
| 74 |
|
| 75 |
procedure THRingPicker.CreateGradient; |
| 76 |
begin |
| 77 |
FGradientWidth := Min(Width, Height); |
| 78 |
FGradientHeight := FGradientWidth; |
| 79 |
inherited; |
| 80 |
end; |
| 81 |
|
| 82 |
procedure THRingPicker.DrawHueLine; |
| 83 |
var |
| 84 |
angle: double; |
| 85 |
sinAngle, cosAngle: Double; |
| 86 |
radius: integer; |
| 87 |
begin |
| 88 |
radius := Min(Width, Height) div 2; |
| 89 |
if (FHue >= 0) and (FHue <= 1.0) then |
| 90 |
begin |
| 91 |
angle := -FHue * TWO_PI; |
| 92 |
SinCos(angle, sinAngle, cosAngle); |
| 93 |
Canvas.Pen.Color := FHueLineColor; |
| 94 |
Canvas.MoveTo(radius, radius); |
| 95 |
Canvas.LineTo(radius + round(radius*cosAngle), radius + round(radius*sinAngle)); |
| 96 |
end; |
| 97 |
end; |
| 98 |
|
| 99 |
function THRingPicker.GetColorAtPoint(x, y: integer): TColor; |
| 100 |
var |
| 101 |
angle: Double; |
| 102 |
dx, dy, radius: integer; |
| 103 |
h: Double; |
| 104 |
begin |
| 105 |
radius := Min(Width, Height) div 2; |
| 106 |
|
| 107 |
if PointInCircle(Point(x, y), Min(Width, Height)) then |
| 108 |
begin |
| 109 |
dx := x - Radius; |
| 110 |
dy := y - Radius; |
| 111 |
angle := 360 + 180 * arctan2(-dy, dx) / pi; |
| 112 |
if angle < 0 then |
| 113 |
angle := angle + 360 |
| 114 |
else if angle > 360 then |
| 115 |
angle := angle - 360; |
| 116 |
h := angle / 360; |
| 117 |
Result := HSLVtoColor(h, FSat, FLum, FVal); |
| 118 |
end |
| 119 |
else |
| 120 |
Result := clNone; |
| 121 |
end; |
| 122 |
|
| 123 |
{ Outer loop: Y, Inner loop: X } |
| 124 |
function THRingPicker.GetGradientColor2D(X, Y: Integer): TColor; |
| 125 |
var |
| 126 |
dx, dy: Integer; |
| 127 |
dSq, rSq: Integer; |
| 128 |
radius, size: Integer; |
| 129 |
H: Double; |
| 130 |
begin |
| 131 |
size := FGradientWidth; // or Height, they are the same... |
| 132 |
radius := size div 2; |
| 133 |
rSq := sqr(radius); |
| 134 |
dx := X - radius; |
| 135 |
dy := Y - radius; |
| 136 |
dSq := sqr(dx) + sqr(dy); |
| 137 |
if dSq <= rSq then |
| 138 |
begin |
| 139 |
H := 180 * (1 + arctan2(dx, dy) / pi); // wp: order (x,y) is correct! |
| 140 |
H := H + 90; |
| 141 |
if H > 360 then H := H - 360; |
| 142 |
Result := HSLVtoColor(H/360, FSat, FLum, FVal); |
| 143 |
if WebSafe then |
| 144 |
Result := GetWebSafe(Result); |
| 145 |
end else |
| 146 |
Result := GetDefaultColor(dctBrush); |
| 147 |
end; |
| 148 |
|
| 149 |
function THRingPicker.GetSelectedColor: TColor; |
| 150 |
begin |
| 151 |
if FSelectedColor <> clNone then |
| 152 |
Result := HSLVtoColor(FHue, FSat, FLum, FVal) |
| 153 |
else |
| 154 |
Result := clNone; |
| 155 |
end; |
| 156 |
{ |
| 157 |
function THRingPicker.MouseOnPicker(X, Y: Integer): Boolean; |
| 158 |
var |
| 159 |
diameter, r: Integer; |
| 160 |
P, ctr: TPoint; |
| 161 |
begin |
| 162 |
diameter := Min(Width, Height); |
| 163 |
r := diameter div 2; // outer radius |
| 164 |
P := Point(x, y); |
| 165 |
ctr := Point(r, r); |
| 166 |
Result := PtInCircle(P, ctr, r) and not PtInCircle(P, ctr, Radius); |
| 167 |
end; } |
| 168 |
|
| 169 |
procedure THRingPicker.Paint; |
| 170 |
var |
| 171 |
rgn, r1, r2: HRGN; |
| 172 |
r: TRect; |
| 173 |
size: Integer; |
| 174 |
ringwidth: Integer; |
| 175 |
begin |
| 176 |
PaintParentBack(Canvas); |
| 177 |
size := Min(Width, Height); // diameter of circle |
| 178 |
ringwidth := size div 2 - FRadius; // FRadius is inner radius |
| 179 |
r := ClientRect; |
| 180 |
r.Right := R.Left + size; |
| 181 |
R.Bottom := R.Top + size; |
| 182 |
InflateRect(R, -1, -1); // Remove spurious black pixels at the border |
| 183 |
r1 := CreateEllipticRgnIndirect(R); |
| 184 |
if ringwidth > 0 then |
| 185 |
begin |
| 186 |
rgn := r1; |
| 187 |
InflateRect(R, -ringwidth, - ringwidth); |
| 188 |
r2 := CreateEllipticRgnIndirect(R); |
| 189 |
CombineRgn(rgn, r1, r2, RGN_DIFF); |
| 190 |
end; |
| 191 |
SelectClipRgn(Canvas.Handle, rgn); |
| 192 |
Canvas.Draw(0, 0, FBufferBmp); |
| 193 |
DeleteObject(rgn); |
| 194 |
DrawHueLine; |
| 195 |
DoChange; |
| 196 |
end; |
| 197 |
|
| 198 |
procedure THRingPicker.Resize; |
| 199 |
begin |
| 200 |
inherited; |
| 201 |
if Min(Width, Height) <> FGradientWidth then |
| 202 |
CreateGradient; |
| 203 |
UpdateCoords; |
| 204 |
end; |
| 205 |
|
| 206 |
procedure THRingPicker.SelectColor(x, y: integer); |
| 207 |
var |
| 208 |
angle, dx, dy, Radius: integer; |
| 209 |
begin |
| 210 |
mx := y; |
| 211 |
my := y; |
| 212 |
FSelectedColor := clWhite; |
| 213 |
radius := Min(Width, Height) div 2; |
| 214 |
dx := x - radius; |
| 215 |
dy := y - radius; |
| 216 |
angle := round(360 + 180*arctan2(-dy, dx) / pi); |
| 217 |
SetRelHue(angle/360); |
| 218 |
end; |
| 219 |
|
| 220 |
procedure THRingPicker.SetHueLineColor(c: TColor); |
| 221 |
begin |
| 222 |
if FHueLineColor <> c then |
| 223 |
begin |
| 224 |
FHueLineColor := c; |
| 225 |
Invalidate; |
| 226 |
end; |
| 227 |
end; |
| 228 |
|
| 229 |
procedure THRingPicker.SetRadius(r: integer); |
| 230 |
begin |
| 231 |
if FRadius <> r then |
| 232 |
begin |
| 233 |
FRadius := r; |
| 234 |
Invalidate; |
| 235 |
end; |
| 236 |
end; |
| 237 |
|
| 238 |
procedure THRingPicker.SetRelHue(H: Double); |
| 239 |
begin |
| 240 |
if H > 1 then H := H - 1; |
| 241 |
if H < 0 then H := H + 1; |
| 242 |
if FHue <> h then |
| 243 |
begin |
| 244 |
FHue := h; |
| 245 |
UpdateCoords; |
| 246 |
Invalidate; |
| 247 |
DoChange; |
| 248 |
end; |
| 249 |
end; |
| 250 |
|
| 251 |
(* |
| 252 |
procedure THRingPicker.SetSat(s: integer); |
| 253 |
begin |
| 254 |
Clamp(s, 0, FMaxSat); |
| 255 |
if Saturation <> s then |
| 256 |
begin |
| 257 |
FSat := s / FMaxSat; |
| 258 |
UpdateCoords; |
| 259 |
Invalidate; |
| 260 |
DoChange; |
| 261 |
end; |
| 262 |
end; |
| 263 |
*) |
| 264 |
procedure THRingPicker.SetSelectedColor(c: TColor); |
| 265 |
var |
| 266 |
H: Double = 0; |
| 267 |
S: Double = 0; |
| 268 |
L: Double = 0; |
| 269 |
V: Double = 0; |
| 270 |
needNewGradient: Boolean; |
| 271 |
begin |
| 272 |
if WebSafe then |
| 273 |
c := GetWebSafe(c); |
| 274 |
if c = GetSelectedColor then |
| 275 |
Exit; |
| 276 |
|
| 277 |
ColorToHSLV(c, H, S, L, V); |
| 278 |
case BrightnessMode of |
| 279 |
bmLuminance: |
| 280 |
begin |
| 281 |
needNewGradient := (S <> FSat) or (L <> FLum); |
| 282 |
FLum := L; |
| 283 |
end; |
| 284 |
bmValue: |
| 285 |
begin |
| 286 |
needNewGradient := (S <> FSat) or (V <> FVal); |
| 287 |
FVal := V; |
| 288 |
end; |
| 289 |
end; |
| 290 |
FHue := h; |
| 291 |
FSat := s; |
| 292 |
UpdateCoords; |
| 293 |
if needNewGradient then |
| 294 |
CreateGradient; |
| 295 |
Invalidate; |
| 296 |
DoChange; |
| 297 |
end; |
| 298 |
|
| 299 |
(* |
| 300 |
procedure THRingPicker.SetVal(v: integer); |
| 301 |
begin |
| 302 |
Clamp(v, 0, FMaxVal); |
| 303 |
if Value <> V then |
| 304 |
begin |
| 305 |
FVal := V / FMaxVal; |
| 306 |
if BrightnessMode = bmValue then |
| 307 |
begin |
| 308 |
CreateGradient; |
| 309 |
Invalidate; |
| 310 |
end; |
| 311 |
DoChange; |
| 312 |
end; |
| 313 |
end; |
| 314 |
*) |
| 315 |
procedure THRingPicker.UpdateCoords; |
| 316 |
var |
| 317 |
r, angle: double; |
| 318 |
radius: integer; |
| 319 |
sinAngle, cosAngle: Double; |
| 320 |
begin |
| 321 |
radius := Min(Width, Height) div 2; |
| 322 |
r := -radius * FSat; |
| 323 |
angle := -(FHue * 2 + 1) * pi; |
| 324 |
SinCos(angle, sinAngle, cosAngle); |
| 325 |
mx := round(cosAngle * r) + radius; |
| 326 |
my := round(sinAngle * r) + radius; |
| 327 |
end; |
| 328 |
|
| 329 |
end. |