summaryrefslogtreecommitdiff
path: root/src/gui/colordialog.inc
blob: 93d8d73135a54513bedda1703396ca144cbb13d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2010 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      This unit contains the Color Selection dialog.
}


{%mainunit fpg_dialogs.pas}

{$IFDEF read_interface}

type

  TfpgColorSelectDialog = class(TfpgBaseDialog)
  private
    {@VFD_HEAD_BEGIN: ColorSelectDialog}
    pcColorSelect: TfpgPageControl;
    tsColorWheel: TfpgTabSheet;
    tsColorNames: TfpgTabSheet;
    cbColorPalette: TfpgComboBox;
    ColorListBox1: TfpgColorListBox;
    Label1: TfpgLabel;
    Label2: TfpgLabel;
    ColorWheel: TfpgColorWheel;
    ValueBar: TfpgValueBar;
    edR: TfpgSpinEdit;
    edG: TfpgSpinEdit;
    edB: TfpgSpinEdit;
    Label3: TfpgLabel;
    Label4: TfpgLabel;
    Label5: TfpgLabel;
    pnlColorPreview: TfpgBevel;
    {@VFD_HEAD_END: ColorSelectDialog}
    FViaRGB: Boolean;  // to prevent recursive changes
    function    GetSelectedColor: TfpgColor;
    procedure   SetSelectedColor(const AValue: TfpgColor);
    procedure   ColorChanged(Sender: TObject);
    procedure   RGBChanged(Sender: TObject);
    procedure   UpdateRGBComponents;
    procedure   PopulatePaletteColorCombo;
    procedure   cbColorPaletteChange(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    procedure   AfterCreate; override;
    property    SelectedColor: TfpgColor read GetSelectedColor write SetSelectedColor;
  end;


{$ENDIF read_interface}



{$IFDEF read_implementation}


function fpgSelectColorDialog(APresetColor: TfpgColor): TfpgColor;
var
  frm: TfpgColorSelectDialog;
begin
  Result := APresetColor;
  frm := TfpgColorSelectDialog.Create(nil);
  try
    frm.ColorWheel.SetSelectedColor(APresetColor);
    if frm.ShowModal = mrOK then
      Result := frm.SelectedColor;
  finally
    frm.Free;
  end;
end;

{ TfpgColorSelectDialog }

function TfpgColorSelectDialog.GetSelectedColor: TfpgColor;
begin
  if pcColorSelect.ActivePageIndex = 0 then
    Result := ValueBar.SelectedColor
  else
    Result := ColorListBox1.Color;
end;

procedure TfpgColorSelectDialog.SetSelectedColor(const AValue: TfpgColor);
begin
  ColorWheel.SetSelectedColor(AValue);
end;

procedure TfpgColorSelectDialog.ColorChanged(Sender: TObject);
begin
//  UpdateHSVComponents;
  if not FViaRGB then
    UpdateRGBComponents;
  pnlColorPreview.BackgroundColor := ValueBar.SelectedColor;
end;

procedure TfpgColorSelectDialog.RGBChanged(Sender: TObject);
var
  rgb: TFPColor;
  c: TfpgColor;
begin
  FViaRGB     := True;  // prevent recursive updates
  rgb.Red     := edR.Value;
  rgb.Green   := edG.Value;
  rgb.Blue    := edB.Value;
  c := FPColorTofpgColor(rgb);
  ColorWheel.SetSelectedColor(c);  // This will trigger ColorWheel and ValueBar OnChange event
  FViaRGB     := False;
end;

procedure TfpgColorSelectDialog.UpdateRGBComponents;
var
  rgb: TFPColor;
  c: TfpgColor;
begin
  c := ValueBar.SelectedColor;
  rgb := fpgColorToFPColor(c);
  edR.Value := rgb.Red;
  edG.Value := rgb.Green;
  edB.Value := rgb.Blue;
end;

procedure TfpgColorSelectDialog.PopulatePaletteColorCombo;
begin
  cbColorPalette.Items.Clear;
  cbColorPalette.Items.Add('cpStandardColors');
  cbColorPalette.Items.Add('cpSystemColors');
  cbColorPalette.Items.Add('cpWebColors');
  cbColorPalette.FocusItem := 0;
  cbColorPalette.OnChange := @cbColorPaletteChange;
end;

procedure TfpgColorSelectDialog.cbColorPaletteChange(Sender: TObject);
begin
  if cbColorPalette.Text = 'cpStandardColors' then
    ColorListBox1.ColorPalette := cpStandardColors
  else if cbColorPalette.Text = 'cpSystemColors' then
    ColorListBox1.ColorPalette := cpSystemColors
  else
    ColorListBox1.ColorPalette := cpWebColors;
  ColorListBox1.SetFocus;
end;

constructor TfpgColorSelectDialog.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FViaRGB := false;
end;


procedure TfpgColorSelectDialog.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: ColorSelectDialog}
  Name := 'ColorSelectDialog';
  SetPosition(340, 164, 328, 375);
  WindowTitle := 'Color Select Dialog';
  Hint := '';
  WindowPosition := wpOneThirdDown;

  pcColorSelect := TfpgPageControl.Create(self);
  with pcColorSelect do
  begin
    Name := 'pcColorSelect';
    SetPosition(4, 4, 320, 332);
    Anchors := [anLeft,anRight,anTop,anBottom];
    ActivePageIndex := 0;
    Hint := '';
    TabOrder := 1;
  end;

  tsColorWheel := TfpgTabSheet.Create(pcColorSelect);
  with tsColorWheel do
  begin
    Name := 'tsColorWheel';
    SetPosition(3, 24, 314, 305);
    Text := 'Color Wheel';
  end;

  tsColorNames := TfpgTabSheet.Create(pcColorSelect);
  with tsColorNames do
  begin
    Name := 'tsColorNames';
    SetPosition(3, 24, 314, 305);
    Text := 'Predefined';
  end;

  cbColorPalette := TfpgComboBox.Create(tsColorNames);
  with cbColorPalette do
  begin
    Name := 'cbColorPalette';
    SetPosition(8, 24, 299, 22);
    Anchors := [anLeft,anRight,anTop];
    FontDesc := '#List';
    Hint := '';
    TabOrder := 1;
  end;

  ColorListBox1 := TfpgColorListBox.Create(tsColorNames);
  with ColorListBox1 do
  begin
    Name := 'ColorListBox1';
    SetPosition(8, 72, 299, 224);
    Anchors := [anLeft,anRight,anTop,anBottom];
    Color := TfpgColor($00FFFF);
    FontDesc := '#List';
    Hint := '';
    TabOrder := 2;
  end;

  Label1 := TfpgLabel.Create(tsColorNames);
  with Label1 do
  begin
    Name := 'Label1';
    SetPosition(8, 6, 328, 16);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Select a color palette';
  end;

  Label2 := TfpgLabel.Create(tsColorNames);
  with Label2 do
  begin
    Name := 'Label2';
    SetPosition(8, 54, 328, 16);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Available colors:';
  end;

  ColorWheel := TfpgColorWheel.Create(tsColorWheel);
  with ColorWheel do
  begin
    Name := 'ColorWheel';
    SetPosition(8, 8, 204, 204);
  end;

  ValueBar := TfpgValueBar.Create(tsColorWheel);
  with ValueBar do
  begin
    Name := 'ValueBar';
    SetPosition(240, 8, 64, 204);
    Value := 1;
    OnChange := @ColorChanged;
  end;

  edR := TfpgSpinEdit.Create(tsColorWheel);
  with edR do
  begin
    Name := 'edR';
    SetPosition(92, 216, 52, 24);
    MaxValue := 255;
    MinValue := 0;
    OnChange := @RGBChanged;
  end;

  edG := TfpgSpinEdit.Create(tsColorWheel);
  with edG do
  begin
    Name := 'edG';
    SetPosition(92, 244, 52, 24);
    MaxValue := 255;
    MinValue := 0;
    OnChange := @RGBChanged;
  end;

  edB := TfpgSpinEdit.Create(tsColorWheel);
  with edB do
  begin
    Name := 'edB';
    SetPosition(92, 272, 52, 24);
    MaxValue := 255;
    MinValue := 0;
    OnChange := @RGBChanged;
  end;

  Label3 := TfpgLabel.Create(tsColorWheel);
  with Label3 do
  begin
    Name := 'Label3';
    SetPosition(8, 220, 80, 16);
    Alignment := taRightJustify;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Red';
  end;

  Label4 := TfpgLabel.Create(tsColorWheel);
  with Label4 do
  begin
    Name := 'Label4';
    SetPosition(8, 248, 80, 16);
    Alignment := taRightJustify;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Green';
  end;

  Label5 := TfpgLabel.Create(tsColorWheel);
  with Label5 do
  begin
    Name := 'Label5';
    SetPosition(8, 276, 80, 16);
    Alignment := taRightJustify;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Blue';
  end;

  pnlColorPreview := TfpgBevel.Create(tsColorWheel);
  with pnlColorPreview do
  begin
    Name := 'pnlColorPreview';
    SetPosition(248, 232, 52, 52);
    Hint := '';
  end;

  {@VFD_BODY_END: ColorSelectDialog}
  {%endregion}

  // link colorwheel and valuebar
  ColorWheel.ValueBar := ValueBar;

  // position standard dialog buttons
  btnCancel.Left  := Width - FDefaultButtonWidth - FSpacing;
  btnCancel.Top   := Height - btnCancel.Height - FSpacing;
  btnOK.Left      := btnCancel.Left - FDefaultButtonWidth - 6;
  btnOK.Top       := btnCancel.Top;
  
  PopulatePaletteColorCombo;
end;


{$ENDIF read_implementation}