mbColorLib: Beginning to add MaxHue, MaxSaturation and MaxValue properties to HSV ColorPickers

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5535 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2016-12-17 21:35:57 +00:00
parent ecb22f64c6
commit 8763264447
6 changed files with 375 additions and 182 deletions

View File

@ -18,14 +18,21 @@ uses
type type
THColorPicker = class(TmbTrackBarPicker) THColorPicker = class(TmbTrackBarPicker)
private private
FVal, FSat, FHue: integer; FVal, FSat, FHue: double;
FMaxVal, FMaxSat, FMaxHue: Integer;
function ArrowPosFromHue(h: integer): integer; function ArrowPosFromHue(h: integer): integer;
function HueFromArrowPos(p: integer): integer; function HueFromArrowPos(p: integer): integer;
function GetHue: Integer;
function GetSat: Integer;
function GetVal: Integer;
function GetSelectedColor: TColor; function GetSelectedColor: TColor;
procedure SetSelectedColor(c: TColor); procedure SetSelectedColor(c: TColor);
procedure SetHue(h: integer); procedure SetHue(h: integer);
procedure SetMaxHue(h: Integer);
procedure SetMaxSat(s: Integer);
procedure SetMaxVal(v: Integer);
procedure SetSat(s: integer); procedure SetSat(s: integer);
procedure SetValue(v: integer); procedure SetVal(v: integer);
protected protected
procedure Execute(tbaAction: integer); override; procedure Execute(tbaAction: integer); override;
function GetArrowPos: integer; override; function GetArrowPos: integer; override;
@ -34,9 +41,12 @@ type
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
published published
property Hue: integer read FHue write SetHue default 0; property Hue: integer read GetHue write SetHue;
property Saturation: integer read FSat write SetSat default 255; property Saturation: integer read GetSat write SetSat;
property Value: integer read FVal write SetValue default 255; property Value: integer read GetVal write SetVal;
property MaxHue: Integer read FMaxHue write SetMaxHue default 359;
property MaxSaturation: Integer read FMaxSat write SetMaxSat default 255;
property MaxValue: Integer read FMaxVal write SetMaxVal default 255;
property SelectedColor: TColor read GetSelectedColor write SetSelectedColor default clRed; property SelectedColor: TColor read GetSelectedColor write SetSelectedColor default clRed;
end; end;
@ -51,11 +61,13 @@ uses
constructor THColorPicker.Create(AOwner: TComponent); constructor THColorPicker.Create(AOwner: TComponent);
begin begin
inherited; inherited;
FGradientWidth := 360; FMaxHue := 359;
FMaxSat := 255;
FMaxVal := 255;
FGradientWidth := FMaxHue + 1;
FGradientHeight := 12; FGradientHeight := 12;
FSat := 255; FSat := 1.0;
FVal := 255; FVal := 1.0;
FArrowPos := ArrowPosFromHue(0);
FChange := false; FChange := false;
SetHue(0); SetHue(0);
HintFormat := 'Hue: %value (selected)'; HintFormat := 'Hue: %value (selected)';
@ -64,30 +76,35 @@ begin
end; end;
function THColorPicker.GetGradientColor(AValue: Integer): TColor; function THColorPicker.GetGradientColor(AValue: Integer): TColor;
var
h: Double;
begin begin
if Layout = lyVertical then AValue := 360 - AValue; if Layout = lyVertical then AValue := (FMaxHue + 1) - AValue;
Result := HSVtoColor(AValue, FSat, FVal); h := AValue / (FMaxHue + 1);
Result := HSVtoColor(h, FSat, FVal);
end; end;
procedure THColorPicker.SetValue(v: integer); function THColorPicker.GetHue: Integer;
begin begin
Clamp(v, 0, 255); Result := round(FHue * FMaxHue);
if FVal <> v then end;
begin
FVal := v; function THColorPicker.GetSat: Integer;
FManual := false; begin
CreateGradient; Result := round(FSat * FMaxSat);
Invalidate; end;
if FChange and Assigned(OnChange) then OnChange(Self);
end; function THColorPicker.GetVal: Integer;
begin
Result := round(FVal * FMaxVal);
end; end;
procedure THColorPicker.SetHue(h: integer); procedure THColorPicker.SetHue(h: integer);
begin begin
Clamp(h, 0, 360); Clamp(h, 0, FMaxHue);
if FHue <> h then if GetHue <> h then
begin begin
FHue := h; FHue := h / FMaxHue;
FArrowPos := ArrowPosFromHue(h); FArrowPos := ArrowPosFromHue(h);
FManual := false; FManual := false;
Invalidate; Invalidate;
@ -95,12 +112,56 @@ begin
end; end;
end; end;
procedure THColorPicker.SetMaxHue(h: Integer);
begin
if h = FMaxHue then
exit;
FMaxHue := h;
FGradientWidth := FMaxHue + 1; // 0 .. FMaxHue --> FMaxHue + 1 pixels
CreateGradient;
Invalidate;
if FChange and Assigned(OnChange) then OnChange(Self);
end;
procedure THColorPicker.SetMaxSat(s: Integer);
begin
if s = FMaxSat then
exit;
FMaxSat := s;
CreateGradient;
Invalidate;
if FChange and Assigned(OnChange) then OnChange(Self);
end;
procedure THColorPicker.SetMaxVal(v: Integer);
begin
if v = FMaxVal then
exit;
FMaxVal := v;
CreateGradient;
Invalidate;
if FChange and Assigned(OnChange) then OnChange(Self);
end;
procedure THColorPicker.SetSat(s: integer); procedure THColorPicker.SetSat(s: integer);
begin begin
Clamp(s, 0, 255); Clamp(s, 0, FMaxSat);
if FSat <> s then if GetSat() <> s then
begin begin
FSat := s; FSat := s / FMaxSat;
FManual := false;
CreateGradient;
Invalidate;
if FChange and Assigned(OnChange) then OnChange(Self);
end;
end;
procedure THColorPicker.SetVal(v: integer);
begin
Clamp(v, 0, FMaxVal);
if GetVal() <> v then
begin
FVal := v / FMaxVal;
FManual := false; FManual := false;
CreateGradient; CreateGradient;
Invalidate; Invalidate;
@ -114,12 +175,12 @@ var
begin begin
if Layout = lyHorizontal then if Layout = lyHorizontal then
begin begin
a := Round(((Width - 12)/360)*h); a := Round((Width - 12) * h / FMaxHue);
if a > Width - FLimit then a := Width - FLimit; if a > Width - FLimit then a := Width - FLimit;
end end
else else
begin begin
a := Round(((Height - 12)/360)*h); a := Round((Height - 12) * h / FMaxHue);
if a > Height - FLimit then a := Height - FLimit; if a > Height - FLimit then a := Height - FLimit;
end; end;
if a < 0 then a := 0; if a < 0 then a := 0;
@ -131,24 +192,23 @@ var
r: integer; r: integer;
begin begin
if Layout = lyHorizontal then if Layout = lyHorizontal then
r := Round(p/((Width - 12)/360)) r := Round(p / (Width - 12) * FMaxHue)
else else
r := Round(p/((Height - 12)/360)); r := Round(p / (Height - 12) * MaxHue);
Clamp(r, 0, 360); Clamp(r, 0, FMaxHue);
Result := r; Result := r;
end; end;
function THColorPicker.GetSelectedColor: TColor; function THColorPicker.GetSelectedColor: TColor;
begin begin
if not WebSafe then Result := HSVtoColor(FHue, FSat, FVal);
Result := HSVtoColor(FHue, FSat, FVal) if WebSafe then
else Result := GetWebSafe(Result);
Result := GetWebSafe(HSVtoColor(FHue, FSat, FVal));
end; end;
function THColorPicker.GetSelectedValue: integer; function THColorPicker.GetSelectedValue: integer;
begin begin
Result := FHue; Result := GetHue();
end; end;
procedure THColorPicker.SetSelectedColor(c: TColor); procedure THColorPicker.SetSelectedColor(c: TColor);
@ -156,11 +216,11 @@ var
h, s, v: integer; h, s, v: integer;
begin begin
if WebSafe then c := GetWebSafe(c); if WebSafe then c := GetWebSafe(c);
RGBToHSV(GetRValue(c), GetGValue(c), GetBValue(c), h, s, v); RGBToHSVRange(GetRValue(c), GetGValue(c), GetBValue(c), h, s, v);
FChange := false; FChange := false;
SetHue(h); SetHue(h);
SetSat(s); SetSat(s);
SetValue(v); SetVal(v);
FManual := false; FManual := false;
FChange := true; FChange := true;
if Assigned(OnChange) then OnChange(Self); if Assigned(OnChange) then OnChange(Self);
@ -168,40 +228,43 @@ end;
function THColorPicker.GetArrowPos: integer; function THColorPicker.GetArrowPos: integer;
begin begin
Result := ArrowPosFromHue(FHue); if FMaxHue = 0 then
Result := inherited GetArrowPos
else
Result := ArrowPosFromHue(GetHue());
end; end;
procedure THColorPicker.Execute(tbaAction: integer); procedure THColorPicker.Execute(tbaAction: integer);
begin begin
case tbaAction of case tbaAction of
TBA_Resize: TBA_Resize:
SetHue(FHue); SetHue(GetHue);
TBA_MouseMove: TBA_MouseMove:
FHue := HueFromArrowPos(FArrowPos); Hue := HueFromArrowPos(FArrowPos);
TBA_MouseDown: TBA_MouseDown:
FHue := HueFromArrowPos(FArrowPos); Hue := HueFromArrowPos(FArrowPos);
TBA_MouseUp: TBA_MouseUp:
FHue := HueFromArrowPos(FArrowPos); Hue := HueFromArrowPos(FArrowPos);
TBA_WheelUp: TBA_WheelUp:
SetHue(FHue + Increment); SetHue(GetHue() + Increment);
TBA_WheelDown: TBA_WheelDown:
SetHue(FHue - Increment); SetHue(GetHue() - Increment);
TBA_VKLeft: TBA_VKLeft:
SetHue(FHue - Increment); SetHue(GetHue() - Increment);
TBA_VKCtrlLeft: TBA_VKCtrlLeft:
SetHue(0); SetHue(0);
TBA_VKRight: TBA_VKRight:
SetHue(FHue + Increment); SetHue(GetHue() + Increment);
TBA_VKCtrlRight: TBA_VKCtrlRight:
SetHue(360); SetHue(FMaxHue);
TBA_VKUp: TBA_VKUp:
SetHue(FHue - Increment); SetHue(GetHue() - Increment);
TBA_VKCtrlUp: TBA_VKCtrlUp:
SetHue(0); SetHue(0);
TBA_VKDown: TBA_VKDown:
SetHue(FHue + Increment); SetHue(GetHue() + Increment);
TBA_VKCtrlDown: TBA_VKCtrlDown:
SetHue(360); SetHue(FMaxHue);
else else
inherited; inherited;
end; end;

