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