2016-12-08 23:14:26 +00:00
|
|
|
unit KColorPicker;
|
|
|
|
|
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE DELPHI}
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2016-12-09 23:47:46 +00:00
|
|
|
{$IFDEF FPC}
|
|
|
|
LCLIntf, LCLType, LMessages,
|
|
|
|
{$ELSE}
|
|
|
|
Windows, Messages,
|
|
|
|
{$ENDIF}
|
|
|
|
SysUtils, Classes, Controls, Graphics, Forms,
|
|
|
|
RGBCMYKUtils, mbTrackBarPicker, HTMLColors;
|
2016-12-08 23:14:26 +00:00
|
|
|
|
|
|
|
type
|
2016-12-09 23:47:46 +00:00
|
|
|
TKColorPicker = class(TmbTrackBarPicker)
|
|
|
|
private
|
|
|
|
FCyan, FMagenta, FYellow, FBlack: integer;
|
|
|
|
function ArrowPosFromBlack(k: integer): integer;
|
|
|
|
function BlackFromArrowPos(p: integer): integer;
|
|
|
|
function GetSelectedColor: TColor;
|
|
|
|
procedure SetSelectedColor(c: TColor);
|
|
|
|
procedure SetCyan(c: integer);
|
|
|
|
procedure SetMagenta(m: integer);
|
|
|
|
procedure SetYellow(y: integer);
|
|
|
|
procedure SetBlack(k: integer);
|
|
|
|
protected
|
|
|
|
procedure Execute(tbaAction: integer); override;
|
|
|
|
function GetArrowPos: integer; override;
|
|
|
|
function GetGradientColor(AValue: Integer): TColor; override;
|
|
|
|
function GetSelectedValue: integer; override;
|
|
|
|
public
|
|
|
|
constructor Create(AOwner: TComponent); override;
|
|
|
|
published
|
|
|
|
property Cyan: integer read FCyan write SetCyan default 255;
|
|
|
|
property Magenta: integer read FMagenta write SetMagenta default 0;
|
|
|
|
property Yellow: integer read FYellow write SetYellow default 0;
|
|
|
|
property Black: integer read FBlack write SetBlack default 0;
|
|
|
|
property SelectedColor: TColor read GetSelectedColor write SetSelectedColor default clRed;
|
|
|
|
property Layout default lyVertical;
|
|
|
|
end;
|
2016-12-08 23:14:26 +00:00
|
|
|
|
|
|
|
procedure Register;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{$IFDEF FPC}
|
|
|
|
{$R KColorPicker.dcr}
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
procedure Register;
|
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
RegisterComponents('mbColor Lib', [TKColorPicker]);
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
{TKColorPicker}
|
|
|
|
|
|
|
|
constructor TKColorPicker.Create(AOwner: TComponent);
|
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
inherited;
|
|
|
|
FGradientWidth := 256;
|
|
|
|
FGradientHeight := 12;
|
|
|
|
SetInitialBounds(0, 0, 22, 267);
|
|
|
|
//Width := 22;
|
|
|
|
//Height := 267;
|
|
|
|
Layout := lyVertical;
|
|
|
|
FCyan := 0;
|
|
|
|
FMagenta := 0;
|
|
|
|
FYellow := 0;
|
|
|
|
FBlack := 255;
|
|
|
|
FArrowPos := ArrowPosFromBlack(255);
|
|
|
|
FChange := false;
|
|
|
|
SetBlack(255);
|
|
|
|
HintFormat := 'Black: %value';
|
|
|
|
FManual := false;
|
|
|
|
FChange := true;
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
|
2016-12-09 23:47:46 +00:00
|
|
|
(*
|
2016-12-08 23:14:26 +00:00
|
|
|
procedure TKColorPicker.CreateKGradient;
|
|
|
|
var
|
2016-12-09 23:47:46 +00:00
|
|
|
i,j: integer;
|
2016-12-08 23:14:26 +00:00
|
|
|
row: pRGBQuadArray;
|
|
|
|
begin
|
|
|
|
if FKBmp = nil then
|
|
|
|
begin
|
|
|
|
FKBmp := TBitmap.Create;
|
|
|
|
FKBmp.PixelFormat := pf32bit;
|
|
|
|
end;
|
|
|
|
if Layout = lyHorizontal then
|
|
|
|
begin
|
|
|
|
FKBmp.width := 255;
|
|
|
|
FKBmp.height := 12;
|
|
|
|
for i := 0 to 254 do
|
|
|
|
for j := 0 to 11 do
|
|
|
|
begin
|
|
|
|
row := FKBmp.ScanLine[j];
|
|
|
|
if not WebSafe then
|
|
|
|
row[i] := RGBToRGBQuad(CMYKtoTColor(FCyan, FMagenta, FYellow, i))
|
|
|
|
// FKBmp.Canvas.Pixels[i, j] := CMYKtoTColor(FCyan, FMagenta, FYellow, i)
|
|
|
|
else
|
|
|
|
row[i] := RGBToRGBQuad(GetWebSafe(CMYKtoTColor(FCyan, FMagenta, FYellow, i)));
|
|
|
|
// FKBmp.Canvas.Pixels[i, j] := GetWebSafe(CMYKtoTColor(FCyan, FMagenta, FYellow, i));
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
FKBmp.width := 12;
|
|
|
|
FKBmp.height := 255;
|
|
|
|
for i := 0 to 254 do
|
|
|
|
begin
|
|
|
|
row := FKBmp.Scanline[i];
|
|
|
|
for j := 0 to 11 do
|
|
|
|
if not WebSafe then
|
|
|
|
row[j] := RGBToRGBQuad(CMYKtoTColor(FCyan, FMagenta, FYellow, 255-i))
|
|
|
|
// FKBmp.Canvas.Pixels[j, i] := CMYKtoTColor(FCyan, FMagenta, FYellow, 255-i)
|
|
|
|
else
|
|
|
|
row[j] := RGBToRGBQuad(GetWebSafe(CMYKtoTColor(FCyan, FMagenta, FYellow, 255-i)));
|
|
|
|
// FKBmp.Canvas.Pixels[j, i] := GetWebSafe(CMYKtoTColor(FCyan, FMagenta, FYellow, 255-i));
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
2016-12-09 23:47:46 +00:00
|
|
|
*)
|
|
|
|
|
|
|
|
function TKColorPicker.GetGradientColor(AValue: Integer): TColor;
|
|
|
|
begin
|
|
|
|
Result := CMYKtoTColor(FCyan, FMagenta, FYellow, AValue);
|
|
|
|
end;
|
2016-12-08 23:14:26 +00:00
|
|
|
|
|
|
|
procedure TKColorPicker.SetBlack(k: integer);
|
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
if k < 0 then k := 0;
|
|
|
|
if k > 255 then k := 255;
|
|
|
|
if FBlack <> k then
|
2016-12-08 23:14:26 +00:00
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
FBlack := k;
|
|
|
|
FArrowPos := ArrowPosFromBlack(k);
|
|
|
|
FManual := false;
|
|
|
|
Invalidate;
|
|
|
|
if FChange and Assigned(OnChange) then OnChange(Self);
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TKColorPicker.SetMagenta(m: integer);
|
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
if m > 255 then m := 255;
|
|
|
|
if m < 0 then m := 0;
|
|
|
|
if FMagenta <> m then
|
2016-12-08 23:14:26 +00:00
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
FMagenta := m;
|
|
|
|
FManual := false;
|
|
|
|
CreateGradient;
|
|
|
|
Invalidate;
|
|
|
|
if FChange and Assigned(OnChange) then OnChange(Self);
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TKColorPicker.SetYellow(y: integer);
|
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
if y > 255 then y := 255;
|
|
|
|
if y < 0 then y := 0;
|
|
|
|
if FYellow <> y then
|
2016-12-08 23:14:26 +00:00
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
FYellow := y;
|
|
|
|
FManual := false;
|
|
|
|
CreateGradient;
|
|
|
|
Invalidate;
|
|
|
|
if FChange and Assigned(OnChange) then OnChange(Self);
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TKColorPicker.SetCyan(c: integer);
|
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
if c > 255 then c := 255;
|
|
|
|
if c < 0 then c := 0;
|
|
|
|
if FCyan <> c then
|
2016-12-08 23:14:26 +00:00
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
FCyan := c;
|
|
|
|
FManual := false;
|
|
|
|
CreateGradient;
|
|
|
|
Invalidate;
|
|
|
|
if FChange and Assigned(OnChange) then OnChange(Self);
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TKColorPicker.ArrowPosFromBlack(k: integer): integer;
|
|
|
|
var
|
2016-12-09 23:47:46 +00:00
|
|
|
a: integer;
|
2016-12-08 23:14:26 +00:00
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
if Layout = lyHorizontal then
|
2016-12-08 23:14:26 +00:00
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
a := Round(((Width - 12)/255)*k);
|
|
|
|
if a > Width - FLimit then a := Width - FLimit;
|
2016-12-08 23:14:26 +00:00
|
|
|
end
|
2016-12-09 23:47:46 +00:00
|
|
|
else
|
2016-12-08 23:14:26 +00:00
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
k := 255 - k;
|
|
|
|
a := Round(((Height - 12)/255)*k);
|
|
|
|
if a > Height - FLimit then a := Height - FLimit;
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
2016-12-09 23:47:46 +00:00
|
|
|
if a < 0 then a := 0;
|
|
|
|
Result := a;
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TKColorPicker.BlackFromArrowPos(p: integer): integer;
|
|
|
|
var
|
2016-12-09 23:47:46 +00:00
|
|
|
r: integer;
|
2016-12-08 23:14:26 +00:00
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
if Layout = lyHorizontal then
|
|
|
|
r := Round(p/((Width - 12)/255))
|
|
|
|
else
|
|
|
|
r := Round(255 - p/((Height - 12)/255));
|
|
|
|
if r < 0 then r := 0;
|
|
|
|
if r > 255 then r := 255;
|
|
|
|
Result := r;
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TKColorPicker.GetSelectedColor: TColor;
|
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
if not WebSafe then
|
|
|
|
Result := CMYKtoTColor(FCyan, FMagenta, FYellow, FBlack)
|
|
|
|
else
|
|
|
|
Result := GetWebSafe(CMYKtoTColor(FCyan, FMagenta, FYellow, FBlack));
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TKColorPicker.GetSelectedValue: integer;
|
|
|
|
begin
|
|
|
|
Result := FBlack;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TKColorPicker.SetSelectedColor(c: TColor);
|
|
|
|
var
|
2016-12-09 23:47:46 +00:00
|
|
|
cy, m, y, k: integer;
|
2016-12-08 23:14:26 +00:00
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
if WebSafe then c := GetWebSafe(c);
|
|
|
|
ColorToCMYK(c, cy, m, y, k);
|
|
|
|
FChange := false;
|
|
|
|
SetMagenta(m);
|
|
|
|
SetYellow(y);
|
|
|
|
SetCyan(cy);
|
|
|
|
SetBlack(k);
|
|
|
|
FManual := false;
|
|
|
|
FChange := true;
|
|
|
|
if Assigned(OnChange) then OnChange(Self);
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TKColorPicker.GetArrowPos: integer;
|
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
Result := ArrowPosFromBlack(FBlack);
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TKColorPicker.Execute(tbaAction: integer);
|
|
|
|
begin
|
2016-12-09 23:47:46 +00:00
|
|
|
case tbaAction of
|
|
|
|
TBA_Resize:
|
|
|
|
SetBlack(FBlack);
|
|
|
|
TBA_MouseMove:
|
|
|
|
FBlack := BlackFromArrowPos(FArrowPos);
|
|
|
|
TBA_MouseDown:
|
|
|
|
FBlack := BlackFromArrowPos(FArrowPos);
|
|
|
|
TBA_MouseUp:
|
|
|
|
FBlack := BlackFromArrowPos(FArrowPos);
|
|
|
|
TBA_WheelUp:
|
|
|
|
SetBlack(FBlack + Increment);
|
|
|
|
TBA_WheelDown:
|
|
|
|
SetBlack(FBlack - Increment);
|
|
|
|
TBA_VKRight:
|
|
|
|
SetBlack(FBlack + Increment);
|
|
|
|
TBA_VKCtrlRight:
|
|
|
|
SetBlack(255);
|
|
|
|
TBA_VKLeft:
|
|
|
|
SetBlack(FBlack - Increment);
|
|
|
|
TBA_VKCtrlLeft:
|
|
|
|
SetBlack(0);
|
|
|
|
TBA_VKUp:
|
|
|
|
SetBlack(FBlack + Increment);
|
|
|
|
TBA_VKCtrlUp:
|
|
|
|
SetBlack(255);
|
|
|
|
TBA_VKDown:
|
|
|
|
SetBlack(FBlack - Increment);
|
|
|
|
TBA_VKCtrlDown:
|
|
|
|
SetBlack(0);
|
|
|
|
else
|
|
|
|
inherited;
|
|
|
|
end;
|
2016-12-08 23:14:26 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|