View File

@ -14,46 +14,26 @@ uses
{$ENDIF} {$ENDIF}
SysUtils, Classes, Graphics, Math, Scanlines; SysUtils, Classes, Graphics, Math, Scanlines;
function RGBtoRGBTriple(R, G, B: byte): TRGBTriple; procedure RGBtoHSV(R, G, B: Integer; out H, S, V: Double);
function RGBtoRGBQuad(R, G, B: byte): TRGBQuad; procedure RGBtoHSVRange(R, G, B: integer; out H, S, V: integer);
function RGBTripleToColor(Triple: TRGBTriple): TColor;
procedure RGBToHSV(R,G,B: integer; var H,S,V: integer); procedure HSVtoRGB(H, S, V: Double; out R, G, B: Integer);
procedure HSVtoRGBRange(H, S, V: Integer; out R, G, B: Integer);
function HSVtoRGBTriple(H, S, V: integer): TRGBTriple; function HSVtoRGBTriple(H, S, V: integer): TRGBTriple;
function HSVtoRGBQuad(H, S, V: integer): TRGBQuad; function HSVtoRGBQuad(H, S, V: integer): TRGBQuad;
function HSVtoColor(H, S, V: integer): TColor;
function HSVtoColor(H, S, V: Double): TColor;
function HSVRangeToColor(H, S, V: Integer): TColor;
function GetHValue(Color: TColor): integer; function GetHValue(Color: TColor): integer;
function GetVValue(Color: TColor): integer; function GetVValue(Color: TColor): integer;
function GetSValue(Color: TColor): integer; function GetSValue(Color: TColor): integer;
implementation implementation
function RGBtoRGBTriple(R, G, B: byte): TRGBTriple; procedure RGBToHSVRange(R, G, B: integer; out H, S, V: integer);
begin
with Result do
begin
rgbtRed := R;
rgbtGreen := G;
rgbtBlue := B;
end
end;
function RGBtoRGBQuad(R, G, B: byte): TRGBQuad;
begin
with Result do
begin
rgbRed := R;
rgbGreen := G;
rgbBlue := B;
rgbReserved := 0;
end
end;
function RGBTripleToColor(Triple: TRGBTriple): TColor;
begin
Result := RGB(Triple.rgbtRed, Triple.rgbtGreen, Triple.rgbtBlue);
end;
procedure RGBToHSV(R, G, B: integer; var H, S, V: integer);
var var
Delta, Min, H1, S1: double; Delta, Min, H1, S1: double;
begin begin
@ -79,6 +59,31 @@ begin
s := round(s1*255); s := round(s1*255);
end; end;
procedure RGBToHSV(R, G, B: Integer; out H, S, V: Double);
var
hh, ss, vv: Integer;
begin
RGBtoHSVRange(R, G, B, hh, ss, vv);
H := H / 360;
S := S / 255;
V := V / 255;
end;
procedure HSVtoRGB(H, S, V: Double; out R, G, B: Integer);
begin
HSVtoRGBRange(round(H*360), round(S*255), round(V*255), R, G, B);
end;
procedure HSVtoRGBRange(H, S, V: Integer; out R, G, B: Integer);
var
t: TRGBTriple;
begin
t := HSVtoRGBTriple(H, S, V);
R := t.rgbtRed;
G := t.rgbtGreen;
B := t.rgbtBlue;
end;
function HSVtoRGBTriple(H, S, V: integer): TRGBTriple; function HSVtoRGBTriple(H, S, V: integer): TRGBTriple;
const const
divisor: integer = 255*60; divisor: integer = 255*60;
@ -141,30 +146,35 @@ begin
end; end;
end; end;
function HSVtoColor(H, S, V: integer): TColor; function HSVRangetoColor(H, S, V: integer): TColor;
begin begin
Result := RGBTripleToColor(HSVtoRGBTriple(H, S, V)); Result := RGBTripleToColor(HSVtoRGBTriple(H, S, V));
end; end;
function HSVtoColor(H, S, V: Double): TColor;
begin
Result := HSVRangeToColor(round(H*360), round(S*255), round(V*255));
end;
function GetHValue(Color: TColor): integer; function GetHValue(Color: TColor): integer;
var var
s, v: integer; s, v: integer;
begin begin
RGBToHSV(GetRValue(Color), GetGValue(Color), GetBValue(Color), Result, s, v); RGBToHSVRange(GetRValue(Color), GetGValue(Color), GetBValue(Color), Result, s, v);
end; end;
function GetSValue(Color: TColor): integer; function GetSValue(Color: TColor): integer;
var var
h, v: integer; h, v: integer;
begin begin
RGBToHSV(GetRValue(Color), GetGValue(Color), GetBValue(Color), h, Result, v); RGBToHSVRange(GetRValue(Color), GetGValue(Color), GetBValue(Color), h, Result, v);
end; end;
function GetVValue(Color: TColor): integer; function GetVValue(Color: TColor): integer;
var var
h, s: integer; h, s: integer;
begin begin
RGBToHSV(GetRValue(Color), GetGValue(Color), GetBValue(Color), h, s, Result); RGBToHSVRange(GetRValue(Color), GetGValue(Color), GetBValue(Color), h, s, Result);
end; end;
end. end.

