| 1 |
unit FormCastleColorPicker; |
| 2 |
|
| 3 |
{$mode ObjFPC}{$H+} |
| 4 |
|
| 5 |
interface |
| 6 |
|
| 7 |
uses |
| 8 |
Classes, SysUtils, Forms, Controls, Graphics, Math, Dialogs, ExtCtrls, |
| 9 |
ComCtrls, StdCtrls, Spin, HSLRingPicker, SLColorPicker, HSColorPicker, |
| 10 |
HRingPicker, HSCirclePicker, LVColorPicker, RColorPicker, GColorPicker, |
| 11 |
BColorPicker, HColorPicker, SColorPicker, CastlePropEdits, CastleColors, |
| 12 |
LCLIntf, Buttons, ButtonPanel, CastleEditorPropEdits; |
| 13 |
|
| 14 |
type |
| 15 |
TCastleColorPickerForm = class(TForm) |
| 16 |
AlphaSpinEdit: TFloatSpinEdit; |
| 17 |
BSpinEditRgb: TFloatSpinEdit; |
| 18 |
BTabColorPickerRgb: TBColorPicker; |
| 19 |
ButtonCopy: TButton; |
| 20 |
ButtonPanel1: TButtonPanel; |
| 21 |
ButtonRevert: TButton; |
| 22 |
EditHex: TEdit; |
| 23 |
GSpinEditRgb: TFloatSpinEdit; |
| 24 |
GTabColorPickerRgb: TGColorPicker; |
| 25 |
HSPanelCirclePicker: THSCirclePicker; |
| 26 |
HSpinEditHsv: TFloatSpinEdit; |
| 27 |
HTabColorPickerHsv: THColorPicker; |
| 28 |
LabelTabHsvTitleH: TLabel; |
| 29 |
LabelTabHexTitleHex: TLabel; |
| 30 |
LabelTabHsvTitleV: TLabel; |
| 31 |
MemoPascalCode: TMemo; |
| 32 |
RLabelTitleRgb: TLabel; |
| 33 |
GLabelTitleRgb: TLabel; |
| 34 |
BLabelTitleRgb: TLabel; |
| 35 |
LabelTitleAlpha: TLabel; |
| 36 |
LabelTabHsvTitleS: TLabel; |
| 37 |
RSpinEditRgb: TFloatSpinEdit; |
| 38 |
SSpinEditHsv: TFloatSpinEdit; |
| 39 |
TabSheetHex: TTabSheet; |
| 40 |
TabSheetPascalCode: TTabSheet; |
| 41 |
TimerMousePos: TTimer; |
| 42 |
VPanelColorPicker: TLVColorPicker; |
| 43 |
AlphaColorPicker: TLVColorPicker; |
| 44 |
VSpinEditHsv: TFloatSpinEdit; |
| 45 |
VTabColorPickerHsv: TLVColorPicker; |
| 46 |
PageControlColorModel: TPageControl; |
| 47 |
PanelAlpha: TPanel; |
| 48 |
PanelCirclePicker: TPanel; |
| 49 |
RTabColorPickerRgb: TRColorPicker; |
| 50 |
STabColorPickerHsv: TSColorPicker; |
| 51 |
TabSheetHsv: TTabSheet; |
| 52 |
TabSheetRgb: TTabSheet; |
| 53 |
procedure AlphaColorPickerChange(Sender: TObject); |
| 54 |
procedure AlphaSpinEditChange(Sender: TObject); |
| 55 |
procedure BSpinEditRgbChange(Sender: TObject); |
| 56 |
procedure BTabColorPickerRgbChange(Sender: TObject); |
| 57 |
procedure ButtonCopyClick(Sender: TObject); |
| 58 |
procedure ButtonRevertClick(Sender: TObject); |
| 59 |
procedure FormCreate(Sender: TObject); |
| 60 |
procedure FormDestroy(Sender: TObject); |
| 61 |
procedure GSpinEditRgbChange(Sender: TObject); |
| 62 |
procedure GTabColorPickerRgbChange(Sender: TObject); |
| 63 |
procedure HSPanelCirclePickerChange(Sender: TObject); |
| 64 |
procedure HSpinEditHsvChange(Sender: TObject); |
| 65 |
procedure HTabColorPickerHsvChange(Sender: TObject); |
| 66 |
procedure PageControlColorModelChange(Sender: TObject); |
| 67 |
procedure RTabColorPickerRgbChange(Sender: TObject); |
| 68 |
procedure SSpinEditHsvChange(Sender: TObject); |
| 69 |
procedure STabColorPickerHsvChange(Sender: TObject); |
| 70 |
procedure TimerMousePosTimer(Sender: TObject); |
| 71 |
procedure VPanelColorPickerChange(Sender: TObject); |
| 72 |
procedure RSpinEditRgbChange(Sender: TObject); |
| 73 |
procedure VSpinEditHsvChange(Sender: TObject); |
| 74 |
procedure VTabColorPickerHsvChange(Sender: TObject); |
| 75 |
|
| 76 |
strict private |
| 77 |
{ How many digits to use for comparisons } |
| 78 |
ColorPrecision: TRoundToRange; |
| 79 |
ColorEpsilon: Single; |
| 80 |
{ A flag to not close window when mouse is outside of form but never |
| 81 |
was inside } |
| 82 |
MouseWasInForm: Boolean; |
| 83 |
procedure SetColorInCirclePickerPanel(NewColor: TCastleColor); overload; |
| 84 |
procedure SetColorInCirclePickerPanel(NewColor: TColor); overload; |
| 85 |
{ Sets Hue the value should be in range from 0 to 6 } |
| 86 |
procedure SetHValueInCirclePickerPanel(const NewValue: Single); |
| 87 |
{ Sets Saturation value should be i range from 0 to 1 } |
| 88 |
procedure SetSValueInCirclePickerPanel(const NewValue: Single); |
| 89 |
{ Sets Value, it should be in range from 0 to 1 } |
| 90 |
procedure SetVValueInCirclePickerPanel(const NewValue: Single); |
| 91 |
procedure SetRValueInCirclePickerPanel(const NewValue: Single); |
| 92 |
procedure SetGValueInCirclePickerPanel(const NewValue: Single); |
| 93 |
procedure SetBValueInCirclePickerPanel(const NewValue: Single); |
| 94 |
|
| 95 |
procedure SetColorInRgbTab(const NewColor: TCastleColor); overload; |
| 96 |
procedure SetColorInRgbTab(const NewColor: TColor); overload; |
| 97 |
procedure SetRValueInRgbTab(const NewValue: Single); |
| 98 |
procedure SetGValueInRgbTab(const NewValue: Single); |
| 99 |
procedure SetBValueInRgbTab(const NewValue: Single); |
| 100 |
procedure BlockEventsInRgbTab; |
| 101 |
procedure UnblockEventsInRgbTab; |
| 102 |
|
| 103 |
procedure SetColorInHsvTab(const NewColor: TCastleColor); overload; |
| 104 |
procedure SetColorInHsvTab(const NewColor: TColor); overload; |
| 105 |
{ Sets Hue in Hsv tab NewValue should be in 0..6 range. } |
| 106 |
procedure SetHValueInHsvTab(const NewValue: Single); |
| 107 |
procedure SetSValueInHsvTab(const NewValue: Single); |
| 108 |
procedure SetVValueInHsvTab(const NewValue: Single); |
| 109 |
procedure BlockEventsInHsvTab; |
| 110 |
procedure UnblockEventsInHsvTab; |
| 111 |
|
| 112 |
procedure SetColorInHexTab(const NewColor: TCastleColor); overload; |
| 113 |
procedure SetColorInHexTab(const NewColor: TColor); overload; |
| 114 |
|
| 115 |
{ Used to get current color value from HSV circle, We need function like |
| 116 |
this because updating all controls is too expensive. } |
| 117 |
procedure UpdateCurrentTabFromPanel; |
| 118 |
procedure SetAlphaValue(const NewValue: Single); |
| 119 |
procedure UpdatePropertyEditorValue; |
| 120 |
procedure GeneratePascalCode; |
| 121 |
public |
| 122 |
ColorPropertyEditor: TCastleAbstractColorPropertyEditor; |
| 123 |
PrevColor: TCastleColor; |
| 124 |
|
| 125 |
procedure Init(const ColorPropEditor: TCastleAbstractColorPropertyEditor; |
| 126 |
const InitColor: TCastleColor; |
| 127 |
const ShowAlpha: Boolean = true); overload; |
| 128 |
|
| 129 |
procedure Init(const ColorPropEditor: TCastleAbstractColorPropertyEditor; |
| 130 |
const InitColorRGB: TCastleColorRGB); overload; |
| 131 |
|
| 132 |
function CurrentCastleColor: TCastleColor; |
| 133 |
function CurrentCastleColorRGB: TCastleColorRGB; |
| 134 |
end; |
| 135 |
|
| 136 |
var |
| 137 |
CastleColorPickerForm: TCastleColorPickerForm; |
| 138 |
|
| 139 |
implementation |
| 140 |
|
| 141 |
{$R *.lfm} |
| 142 |
|
| 143 |
uses Clipbrd, PropEdits, |
| 144 |
CastleVectors, CastleLog, CastleUtils, mbColorConv, CastleControl; |
| 145 |
|
| 146 |
{ TCastleColorPickerForm } |
| 147 |
|
| 148 |
procedure TCastleColorPickerForm.HSPanelCirclePickerChange(Sender: TObject); |
| 149 |
begin |
| 150 |
UpdatePropertyEditorValue; |
| 151 |
UpdateCurrentTabFromPanel; |
| 152 |
end; |
| 153 |
|
| 154 |
procedure TCastleColorPickerForm.GTabColorPickerRgbChange(Sender: TObject); |
| 155 |
begin |
| 156 |
SetGValueInRgbTab(GTabColorPickerRgb.Green / 255); |
| 157 |
SetGValueInCirclePickerPanel(GTabColorPickerRgb.Green / 255); |
| 158 |
end; |
| 159 |
|
| 160 |
procedure TCastleColorPickerForm.BTabColorPickerRgbChange(Sender: TObject); |
| 161 |
begin |
| 162 |
SetBValueInRgbTab(BTabColorPickerRgb.Blue / 255); |
| 163 |
SetBValueInCirclePickerPanel(BTabColorPickerRgb.Blue / 255); |
| 164 |
end; |
| 165 |
|
| 166 |
procedure TCastleColorPickerForm.ButtonCopyClick(Sender: TObject); |
| 167 |
begin |
| 168 |
Clipboard.AsText := EditHex.Text; |
| 169 |
end; |
| 170 |
|
| 171 |
procedure TCastleColorPickerForm.ButtonRevertClick(Sender: TObject); |
| 172 |
begin |
| 173 |
SetColorInCirclePickerPanel(PrevColor); |
| 174 |
SetAlphaValue(PrevColor.W); |
| 175 |
end; |
| 176 |
|
| 177 |
procedure TCastleColorPickerForm.FormCreate(Sender: TObject); |
| 178 |
begin |
| 179 |
MouseWasInForm := false; |
| 180 |
TimerMousePos.Enabled := true; |
| 181 |
end; |
| 182 |
|
| 183 |
procedure TCastleColorPickerForm.FormDestroy(Sender: TObject); |
| 184 |
begin |
| 185 |
TimerMousePos.Enabled := false; |
| 186 |
end; |
| 187 |
|
| 188 |
procedure TCastleColorPickerForm.BSpinEditRgbChange(Sender: TObject); |
| 189 |
begin |
| 190 |
SetBValueInRgbTab(BSpinEditRgb.Value); |
| 191 |
SetBValueInCirclePickerPanel(BSpinEditRgb.Value); |
| 192 |
end; |
| 193 |
|
| 194 |
procedure TCastleColorPickerForm.AlphaColorPickerChange(Sender: TObject); |
| 195 |
begin |
| 196 |
SetAlphaValue(AlphaColorPicker.RelValue); |
| 197 |
end; |
| 198 |
|
| 199 |
procedure TCastleColorPickerForm.AlphaSpinEditChange(Sender: TObject); |
| 200 |
begin |
| 201 |
SetAlphaValue(AlphaSpinEdit.Value); |
| 202 |
end; |
| 203 |
|
| 204 |
procedure TCastleColorPickerForm.GSpinEditRgbChange(Sender: TObject); |
| 205 |
begin |
| 206 |
SetGValueInRgbTab(GSpinEditRgb.Value); |
| 207 |
SetGValueInCirclePickerPanel(GSpinEditRgb.Value); |
| 208 |
end; |
| 209 |
|
| 210 |
procedure TCastleColorPickerForm.HSpinEditHsvChange(Sender: TObject); |
| 211 |
begin |
| 212 |
SetHValueInHsvTab(HSpinEditHsv.Value); // Spin edit uses castle range from 0..6 |
| 213 |
SetHValueInCirclePickerPanel(HSpinEditHsv.Value); |
| 214 |
end; |
| 215 |
|
| 216 |
procedure TCastleColorPickerForm.HTabColorPickerHsvChange(Sender: TObject); |
| 217 |
begin |
| 218 |
SetHValueInHsvTab(HTabColorPickerHsv.RelHue * 6); |
| 219 |
SetHValueInCirclePickerPanel(HTabColorPickerHsv.RelHue * 6); |
| 220 |
end; |
| 221 |
|
| 222 |
procedure TCastleColorPickerForm.PageControlColorModelChange(Sender: TObject); |
| 223 |
begin |
| 224 |
{ Update values when tab is shown. } |
| 225 |
UpdateCurrentTabFromPanel; |
| 226 |
end; |
| 227 |
|
| 228 |
procedure TCastleColorPickerForm.RTabColorPickerRgbChange(Sender: TObject); |
| 229 |
begin |
| 230 |
SetRValueInRgbTab(RTabColorPickerRgb.Red / 255); |
| 231 |
SetRValueInCirclePickerPanel(RTabColorPickerRgb.Red / 255); |
| 232 |
end; |
| 233 |
|
| 234 |
procedure TCastleColorPickerForm.SSpinEditHsvChange(Sender: TObject); |
| 235 |
begin |
| 236 |
SetSValueInHsvTab(SSpinEditHsv.Value); |
| 237 |
SetSValueInCirclePickerPanel(SSpinEditHsv.Value); |
| 238 |
end; |
| 239 |
|
| 240 |
procedure TCastleColorPickerForm.STabColorPickerHsvChange(Sender: TObject); |
| 241 |
begin |
| 242 |
SetSValueInHsvTab(STabColorPickerHsv.RelSaturation); |
| 243 |
SetSValueInCirclePickerPanel(STabColorPickerHsv.RelSaturation); |
| 244 |
end; |
| 245 |
|
| 246 |
procedure TCastleColorPickerForm.TimerMousePosTimer(Sender: TObject); |
| 247 |
var |
| 248 |
TestRect: TRect; |
| 249 |
Margin: Integer; |
| 250 |
begin |
| 251 |
Margin := Round (Width / 4); |
| 252 |
|
| 253 |
TestRect.Top := ClientRect.Top - Margin ; |
| 254 |
TestRect.Left := ClientRect.Left - Margin; |
| 255 |
TestRect.Width := ClientRect.Width + Margin * 2; |
| 256 |
TestRect.Height := ClientRect.Height + Margin * 2; |
| 257 |
|
| 258 |
if TestRect.Contains(ScreenToClient(Mouse.CursorPos)) then |
| 259 |
MouseWasInForm := true |
| 260 |
else |
| 261 |
if MouseWasInForm then |
| 262 |
Close; |
| 263 |
end; |
| 264 |
|
| 265 |
procedure TCastleColorPickerForm.VPanelColorPickerChange(Sender: TObject); |
| 266 |
begin |
| 267 |
if HSPanelCirclePicker.Value <> VPanelColorPicker.Value then |
| 268 |
HSPanelCirclePicker.Value := VPanelColorPicker.Value; |
| 269 |
end; |
| 270 |
|
| 271 |
procedure TCastleColorPickerForm.RSpinEditRgbChange(Sender: TObject); |
| 272 |
begin |
| 273 |
SetRValueInRgbTab(RSpinEditRgb.Value); |
| 274 |
SetRValueInCirclePickerPanel(RSpinEditRgb.Value); |
| 275 |
end; |
| 276 |
|
| 277 |
procedure TCastleColorPickerForm.VSpinEditHsvChange(Sender: TObject); |
| 278 |
begin |
| 279 |
SetVValueInHsvTab(VSpinEditHsv.Value); |
| 280 |
SetVValueInCirclePickerPanel(VSpinEditHsv.Value); |
| 281 |
end; |
| 282 |
|
| 283 |
procedure TCastleColorPickerForm.VTabColorPickerHsvChange(Sender: TObject); |
| 284 |
begin |
| 285 |
SetVValueInHsvTab(VTabColorPickerHsv.RelValue); |
| 286 |
SetVValueInCirclePickerPanel(VTabColorPickerHsv.RelValue); |
| 287 |
end; |
| 288 |
|
| 289 |
procedure TCastleColorPickerForm.SetColorInCirclePickerPanel(NewColor: TCastleColor); |
| 290 |
var |
| 291 |
ColorByte: TVector3Byte; |
| 292 |
begin |
| 293 |
ColorByte := Vector3Byte(NewColor.XYZ); // edit only Color RGB |
| 294 |
SetColorInCirclePickerPanel(RGBToColor(ColorByte[0], ColorByte[1], ColorByte[2])); |
| 295 |
end; |
| 296 |
|
| 297 |
procedure TCastleColorPickerForm.SetColorInCirclePickerPanel(NewColor: TColor); |
| 298 |
begin |
| 299 |
if HSPanelCirclePicker.SelectedColor <> NewColor then |
| 300 |
HSPanelCirclePicker.SelectedColor := NewColor; |
| 301 |
if VPanelColorPicker.Value <> HSPanelCirclePicker.Value then |
| 302 |
VPanelColorPicker.Value := HSPanelCirclePicker.Value; |
| 303 |
end; |
| 304 |
|
| 305 |
procedure TCastleColorPickerForm.SetHValueInCirclePickerPanel( |
| 306 |
const NewValue: Single); |
| 307 |
var |
| 308 |
//NewValueRounded: Single; // unused |
| 309 |
NewValueForControl: Single; // THSCirclePicker uses value from 0..1 |
| 310 |
begin |
| 311 |
//NewValueRounded := RoundTo(NewValue, ColorPrecision); |
| 312 |
NewValueForControl := RoundTo(NewValue / 6, ColorPrecision); |
| 313 |
if not SameValue(RoundTo(HSPanelCirclePicker.RelHue, ColorPrecision), |
| 314 |
NewValueForControl, ColorEpsilon) then |
| 315 |
HSPanelCirclePicker.RelHue := NewValueForControl; |
| 316 |
end; |
| 317 |
|
| 318 |
procedure TCastleColorPickerForm.SetSValueInCirclePickerPanel( |
| 319 |
const NewValue: Single); |
| 320 |
var |
| 321 |
NewValueRounded: Single; |
| 322 |
begin |
| 323 |
NewValueRounded := RoundTo(NewValue, ColorPrecision); |
| 324 |
if not SameValue(RoundTo(HSPanelCirclePicker.RelSaturation, ColorPrecision), |
| 325 |
NewValueRounded, ColorEpsilon) then |
| 326 |
HSPanelCirclePicker.RelSaturation := NewValueRounded; |
| 327 |
end; |
| 328 |
|
| 329 |
procedure TCastleColorPickerForm.SetVValueInCirclePickerPanel( |
| 330 |
const NewValue: Single); |
| 331 |
var |
| 332 |
NewValueRounded: Single; |
| 333 |
begin |
| 334 |
NewValueRounded := RoundTo(NewValue, ColorPrecision); |
| 335 |
if not SameValue(HSPanelCirclePicker.RelValue, NewValueRounded, ColorEpsilon) then |
| 336 |
HSPanelCirclePicker.RelValue := NewValueRounded; |
| 337 |
if not SameValue(VPanelColorPicker.RelValue, NewValueRounded, ColorEpsilon) then |
| 338 |
VPanelColorPicker.RelValue := NewValueRounded; |
| 339 |
end; |
| 340 |
|
| 341 |
procedure TCastleColorPickerForm.SetRValueInCirclePickerPanel( |
| 342 |
const NewValue: Single); |
| 343 |
var |
| 344 |
NewValueInt: Integer; |
| 345 |
begin |
| 346 |
NewValueInt := Round(RoundTo(NewValue, ColorPrecision) * 255); |
| 347 |
if HSPanelCirclePicker.Red <> NewValueInt then |
| 348 |
HSPanelCirclePicker.Red := NewValueInt; |
| 349 |
end; |
| 350 |
|
| 351 |
procedure TCastleColorPickerForm.SetGValueInCirclePickerPanel( |
| 352 |
const NewValue: Single); |
| 353 |
var |
| 354 |
NewValueInt: Integer; |
| 355 |
begin |
| 356 |
NewValueInt := Round(RoundTo(NewValue, ColorPrecision) * 255); |
| 357 |
if HSPanelCirclePicker.Green <> NewValueInt then |
| 358 |
HSPanelCirclePicker.Green := NewValueInt; |
| 359 |
end; |
| 360 |
|
| 361 |
procedure TCastleColorPickerForm.SetBValueInCirclePickerPanel( |
| 362 |
const NewValue: Single); |
| 363 |
var |
| 364 |
NewValueInt: Integer; |
| 365 |
begin |
| 366 |
NewValueInt := Round(RoundTo(NewValue, ColorPrecision) * 255); |
| 367 |
if HSPanelCirclePicker.Blue <> NewValueInt then |
| 368 |
HSPanelCirclePicker.Blue := NewValueInt; |
| 369 |
end; |
| 370 |
|
| 371 |
procedure TCastleColorPickerForm.SetColorInRgbTab(const NewColor: TCastleColor); |
| 372 |
begin |
| 373 |
BlockEventsInRgbTab; |
| 374 |
try |
| 375 |
SetRValueInRgbTab(NewColor.X); |
| 376 |
SetGValueInRgbTab(NewColor.Y); |
| 377 |
SetBValueInRgbTab(NewColor.Z); |
| 378 |
finally |
| 379 |
UnblockEventsInRgbTab; |
| 380 |
end; |
| 381 |
end; |
| 382 |
|
| 383 |
procedure TCastleColorPickerForm.SetColorInRgbTab(const NewColor: TColor); |
| 384 |
var |
| 385 |
Rgb: Longint; |
| 386 |
begin |
| 387 |
Rgb := ColorToRGB(NewColor); |
| 388 |
BlockEventsInRgbTab; |
| 389 |
try |
| 390 |
SetRValueInRgbTab(GetRValue(Rgb) / 255); |
| 391 |
SetGValueInRgbTab(GetGValue(Rgb) / 255); |
| 392 |
SetBValueInRgbTab(GetBValue(Rgb) / 255); |
| 393 |
finally |
| 394 |
UnblockEventsInRgbTab; |
| 395 |
end; |
| 396 |
end; |
| 397 |
|
| 398 |
procedure TCastleColorPickerForm.SetRValueInRgbTab(const NewValue: Single); |
| 399 |
var |
| 400 |
NewValueRounded: Single; |
| 401 |
NewValueInt: Integer; |
| 402 |
begin |
| 403 |
NewValueRounded := RoundTo(NewValue, ColorPrecision); |
| 404 |
NewValueInt := Round(NewValueRounded * 255); |
| 405 |
if RTabColorPickerRgb.Red <> NewValueInt then |
| 406 |
RTabColorPickerRgb.Red := NewValueInt; |
| 407 |
if GTabColorPickerRgb.Red <> NewValueInt then |
| 408 |
GTabColorPickerRgb.Red := NewValueInt; |
| 409 |
if BTabColorPickerRgb.Red <> NewValueInt then |
| 410 |
BTabColorPickerRgb.Red := NewValueInt; |
| 411 |
if not SameValue(RSpinEditRgb.Value, NewValueRounded, ColorEpsilon) then |
| 412 |
RSpinEditRgb.Value := NewValueRounded; |
| 413 |
end; |
| 414 |
|
| 415 |
procedure TCastleColorPickerForm.SetGValueInRgbTab(const NewValue: Single); |
| 416 |
var |
| 417 |
NewValueRounded: Single; |
| 418 |
NewValueInt: Integer; |
| 419 |
begin |
| 420 |
NewValueRounded := RoundTo(NewValue, ColorPrecision); |
| 421 |
NewValueInt := Round(NewValueRounded * 255); |
| 422 |
if RTabColorPickerRgb.Green <> NewValueInt then |
| 423 |
RTabColorPickerRgb.Green := NewValueInt; |
| 424 |
if GTabColorPickerRgb.Green <> NewValueInt then |
| 425 |
GTabColorPickerRgb.Green := NewValueInt; |
| 426 |
if BTabColorPickerRgb.Green <> NewValueInt then |
| 427 |
BTabColorPickerRgb.Green := NewValueInt; |
| 428 |
if not SameValue(GSpinEditRgb.Value, NewValueRounded, ColorEpsilon) then |
| 429 |
GSpinEditRgb.Value := NewValueRounded; |
| 430 |
end; |
| 431 |
|
| 432 |
procedure TCastleColorPickerForm.SetBValueInRgbTab(const NewValue: Single); |
| 433 |
var |
| 434 |
NewValueRounded: Single; |
| 435 |
NewValueInt: Integer; |
| 436 |
begin |
| 437 |
NewValueRounded := RoundTo(NewValue, ColorPrecision); |
| 438 |
NewValueInt := Round(NewValueRounded * 255); |
| 439 |
if RTabColorPickerRgb.Blue <> NewValueInt then |
| 440 |
RTabColorPickerRgb.Blue := NewValueInt; |
| 441 |
if GTabColorPickerRgb.Blue <> NewValueInt then |
| 442 |
GTabColorPickerRgb.Blue := NewValueInt; |
| 443 |
if BTabColorPickerRgb.Blue <> NewValueInt then |
| 444 |
BTabColorPickerRgb.Blue := NewValueInt; |
| 445 |
if not SameValue(BSpinEditRgb.Value, NewValueRounded, ColorEpsilon) then |
| 446 |
BSpinEditRgb.Value := NewValueRounded; |
| 447 |
end; |
| 448 |
|
| 449 |
procedure TCastleColorPickerForm.BlockEventsInRgbTab; |
| 450 |
begin |
| 451 |
RTabColorPickerRgb.OnChange := nil; |
| 452 |
GTabColorPickerRgb.OnChange := nil; |
| 453 |
BTabColorPickerRgb.OnChange := nil; |
| 454 |
RSpinEditRgb.OnChange := nil; |
| 455 |
GSpinEditRgb.OnChange := nil; |
| 456 |
BSpinEditRgb.OnChange := nil; |
| 457 |
end; |
| 458 |
|
| 459 |
procedure TCastleColorPickerForm.UnblockEventsInRgbTab; |
| 460 |
begin |
| 461 |
RTabColorPickerRgb.OnChange := @RTabColorPickerRgbChange; |
| 462 |
GTabColorPickerRgb.OnChange := @GTabColorPickerRgbChange; |
| 463 |
BTabColorPickerRgb.OnChange := @BTabColorPickerRgbChange; |
| 464 |
RSpinEditRgb.OnChange := @RSpinEditRgbChange; |
| 465 |
GSpinEditRgb.OnChange := @GSpinEditRgbChange; |
| 466 |
BSpinEditRgb.OnChange := @BSpinEditRgbChange; |
| 467 |
end; |
| 468 |
|
| 469 |
procedure TCastleColorPickerForm.SetColorInHsvTab(const NewColor: TCastleColor); |
| 470 |
var |
| 471 |
ColorByte: TVector3Byte; |
| 472 |
HDouble, SDouble, VDouble: Double; |
| 473 |
begin |
| 474 |
ColorByte := Vector3Byte(NewColor.XYZ); // edit only Color RGB |
| 475 |
RGBtoHSV(ColorByte[0], ColorByte[1], ColorByte[2], HDouble, SDouble, VDouble); |
| 476 |
|
| 477 |
BlockEventsInHsvTab; |
| 478 |
try |
| 479 |
SetHValueInHsvTab(HDouble * 6); |
| 480 |
SetSValueInHsvTab(SDouble); |
| 481 |
SetVValueInHsvTab(VDouble); |
| 482 |
finally |
| 483 |
UnblockEventsInHsvTab; |
| 484 |
end; |
| 485 |
end; |
| 486 |
|
| 487 |
procedure TCastleColorPickerForm.SetColorInHsvTab(const NewColor: TColor); |
| 488 |
var |
| 489 |
HDouble, SDouble, VDouble: Double; |
| 490 |
begin |
| 491 |
ColortoHSV(NewColor, HDouble, SDouble, VDouble); |
| 492 |
|
| 493 |
BlockEventsInHsvTab; |
| 494 |
try |
| 495 |
SetHValueInHsvTab(HDouble * 6); |
| 496 |
SetSValueInHsvTab(SDouble); |
| 497 |
SetVValueInHsvTab(VDouble); |
| 498 |
finally |
| 499 |
UnblockEventsInHsvTab; |
| 500 |
end; |
| 501 |
end; |
| 502 |
|
| 503 |
procedure TCastleColorPickerForm.SetHValueInHsvTab(const NewValue: Single); |
| 504 |
var |
| 505 |
NewValueRounded: Single; |
| 506 |
NewValueForControl: Single; // controls uses 0..1 range for hue |
| 507 |
begin |
| 508 |
NewValueRounded := RoundTo(NewValue, ColorPrecision); |
| 509 |
NewValueForControl := RoundTo(NewValueRounded / 6, ColorPrecision); |
| 510 |
if not SameValue(HTabColorPickerHsv.RelHue, NewValueForControl, ColorEpsilon) then |
| 511 |
HTabColorPickerHsv.RelHue := NewValueForControl; |
| 512 |
if not SameValue(STabColorPickerHsv.RelHue, NewValueForControl, ColorEpsilon) then |
| 513 |
STabColorPickerHsv.RelHue := NewValueForControl; |
| 514 |
if not SameValue(VTabColorPickerHsv.RelHue, NewValueForControl, ColorEpsilon) then |
| 515 |
VTabColorPickerHsv.RelHue := NewValueForControl; |
| 516 |
|
| 517 |
if not SameValue(HSpinEditHsv.Value, NewValueRounded, ColorEpsilon) then |
| 518 |
HSpinEditHsv.Value := NewValueRounded; |
| 519 |
end; |
| 520 |
|
| 521 |
procedure TCastleColorPickerForm.SetSValueInHsvTab(const NewValue: Single); |
| 522 |
var |
| 523 |
NewValueRounded: Single; |
| 524 |
begin |
| 525 |
NewValueRounded := RoundTo(NewValue, ColorPrecision); |
| 526 |
if not SameValue(HTabColorPickerHsv.RelSaturation, NewValueRounded, ColorEpsilon) then |
| 527 |
HTabColorPickerHsv.RelSaturation := NewValueRounded; |
| 528 |
if not SameValue(STabColorPickerHsv.RelSaturation, NewValueRounded, ColorEpsilon) then |
| 529 |
STabColorPickerHsv.RelSaturation := NewValueRounded; |
| 530 |
if not SameValue(VTabColorPickerHsv.RelSaturation, NewValueRounded, ColorEpsilon) then |
| 531 |
VTabColorPickerHsv.RelSaturation := NewValueRounded; |
| 532 |
|
| 533 |
if not SameValue(SSpinEditHsv.Value, NewValueRounded, ColorEpsilon) then |
| 534 |
SSpinEditHsv.Value := NewValueRounded; |
| 535 |
end; |
| 536 |
|
| 537 |
procedure TCastleColorPickerForm.SetVValueInHsvTab(const NewValue: Single); |
| 538 |
var |
| 539 |
NewValueRounded: Single; |
| 540 |
begin |
| 541 |
NewValueRounded := RoundTo(NewValue, ColorPrecision); |
| 542 |
if not SameValue(HTabColorPickerHsv.RelValue, NewValueRounded, ColorEpsilon) then |
| 543 |
HTabColorPickerHsv.RelValue := NewValueRounded; |
| 544 |
if not SameValue(STabColorPickerHsv.RelValue, NewValueRounded, ColorEpsilon) then |
| 545 |
STabColorPickerHsv.RelValue := NewValueRounded; |
| 546 |
if not SameValue(VTabColorPickerHsv.RelValue, NewValueRounded, ColorEpsilon) then |
| 547 |
VTabColorPickerHsv.RelValue := NewValue; |
| 548 |
|
| 549 |
if not SameValue(VSpinEditHsv.Value, NewValueRounded, ColorEpsilon) then |
| 550 |
VSpinEditHsv.Value := NewValueRounded; |
| 551 |
end; |
| 552 |
|
| 553 |
procedure TCastleColorPickerForm.BlockEventsInHsvTab; |
| 554 |
begin |
| 555 |
HTabColorPickerHsv.OnChange := nil; |
| 556 |
STabColorPickerHsv.OnChange := nil; |
| 557 |
VTabColorPickerHsv.OnChange := nil; |
| 558 |
HSpinEditHsv.OnChange := nil; |
| 559 |
SSpinEditHsv.OnChange := nil; |
| 560 |
VSpinEditHsv.OnChange := nil; |
| 561 |
end; |
| 562 |
|
| 563 |
procedure TCastleColorPickerForm.UnblockEventsInHsvTab; |
| 564 |
begin |
| 565 |
HTabColorPickerHsv.OnChange := @HTabColorPickerHsvChange; |
| 566 |
STabColorPickerHsv.OnChange := @STabColorPickerHsvChange; |
| 567 |
VTabColorPickerHsv.OnChange := @VTabColorPickerHsvChange; |
| 568 |
HSpinEditHsv.OnChange := @HSpinEditHsvChange; |
| 569 |
SSpinEditHsv.OnChange := @SSpinEditHsvChange; |
| 570 |
VSpinEditHsv.OnChange := @VSpinEditHsvChange; |
| 571 |
end; |
| 572 |
|
| 573 |
procedure TCastleColorPickerForm.SetColorInHexTab(const NewColor: TCastleColor); |
| 574 |
begin |
| 575 |
EditHex.Text := ColorToHex(NewColor); |
| 576 |
end; |
| 577 |
|
| 578 |
procedure TCastleColorPickerForm.SetColorInHexTab(const NewColor: TColor); |
| 579 |
var |
| 580 |
ColorByte: TVector4Byte; |
| 581 |
begin |
| 582 |
RedGreenBlue(NewColor, ColorByte.X, ColorByte.Y, ColorByte.Z); |
| 583 |
ColorByte.W := AlphaColorPicker.Value; |
| 584 |
EditHex.Text := ColorToHex(Vector4(ColorByte)); |
| 585 |
end; |
| 586 |
|
| 587 |
procedure TCastleColorPickerForm.UpdateCurrentTabFromPanel; |
| 588 |
begin |
| 589 |
if PageControlColorModel.ActivePage = TabSheetRgb then |
| 590 |
SetColorInRgbTab(HSPanelCirclePicker.SelectedColor) |
| 591 |
else |
| 592 |
if PageControlColorModel.ActivePage = TabSheetHsv then |
| 593 |
SetColorInHsvTab(HSPanelCirclePicker.SelectedColor) |
| 594 |
else |
| 595 |
if PageControlColorModel.ActivePage = TabSheetPascalCode then |
| 596 |
GeneratePascalCode |
| 597 |
else |
| 598 |
if PageControlColorModel.ActivePage = TabSheetHex then |
| 599 |
SetColorInHexTab(HSPanelCirclePicker.SelectedColor); |
| 600 |
end; |
| 601 |
|
| 602 |
procedure TCastleColorPickerForm.SetAlphaValue(const NewValue: Single); |
| 603 |
var |
| 604 |
NewValueRounded: Single; |
| 605 |
ValueChanged: Boolean; |
| 606 |
begin |
| 607 |
ValueChanged := false; |
| 608 |
NewValueRounded := RoundTo(NewValue, ColorPrecision); |
| 609 |
|
| 610 |
if not SameValue(AlphaColorPicker.RelValue, NewValueRounded, ColorEpsilon) then |
| 611 |
begin |
| 612 |
AlphaColorPicker.RelValue := NewValueRounded; |
| 613 |
ValueChanged := true; |
| 614 |
end; |
| 615 |
|
| 616 |
if not SameValue(AlphaSpinEdit.Value, NewValueRounded, ColorEpsilon) then |
| 617 |
begin |
| 618 |
AlphaSpinEdit.Value := NewValueRounded; |
| 619 |
ValueChanged := true; |
| 620 |
end; |
| 621 |
|
| 622 |
if ValueChanged then |
| 623 |
begin |
| 624 |
UpdatePropertyEditorValue; |
| 625 |
if PageControlColorModel.ActivePage = TabSheetPascalCode then |
| 626 |
GeneratePascalCode; |
| 627 |
end; |
| 628 |
end; |
| 629 |
|
| 630 |
procedure TCastleColorPickerForm.UpdatePropertyEditorValue; |
| 631 |
begin |
| 632 |
// on color change in circle |
| 633 |
if Assigned(ColorPropertyEditor) then |
| 634 |
ColorPropertyEditor.SetValue(ColorToHex(CurrentCastleColor)); |
| 635 |
end; |
| 636 |
|
| 637 |
procedure TCastleColorPickerForm.GeneratePascalCode; |
| 638 |
var |
| 639 |
RSingle, GSingle, BSingle: Single; |
| 640 |
HSingle, SSingle, VSingle: Single; |
| 641 |
RText, GText, BText, AText: String; |
| 642 |
HText, SText, VText: String; |
| 643 |
begin |
| 644 |
MemoPascalCode.Lines.Clear; |
| 645 |
|
| 646 |
RSingle := RoundTo(HSPanelCirclePicker.Red / 255, ColorPrecision); |
| 647 |
GSingle := RoundTo(HSPanelCirclePicker.Green / 255, ColorPrecision); |
| 648 |
BSingle := RoundTo(HSPanelCirclePicker.Blue / 255, ColorPrecision); |
| 649 |
HSingle := RoundTo(HSPanelCirclePicker.RelHue * 6, ColorPrecision); |
| 650 |
SSingle := RoundTo(HSPanelCirclePicker.RelSaturation, ColorPrecision); |
| 651 |
VSingle := RoundTo(HSPanelCirclePicker.RelValue, ColorPrecision); |
| 652 |
|
| 653 |
RText := FloatToStrFDot(RSingle, ffFixed, 0, Abs(ColorPrecision)); |
| 654 |
GText := FloatToStrFDot(GSingle, ffFixed, 0, Abs(ColorPrecision)); |
| 655 |
BText := FloatToStrFDot(BSingle, ffFixed, 0, Abs(ColorPrecision)); |
| 656 |
HText := FloatToStrFDot(HSingle, ffFixed, 0, Abs(ColorPrecision)); |
| 657 |
SText := FloatToStrFDot(SSingle, ffFixed, 0, Abs(ColorPrecision)); |
| 658 |
VText := FloatToStrFDot(VSingle, ffFixed, 0, Abs(ColorPrecision)); |
| 659 |
|
| 660 |
AText := FloatToStrFDot(AlphaSpinEdit.Value, ffFixed, 0, Abs(ColorPrecision)); |
| 661 |
|
| 662 |
{ Synchronize generated snippet with tests in tests/code/testcases/testcastlecolors.pas : |
| 663 |
TTestCastleColors.TestColorPickerCodeRgb |
| 664 |
TTestCastleColors.TestColorPickerCodeRgba } |
| 665 |
|
| 666 |
if AlphaColorPicker.Enabled then |
| 667 |
begin |
| 668 |
MemoPascalCode.Lines.Add('// Color constant:'); |
| 669 |
MemoPascalCode.Lines.Add('const'); |
| 670 |
MemoPascalCode.Lines.Add(' MyColor: TCastleColor = (' + |
| 671 |
'X: ' + RText + '; Y: ' + GText + '; Z: ' + BText + '; W: ' + AText + ');'); |
| 672 |
|
| 673 |
MemoPascalCode.Lines.Add(''); |
| 674 |
MemoPascalCode.Lines.Add('// Examples how to set color:'); |
| 675 |
MemoPascalCode.Lines.Add('MyControl.Color := MyColor;'); |
| 676 |
MemoPascalCode.Lines.Add('MyControl.Color := Vector4(' + RText + ', ' + GText + |
| 677 |
', ' + BText + ', ' + AText +');'); |
| 678 |
MemoPascalCode.Lines.Add('MyControl.Color := HsvToRgba(Vector3(' + HText + |
| 679 |
', ' + SText + ', ' + VText + '), ' + AText + ');'); |
| 680 |
MemoPascalCode.Lines.Add('MyControl.Color := HexToColor(''' + ColorToHex(CurrentCastleColor) + ''');'); |
| 681 |
end else |
| 682 |
begin |
| 683 |
MemoPascalCode.Lines.Add('// Color constant:'); |
| 684 |
MemoPascalCode.Lines.Add('const'); |
| 685 |
MemoPascalCode.Lines.Add(' MyColor: TCastleColorRGB = (' + |
| 686 |
'X: ' + RText + '; Y: ' + GText + '; Z: ' + BText + ');'); |
| 687 |
|
| 688 |
MemoPascalCode.Lines.Add(''); |
| 689 |
MemoPascalCode.Lines.Add('// Examples how to set RGB color:'); |
| 690 |
MemoPascalCode.Lines.Add('MyControl.ColorRGB := MyColor;'); |
| 691 |
MemoPascalCode.Lines.Add('MyControl.ColorRGB := Vector3(' + RText + ', ' + GText + |
| 692 |
', ' + BText + ');'); |
| 693 |
MemoPascalCode.Lines.Add('MyControl.ColorRGB := HsvToRgb(Vector3(' + HText + |
| 694 |
', ' + SText + ', ' + VText + '));'); |
| 695 |
MemoPascalCode.Lines.Add('MyControl.ColorRGB := HexToColorRGB(''' + ColorRGBToHex(CurrentCastleColorRGB) + ''');'); |
| 696 |
end; |
| 697 |
|
| 698 |
{ Without this, at least on LCL WinAPI, the memo is scrolled to the end |
| 699 |
after the above operations. |
| 700 |
We prefer to show the memo beginning. } |
| 701 |
MemoPascalCode.SelStart := 0; |
| 702 |
end; |
| 703 |
|
| 704 |
function TCastleColorPickerForm.CurrentCastleColor: TCastleColor; |
| 705 |
var |
| 706 |
ColorByte: TVector4Byte; |
| 707 |
begin |
| 708 |
RedGreenBlue(HSPanelCirclePicker.SelectedColor, ColorByte.X, ColorByte.Y, ColorByte.Z); |
| 709 |
ColorByte.W := AlphaColorPicker.Value; |
| 710 |
Result := Vector4(ColorByte); |
| 711 |
end; |
| 712 |
|
| 713 |
function TCastleColorPickerForm.CurrentCastleColorRGB: TCastleColorRGB; |
| 714 |
var |
| 715 |
CurrentColor: TCastleColor; |
| 716 |
begin |
| 717 |
CurrentColor := CurrentCastleColor; |
| 718 |
Result.X := CurrentColor.X; |
| 719 |
Result.Y := CurrentColor.Y; |
| 720 |
Result.Z := CurrentColor.Z; |
| 721 |
end; |
| 722 |
|
| 723 |
procedure TCastleColorPickerForm.Init( |
| 724 |
const ColorPropEditor: TCastleAbstractColorPropertyEditor; |
| 725 |
const InitColor: TCastleColor; |
| 726 |
const ShowAlpha: Boolean); |
| 727 |
|
| 728 |
function CaptionFromEditor(const PropEditor: TPropertyEditor): String; |
| 729 |
begin |
| 730 |
Result := 'Change '; |
| 731 |
Assert(PropEditor.PropCount > 0); |
| 732 |
|
| 733 |
// add component name |
| 734 |
if PropEditor.GetComponent(0) <> nil then |
| 735 |
begin |
| 736 |
// GetComponent returns TPersistent, not TComponent |
| 737 |
if PropEditor.GetComponent(0) is TComponent then |
| 738 |
Result := Result + TComponent(PropEditor.GetComponent(0)).Name + '.' |
| 739 |
else |
| 740 |
Result := Result + PropEditor.GetComponent(0).ClassName + '.'; |
| 741 |
end; |
| 742 |
|
| 743 |
// add property name |
| 744 |
Result := Result + PropEditor.GetName; |
| 745 |
|
| 746 |
// add ... to signal multiple properties |
| 747 |
if PropEditor.PropCount > 1 then |
| 748 |
Result := Result + '...'; |
| 749 |
end; |
| 750 |
|
| 751 |
begin |
| 752 |
ColorPropertyEditor := ColorPropEditor; |
| 753 |
Caption := CaptionFromEditor(ColorPropEditor); |
| 754 |
|
| 755 |
ColorPrecision := -3; |
| 756 |
ColorEpsilon := 0.0009; |
| 757 |
PrevColor := InitColor; |
| 758 |
SetColorInCirclePickerPanel(InitColor); |
| 759 |
SetColorInRgbTab(InitColor); |
| 760 |
SetColorInHsvTab(InitColor); |
| 761 |
SetAlphaValue(InitColor.W); |
| 762 |
|
| 763 |
PanelAlpha.Visible:= ShowAlpha; |
| 764 |
AlphaColorPicker.Enabled := ShowAlpha; |
| 765 |
AlphaSpinEdit.Enabled := ShowAlpha; |
| 766 |
end; |
| 767 |
|
| 768 |
procedure TCastleColorPickerForm.Init( |
| 769 |
const ColorPropEditor: TCastleAbstractColorPropertyEditor; |
| 770 |
const InitColorRGB: TCastleColorRGB); |
| 771 |
begin |
| 772 |
Init(ColorPropEditor, Vector4(InitColorRGB, 1.0), false); |
| 773 |
end; |
| 774 |
|
| 775 |
end. |
| 776 |
|