| 1 |
unit KColorPicker; |
| 2 |
|
| 3 |
{$IFDEF FPC} |
| 4 |
{$MODE DELPHI} |
| 5 |
{$ENDIF} |
| 6 |
|
| 7 |
interface |
| 8 |
|
| 9 |
uses |
| 10 |
LCLIntf, LCLType, |
| 11 |
SysUtils, Classes, Controls, Graphics, Forms, |
| 12 |
RGBCMYKUtils, mbTrackBarPicker, HTMLColors; |
| 13 |
|
| 14 |
type |
| 15 |
TKColorPicker = class(TmbTrackBarPicker) |
| 16 |
private |
| 17 |
FCyan, FMagenta, FYellow, FBlack: integer; |
| 18 |
function ArrowPosFromBlack(k: integer): integer; |
| 19 |
function BlackFromArrowPos(p: integer): integer; |
| 20 |
procedure SetBlack(k: integer); |
| 21 |
procedure SetCyan(c: integer); |
| 22 |
procedure SetMagenta(m: integer); |
| 23 |
procedure SetYellow(y: integer); |
| 24 |
protected |
| 25 |
procedure Execute(tbaAction: integer); override; |
| 26 |
function GetArrowPos: integer; override; |
| 27 |
function GetGradientColor(AValue: Integer): TColor; override; |
| 28 |
function GetSelectedColor: TColor; override; |
| 29 |
function GetSelectedValue: integer; override; |
| 30 |
procedure SetSelectedColor(clr: TColor); override; |
| 31 |
public |
| 32 |
constructor Create(AOwner: TComponent); override; |
| 33 |
published |
| 34 |
property Cyan: integer read FCyan write SetCyan default 255; |
| 35 |
property Magenta: integer read FMagenta write SetMagenta default 0; |
| 36 |
property Yellow: integer read FYellow write SetYellow default 0; |
| 37 |
property Black: integer read FBlack write SetBlack default 0; |
| 38 |
property SelectedColor default clRed; |
| 39 |
property Layout default lyVertical; |
| 40 |
property HintFormat; |
| 41 |
end; |
| 42 |
|
| 43 |
implementation |
| 44 |
|
| 45 |
uses |
| 46 |
mbUtils; |
| 47 |
|
| 48 |
{TKColorPicker} |
| 49 |
|
| 50 |
constructor TKColorPicker.Create(AOwner: TComponent); |
| 51 |
begin |
| 52 |
inherited; |
| 53 |
FGradientWidth := 256; |
| 54 |
FGradientHeight := 1; |
| 55 |
FCyan := 0; |
| 56 |
FMagenta := 0; |
| 57 |
FYellow := 0; |
| 58 |
SetBlack(255); |
| 59 |
Layout := lyVertical; |
| 60 |
HintFormat := 'Black: %value (selected)'; |
| 61 |
end; |
| 62 |
|
| 63 |
function TKColorPicker.ArrowPosFromBlack(k: integer): integer; |
| 64 |
var |
| 65 |
a: integer; |
| 66 |
begin |
| 67 |
if Layout = lyHorizontal then |
| 68 |
begin |
| 69 |
a := Round(((Width - 12)/255)*k); |
| 70 |
if a > Width - FLimit then a := Width - FLimit; |
| 71 |
end |
| 72 |
else |
| 73 |
begin |
| 74 |
k := 255 - k; |
| 75 |
a := Round(((Height - 12)/255)*k); |
| 76 |
if a > Height - FLimit then a := Height - FLimit; |
| 77 |
end; |
| 78 |
if a < 0 then a := 0; |
| 79 |
Result := a; |
| 80 |
end; |
| 81 |
|
| 82 |
function TKColorPicker.BlackFromArrowPos(p: integer): integer; |
| 83 |
var |
| 84 |
k: integer; |
| 85 |
begin |
| 86 |
case Layout of |
| 87 |
lyHorizontal: |
| 88 |
k := Round(p * 255 / (Width - 12)); |
| 89 |
lyVertical: |
| 90 |
k := Round(255 - p * 255 / (Height - 12)); |
| 91 |
end; |
| 92 |
Clamp(k, 0, 255); |
| 93 |
Result := k; |
| 94 |
end; |
| 95 |
|
| 96 |
procedure TKColorPicker.Execute(tbaAction: integer); |
| 97 |
begin |
| 98 |
case tbaAction of |
| 99 |
TBA_Resize: |
| 100 |
SetBlack(FBlack); |
| 101 |
TBA_MouseMove: |
| 102 |
SetBlack(BlackFromArrowPos(FArrowPos)); |
| 103 |
TBA_MouseDown: |
| 104 |
SetBlack(BlackFromArrowPos(FArrowPos)); |
| 105 |
TBA_MouseUp: |
| 106 |
SetBlack(BlackFromArrowPos(FArrowPos)); |
| 107 |
TBA_WheelUp: |
| 108 |
SetBlack(FBlack + Increment); |
| 109 |
TBA_WheelDown: |
| 110 |
SetBlack(FBlack - Increment); |
| 111 |
TBA_VKRight: |
| 112 |
SetBlack(FBlack + Increment); |
| 113 |
TBA_VKCtrlRight: |
| 114 |
SetBlack(255); |
| 115 |
TBA_VKLeft: |
| 116 |
SetBlack(FBlack - Increment); |
| 117 |
TBA_VKCtrlLeft: |
| 118 |
SetBlack(0); |
| 119 |
TBA_VKUp: |
| 120 |
SetBlack(FBlack + Increment); |
| 121 |
TBA_VKCtrlUp: |
| 122 |
SetBlack(255); |
| 123 |
TBA_VKDown: |
| 124 |
SetBlack(FBlack - Increment); |
| 125 |
TBA_VKCtrlDown: |
| 126 |
SetBlack(0); |
| 127 |
else |
| 128 |
inherited; |
| 129 |
end; |
| 130 |
end; |
| 131 |
|
| 132 |
function TKColorPicker.GetArrowPos: integer; |
| 133 |
begin |
| 134 |
Result := ArrowPosFromBlack(FBlack); |
| 135 |
end; |
| 136 |
|
| 137 |
function TKColorPicker.GetGradientColor(AValue: Integer): TColor; |
| 138 |
begin |
| 139 |
Result := CMYKtoColor(FCyan, FMagenta, FYellow, AValue); |
| 140 |
end; |
| 141 |
|
| 142 |
function TKColorPicker.GetSelectedColor: TColor; |
| 143 |
begin |
| 144 |
Result := CMYKtoColor(FCyan, FMagenta, FYellow, FBlack); |
| 145 |
if WebSafe then |
| 146 |
Result := GetWebSafe(Result); |
| 147 |
end; |
| 148 |
|
| 149 |
function TKColorPicker.GetSelectedValue: integer; |
| 150 |
begin |
| 151 |
Result := FBlack; |
| 152 |
end; |
| 153 |
|
| 154 |
procedure TKColorPicker.SetBlack(k: integer); |
| 155 |
begin |
| 156 |
Clamp(k, 0, 255); |
| 157 |
if FBlack <> k then |
| 158 |
begin |
| 159 |
FBlack := k; |
| 160 |
FArrowPos := ArrowPosFromBlack(k); |
| 161 |
Invalidate; |
| 162 |
DoChange; |
| 163 |
end; |
| 164 |
end; |
| 165 |
|
| 166 |
procedure TKColorPicker.SetCyan(c: integer); |
| 167 |
begin |
| 168 |
Clamp(c, 0, 255); |
| 169 |
if FCyan <> c then |
| 170 |
begin |
| 171 |
FCyan := c; |
| 172 |
CreateGradient; |
| 173 |
Invalidate; |
| 174 |
DoChange; |
| 175 |
end; |
| 176 |
end; |
| 177 |
|
| 178 |
procedure TKColorPicker.SetMagenta(m: integer); |
| 179 |
begin |
| 180 |
Clamp(m, 0, 255); |
| 181 |
if FMagenta <> m then |
| 182 |
begin |
| 183 |
FMagenta := m; |
| 184 |
CreateGradient; |
| 185 |
Invalidate; |
| 186 |
DoChange; |
| 187 |
end; |
| 188 |
end; |
| 189 |
|
| 190 |
procedure TKColorPicker.SetSelectedColor(clr: TColor); |
| 191 |
var |
| 192 |
c, m, y, k: integer; |
| 193 |
newGradient: Boolean; |
| 194 |
begin |
| 195 |
if WebSafe then |
| 196 |
clr := GetWebSafe(clr); |
| 197 |
if clr = GetSelectedColor then |
| 198 |
exit; |
| 199 |
|
| 200 |
ColorToCMYK(clr, c, m, y, k); |
| 201 |
newGradient := (c <> FCyan) or (m <> FMagenta) or (y <> FYellow); |
| 202 |
FCyan := c; |
| 203 |
FMagenta := m; |
| 204 |
FYellow := y; |
| 205 |
FBlack := k; |
| 206 |
if newGradient then |
| 207 |
CreateGradient; |
| 208 |
Invalidate; |
| 209 |
DoChange; |
| 210 |
end; |
| 211 |
|
| 212 |
procedure TKColorPicker.SetYellow(y: integer); |
| 213 |
begin |
| 214 |
Clamp(y, 0, 255); |
| 215 |
if FYellow <> y then |
| 216 |
begin |
| 217 |
FYellow := y; |
| 218 |
CreateGradient; |
| 219 |
Invalidate; |
| 220 |
DoChange; |
| 221 |
end; |
| 222 |
end; |
| 223 |
|
| 224 |
end. |