View File

@ -18,14 +18,21 @@ uses
type type
TSColorPicker = class(TmbTrackBarPicker) TSColorPicker = class(TmbTrackBarPicker)
private private
FVal, FHue, FSat: integer; FVal, FHue, FSat: Double;
FMaxVal, FMaxHue, FMaxSat: Integer;
function ArrowPosFromSat(s: integer): integer; function ArrowPosFromSat(s: integer): integer;
function SatFromArrowPos(p: integer): integer; function SatFromArrowPos(p: integer): integer;
function GetHue: Integer;
function GetSat: Integer;
function GetVal: Integer;
function GetSelectedColor: TColor; function GetSelectedColor: TColor;
procedure SetSelectedColor(c: TColor); procedure SetSelectedColor(c: TColor);
procedure SetHue(h: integer); procedure SetHue(h: integer);
procedure SetSat(s: integer); procedure SetSat(s: integer);
procedure SetValue(v: integer); procedure SetValue(v: integer);
procedure SetMaxHue(h: Integer);
procedure SetMaxSat(s: Integer);
procedure SetMaxVal(v: Integer);
protected protected
procedure Execute(tbaAction: integer); override; procedure Execute(tbaAction: integer); override;
function GetArrowPos: integer; override; function GetArrowPos: integer; override;
@ -34,9 +41,12 @@ type
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
published published
property Hue: integer read FHue write SetHue default 0; property Hue: integer read GetHue write SetHue;
property Saturation: integer read FSat write SetSat default 255; property Saturation: integer read GetSat write SetSat;
property Value: integer read FVal write SetValue default 255; property Value: integer read GetVal write SetValue;
property MaxHue: Integer read FMaxHue write SetMaxHue default 359;
property MaxSaturation: Integer read FMaxSat write SetMaxSat default 255;
property MaxValue: Integer read FMaxVal write SetMaxVal default 255;
property SelectedColor: TColor read GetSelectedColor write SetSelectedColor default clRed; property SelectedColor: TColor read GetSelectedColor write SetSelectedColor default clRed;
end; end;
@ -51,13 +61,15 @@ uses
constructor TSColorPicker.Create(AOwner: TComponent); constructor TSColorPicker.Create(AOwner: TComponent);
begin begin
inherited; inherited;
FGradientWidth := 256; FMaxHue := 359;
FMaxSat := 255;
FMaxVal := 255;
FGradientWidth := FMaxSat + 1;
FGradientHeight := 12; FGradientHeight := 12;
FHue := 0;
FVal := 255;
FArrowPos := ArrowPosFromSat(0);
FChange := false; FChange := false;
SetSat(255); FHue := 0;
FVal := 1.0;
SetSat(FMaxSat);
HintFormat := 'Saturation: %value (selected)'; HintFormat := 'Saturation: %value (selected)';
FManual := false; FManual := false;
FChange := true; FChange := true;
@ -111,15 +123,15 @@ end;
function TSColorPicker.GetGradientColor(AValue: Integer): TColor; function TSColorPicker.GetGradientColor(AValue: Integer): TColor;
begin begin
Result := HSVtoColor(FHue, AValue, FVal); Result := HSVtoColor(FHue, AValue/FMaxSat, FVal);
end; end;
procedure TSColorPicker.SetValue(v: integer); procedure TSColorPicker.SetValue(v: integer);
begin begin
Clamp(v, 0, 255); Clamp(v, 0, FMaxVal);
if FVal <> v then if GetVal() <> v then
begin begin
FVal := v; FVal := v / FMaxVal;
FManual := false; FManual := false;
CreateGradient; CreateGradient;
Invalidate; Invalidate;
@ -129,10 +141,10 @@ end;
procedure TSColorPicker.SetHue(h: integer); procedure TSColorPicker.SetHue(h: integer);
begin begin
Clamp(h, 0, 360); Clamp(h, 0, FMaxHue);
if FHue <> h then if GetHue() <> h then
begin begin
FHue := h; FHue := h / FMaxHue;
CreateGradient; CreateGradient;
FManual := false; FManual := false;
Invalidate; Invalidate;
@ -142,10 +154,10 @@ end;
procedure TSColorPicker.SetSat(s: integer); procedure TSColorPicker.SetSat(s: integer);
begin begin
Clamp(s, 0, 255); Clamp(s, 0, FMaxSat);
if FSat <> s then if GetSat() <> s then
begin begin
FSat := s; FSat := s / FMaxSat;
FManual := false; FManual := false;
FArrowPos := ArrowPosFromSat(s); FArrowPos := ArrowPosFromSat(s);
Invalidate; Invalidate;
@ -159,13 +171,13 @@ var
begin begin
if Layout = lyHorizontal then if Layout = lyHorizontal then
begin begin
a := Round(((Width - 12)/255)*s); a := Round(s / FMaxSat * (Width - 12));
if a > Width - FLimit then a := Width - FLimit; if a > Width - FLimit then a := Width - FLimit;
end end
else else
begin begin
s := 255 - s; s := FMaxSat - s;
a := Round(((Height - 12)/255)*s); a := Round(s / FMaxSat * (Height - 12));
if a > Height - FLimit then a := Height - FLimit; if a > Height - FLimit then a := Height - FLimit;
end; end;
if a < 0 then a := 0; if a < 0 then a := 0;
@ -177,24 +189,69 @@ var
r: integer; r: integer;
begin begin
if Layout = lyHorizontal then if Layout = lyHorizontal then
r := Round(p/((Width - 12)/255)) r := Round(p / (Width - 12) * FMaxSat)
else else
r := Round(255 - p/((Height - 12)/255)); r := Round(FMaxSat - p / (Height - 12) * FMaxSat);
Clamp(r, 0, 255); Clamp(r, 0, FMaxSat);
Result := r; Result := r;
end; end;
function TSColorPicker.GetHue: Integer;
begin
Result := round(FHue * FMaxHue);
end;
function TSColorPicker.GetSat: Integer;
begin
Result := round(FSat * FMaxSat);
end;
function TSColorPicker.GetVal: Integer;
begin
Result := round(FVal * FMaxVal);
end;
procedure TSColorPicker.SetMaxHue(h: Integer);
begin
if h = FMaxHue then
exit;
FMaxHue := h;
CreateGradient;
if FChange and Assigned(OnChange) then OnChange(Self);
Invalidate;
end;
procedure TSColorPicker.SetMaxSat(s: Integer);
begin
if s = FMaxSat then
exit;
FMaxSat := s;
FGradientWidth := FMaxSat + 1;
CreateGradient;
if FChange and Assigned(OnChange) then OnChange(Self);
Invalidate;
end;
procedure TSColorPicker.SetMaxVal(v: Integer);
begin
if v = FMaxVal then
exit;
FMaxVal := v;
CreateGradient;
if FChange and Assigned(OnChange) then OnChange(Self);
Invalidate;
end;
function TSColorPicker.GetSelectedColor: TColor; function TSColorPicker.GetSelectedColor: TColor;
begin begin
if not WebSafe then Result := HSVToColor(FHue, FSat, FVal);
Result := HSVtoColor(FHue, FSat, FVal) if WebSafe then
else Result := GetWebSafe(Result);
Result := GetWebSafe(HSVtoColor(FHue, FSat, FVal));
end; end;
function TSColorPicker.GetSelectedValue: integer; function TSColorPicker.GetSelectedValue: integer;
begin begin
Result := FSat; Result := GetSat();
end; end;
procedure TSColorPicker.SetSelectedColor(c: TColor); procedure TSColorPicker.SetSelectedColor(c: TColor);
@ -202,7 +259,7 @@ var
h, s, v: integer; h, s, v: integer;
begin begin
if WebSafe then c := GetWebSafe(c); if WebSafe then c := GetWebSafe(c);
RGBToHSV(GetRValue(c), GetGValue(c), GetBValue(c), h, s, v); RGBToHSVRange(GetRValue(c), GetGValue(c), GetBValue(c), h, s, v);
FChange := false; FChange := false;
SetHue(h); SetHue(h);
SetSat(s); SetSat(s);
@ -214,38 +271,41 @@ end;
function TSColorPicker.GetArrowPos: integer; function TSColorPicker.GetArrowPos: integer;
begin begin
Result := ArrowPosFromSat(FSat); if FMaxSat = 0 then
Result := inherited GetArrowPos
else
Result := ArrowPosFromSat(GetSat());
end; end;
procedure TSColorPicker.Execute(tbaAction: integer); procedure TSColorPicker.Execute(tbaAction: integer);
begin begin
case tbaAction of case tbaAction of
TBA_Resize: TBA_Resize:
SetSat(FSat); SetSat(GetSat());
TBA_MouseMove: TBA_MouseMove:
FSat := SatFromArrowPos(FArrowPos); SetSat(SatFromArrowPos(FArrowPos));
TBA_MouseDown: TBA_MouseDown:
FSat := SatFromArrowPos(FArrowPos); SetSat(SatFromArrowPos(FArrowPos));
TBA_MouseUp: TBA_MouseUp:
FSat := SatFromArrowPos(FArrowPos); SetSat(SatFromArrowPos(FArrowPos));
TBA_WheelUp: TBA_WheelUp:
SetSat(FSat + Increment); SetSat(GetSat() + Increment);
TBA_WheelDown: TBA_WheelDown:
SetSat(FSat - Increment); SetSat(GetSat() - Increment);
TBA_VKLeft: TBA_VKLeft:
SetSat(FSat - Increment); SetSat(GetSat() - Increment);
TBA_VKCtrlLeft: TBA_VKCtrlLeft:
SetSat(0); SetSat(0);
TBA_VKRight: TBA_VKRight:
SetSat(FSat + Increment); SetSat(GetSat() + Increment);
TBA_VKCtrlRight: TBA_VKCtrlRight:
SetSat(255); SetSat(FMaxSat);
TBA_VKUp: TBA_VKUp:
SetSat(FSat + Increment); SetSat(GetSat() + Increment);
TBA_VKCtrlUp: TBA_VKCtrlUp:
SetSat(255); SetSat(FMaxSat);
TBA_VKDown: TBA_VKDown:
SetSat(FSat - Increment); SetSat(GetSat() - Increment);
TBA_VKCtrlDown: TBA_VKCtrlDown:
SetSat(0); SetSat(0);
else else

View File

@ -251,9 +251,9 @@ var
begin begin
triple := HSLToRGBTriple(FHue, FSat, FLum); triple := HSLToRGBTriple(FHue, FSat, FLum);
if not WebSafe then if not WebSafe then
Result := RGBTripleToTColor(triple) Result := RGBTripleToColor(triple)
else else
Result := GetWebSafe(RGBTripleToTColor(triple)); Result := GetWebSafe(RGBTripleToColor(triple));
end; end;
function TSLColorPicker.GetColorAtPoint(x, y: integer): TColor; function TSLColorPicker.GetColorAtPoint(x, y: integer): TColor;
@ -262,9 +262,9 @@ var
begin begin
triple := HSLToRGBTriple(FHue, MulDiv(255, x, Width), MulDiv(255, Height - y, Height)); triple := HSLToRGBTriple(FHue, MulDiv(255, x, Width), MulDiv(255, Height - y, Height));
if not WebSafe then if not WebSafe then
Result := RGBTripleToTColor(triple) Result := RGBTripleToColor(triple)
else else
Result := GetWebSafe(RGBTripleToTColor(triple)); Result := GetWebSafe(RGBTripleToColor(triple));
end; end;
procedure TSLColorPicker.CNKeyDown( procedure TSLColorPicker.CNKeyDown(

View File

@ -25,7 +25,7 @@ function RGBtoRGBTriple(R, G, B: byte): TRGBTriple;
function RGBtoRGBQuad(R, G, B: byte): TRGBQuad; overload; function RGBtoRGBQuad(R, G, B: byte): TRGBQuad; overload;
function RGBToRGBQuad(c: TColor): TRGBQuad; overload; function RGBToRGBQuad(c: TColor): TRGBQuad; overload;
function RGBQuadToRGB(q: TRGBQuad): TColor; function RGBQuadToRGB(q: TRGBQuad): TColor;
function RGBTripleToTColor(RGBTriple : TRGBTriple) : TColor; function RGBTripleToColor(RGBTriple : TRGBTriple) : TColor;
implementation implementation
@ -66,7 +66,7 @@ begin
Result := RGB(q.rgbRed, q.rgbGreen, q.rgbBlue); Result := RGB(q.rgbRed, q.rgbGreen, q.rgbBlue);
end; end;
function RGBTripleToTColor(RGBTriple: TRGBTriple): TColor; function RGBTripleToColor(RGBTriple: TRGBTriple): TColor;
begin begin
Result := RGBTriple.rgbtBlue shl 16 + RGBTriple.rgbtGreen shl 8 + RGBTriple.rgbtRed; Result := RGBTriple.rgbtBlue shl 16 + RGBTriple.rgbtGreen shl 8 + RGBTriple.rgbtRed;
end; end;

View File

@ -18,12 +18,19 @@ uses
type type
TVColorPicker = class(TmbTrackBarPicker) TVColorPicker = class(TmbTrackBarPicker)
private private
FHue, FSat, FVal: integer; FHue, FSat, FVal: Double;
function ArrowPosFromVal(l: integer): integer; FMaxHue, FMaxSat, FMaxVal: Integer;
function ArrowPosFromVal(v: integer): integer;
function ValFromArrowPos(p: integer): integer; function ValFromArrowPos(p: integer): integer;
function GetHue: Integer;
function GetSat: Integer;
function GetSelectedColor: TColor; function GetSelectedColor: TColor;
function GetValue: Integer;
procedure SetSelectedColor(c: TColor); procedure SetSelectedColor(c: TColor);
procedure SetHue(h: integer); procedure SetHue(h: integer);
procedure SetMaxHue(h: Integer);
procedure SetMaxSat(s: Integer);
procedure SetMaxVal(v: Integer);
procedure SetSat(s: integer); procedure SetSat(s: integer);
procedure SetValue(v: integer); procedure SetValue(v: integer);
protected protected
@ -34,9 +41,12 @@ type
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
published published
property Hue: integer read FHue write SetHue default 0; property Hue: integer read GetHue write SetHue;
property Saturation: integer read FSat write SetSat default 0; property Saturation: integer read GetSat write SetSat;
property Value: integer read FVal write SetValue default 255; property Value: integer read GetValue write SetValue;
property MaxHue: Integer read FMaxHue write SetMaxHue default 359;
property MaxSaturation: Integer read FMaxSat write SetMaxSat default 255;
property MaxValue: Integer read FMaxVal write SetMaxVal default 255;
property SelectedColor: TColor read GetSelectedColor write SetSelectedColor default clRed; property SelectedColor: TColor read GetSelectedColor write SetSelectedColor default clRed;
end; end;
@ -47,13 +57,15 @@ implementation
constructor TVColorPicker.Create(AOwner: TComponent); constructor TVColorPicker.Create(AOwner: TComponent);
begin begin
inherited; inherited;
FGradientWidth := 256; FMaxHue := 359;
FMaxSat := 255;
FMaxVal := 255;
FGradientWidth := FMaxVal + 1;
FGradientHeight := 12; FGradientHeight := 12;
FHue := 0; FHue := 0;
FSat := 0; FSat := 0;
FArrowPos := ArrowPosFromVal(255);
FChange := false; FChange := false;
SetValue(255); SetValue(FMaxVal);
HintFormat := 'Value: %value (selected)'; HintFormat := 'Value: %value (selected)';
FManual := false; FManual := false;
FChange := true; FChange := true;
@ -61,16 +73,31 @@ end;
function TVColorPicker.GetGradientColor(AValue: Integer): TColor; function TVColorPicker.GetGradientColor(AValue: Integer): TColor;
begin begin
Result := HSVtoColor(FHue, FSat, AValue); Result := HSVtoColor(FHue, FSat, AValue / FMaxVal);
end;
function TVColorPicker.GetHue: Integer;
begin
Result := round(FHue * FMaxHue);
end;
function TVColorPicker.GetSat: Integer;
begin
Result := round(FSat * FMaxSat);
end;
function TVColorPicker.GetValue: Integer;
begin
Result := round(FVal * FMaxVal);
end; end;
procedure TVColorPicker.SetHue(h: integer); procedure TVColorPicker.SetHue(h: integer);
begin begin
if h > 360 then h := 360; if h > FMaxHue+1 then h := FMaxHue + 1;
if h < 0 then h := 0; if h < 0 then h := 0;
if FHue <> h then if GetHue() <> h then
begin begin
FHue := h; FHue := h / (FMaxHue + 1);
FManual := false; FManual := false;
CreateGradient; CreateGradient;
Invalidate; Invalidate;
@ -78,13 +105,44 @@ begin
end; end;
end; end;
procedure TVColorPicker.SetMaxHue(h: Integer);
begin
if h = FMaxHue then
exit;
FMaxHue := h;
CreateGradient;
Invalidate;
if FChange and Assigned(OnChange) then OnChange(Self);
end;
procedure TVColorPicker.SetMaxSat(s: Integer);
begin
if s = FMaxSat then
exit;
FMaxSat := s;
CreateGradient;
Invalidate;
if FChange and Assigned(OnChange) then OnChange(Self);
end;
procedure TVColorPicker.SetMaxVal(v: Integer);
begin
if v = FMaxVal then
exit;
FMaxVal := v;
FGradientWidth := FMaxVal + 1;
CreateGradient;
Invalidate;
if FChange and Assigned(OnChange) then OnChange(Self);
end;
procedure TVColorPicker.SetSat(s: integer); procedure TVColorPicker.SetSat(s: integer);
begin begin
if s > 255 then s := 255; if s > FMaxSat then s := FMaxSat;
if s < 0 then s := 0; if s < 0 then s := 0;
if FSat <> s then if GetSat() <> s then
begin begin
FSat := s; FSat := s / FMaxSat;
FManual := false; FManual := false;
CreateGradient; CreateGradient;
Invalidate; Invalidate;
@ -92,19 +150,19 @@ begin
end; end;
end; end;
function TVColorPicker.ArrowPosFromVal(l: integer): integer; function TVColorPicker.ArrowPosFromVal(v: integer): integer;
var var
a: integer; a: integer;
begin begin
if Layout = lyHorizontal then if Layout = lyHorizontal then
begin begin
a := Round(((Width - 12)/255)*l); a := Round((Width - 12) * v / FMaxVal);
if a > Width - FLimit then a := Width - FLimit; if a > Width - FLimit then a := Width - FLimit;
end end
else else
begin begin
l := 255 - l; v := FMaxVal - v;
a := Round(((Height - 12)/255)*l); a := Round((Height - 12) * v / FMaxVal);
if a > Height - FLimit then a := Height - FLimit; if a > Height - FLimit then a := Height - FLimit;
end; end;
if a < 0 then a := 0; if a < 0 then a := 0;
@ -116,21 +174,21 @@ var
r: integer; r: integer;
begin begin
if Layout = lyHorizontal then if Layout = lyHorizontal then
r := Round(p/((Width - 12)/255)) r := Round(p / (Width - 12) * FMaxVal)
else else
r := Round(255 - p/((Height - 12)/255)); r := Round(FMaxVal - p / (Height - 12) * FMaxVal);
if r < 0 then r := 0; if r < 0 then r := 0;
if r > 255 then r := 255; if r > FMaxVal then r := FMaxVal;
Result := r; Result := r;
end; end;
procedure TVColorPicker.SetValue(V: integer); procedure TVColorPicker.SetValue(V: integer);
begin begin
if v < 0 then v := 0; if v < 0 then v := 0;
if v > 255 then v := 255; if v > FMaxVal then v := FMaxVal;
if FVal <> v then if GetValue() <> v then
begin begin
FVal := v; FVal := v / FMaxVal;
FArrowPos := ArrowPosFromVal(v); FArrowPos := ArrowPosFromVal(v);
FManual := false; FManual := false;
Invalidate; Invalidate;
@ -140,15 +198,14 @@ end;
function TVColorPicker.GetSelectedColor: TColor; function TVColorPicker.GetSelectedColor: TColor;
begin begin
if not WebSafe then Result := HSVtoColor(FHue, FSat, FVal);
Result := HSVtoColor(FHue, FSat, FVal) if WebSafe then
else Result := GetWebSafe(Result);
Result := GetWebSafe(HSVtoColor(FHue, FSat, FVal));
end; end;
function TVColorPicker.GetSelectedValue: integer; function TVColorPicker.GetSelectedValue: integer;
begin begin
Result := FVal; Result := GetValue();
end; end;
procedure TVColorPicker.SetSelectedColor(c: TColor); procedure TVColorPicker.SetSelectedColor(c: TColor);
@ -156,7 +213,7 @@ var
h, s, v: integer; h, s, v: integer;
begin begin
if WebSafe then c := GetWebSafe(c); if WebSafe then c := GetWebSafe(c);
RGBToHSV(GetRValue(c), GetGValue(c), GetBValue(c), h, s, v); RGBToHSVRange(GetRValue(c), GetGValue(c), GetBValue(c), h, s, v);
FChange := false; FChange := false;
SetHue(h); SetHue(h);
SetSat(s); SetSat(s);
@ -168,38 +225,41 @@ end;
function TVColorPicker.GetArrowPos: integer; function TVColorPicker.GetArrowPos: integer;
begin begin
Result := ArrowPosFromVal(FVal); if FMaxVal = 0 then
Result := inherited GetArrowPos
else
Result := ArrowPosFromVal(GetValue());
end; end;
procedure TVColorPicker.Execute(tbaAction: integer); procedure TVColorPicker.Execute(tbaAction: integer);
begin begin
case tbaAction of case tbaAction of
TBA_Resize: TBA_Resize:
SetValue(FVal); SetValue(GetValue());
TBA_MouseMove: TBA_MouseMove:
FVal := ValFromArrowPos(FArrowPos); SetValue(ValFromArrowPos(FArrowPos));
TBA_MouseDown: TBA_MouseDown:
FVal := ValFromArrowPos(FArrowPos); SetValue(ValFromArrowPos(FArrowPos));
TBA_MouseUp: TBA_MouseUp:
FVal := ValFromArrowPos(FArrowPos); SetValue(ValFromArrowPos(FArrowPos));
TBA_WheelUp: TBA_WheelUp:
SetValue(FVal + Increment); SetValue(GetValue() + Increment);
TBA_WheelDown: TBA_WheelDown:
SetValue(FVal - Increment); SetValue(GetValue() - Increment);
TBA_VKRight: TBA_VKRight:
SetValue(FVal + Increment); SetValue(GetValue() + Increment);
TBA_VKCtrlRight: TBA_VKCtrlRight:
SetValue(255); SetValue(FMaxVal);
TBA_VKLeft: TBA_VKLeft:
SetValue(FVal - Increment); SetValue(GetValue() - Increment);
TBA_VKCtrlLeft: TBA_VKCtrlLeft:
SetValue(0); SetValue(0);
TBA_VKUp: TBA_VKUp:
SetValue(FVal + Increment); SetValue(GetValue() + Increment);
TBA_VKCtrlUp: TBA_VKCtrlUp:
SetValue(255); SetValue(FMaxVal);
TBA_VKDown: TBA_VKDown:
SetValue(FVal - Increment); SetValue(GetValue() - Increment);
TBA_VKCtrlDown: TBA_VKCtrlDown:
SetValue(0); SetValue(0);
else else