1 | unit GR32_Dsgn_ColorPicker;
|
---|
2 |
|
---|
3 | (* ***** BEGIN LICENSE BLOCK *****
|
---|
4 | * Version: MPL 1.1 or LGPL 2.1 with linking exception
|
---|
5 | *
|
---|
6 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
7 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
8 | * the License. You may obtain a copy of the License at
|
---|
9 | * http://www.mozilla.org/MPL/
|
---|
10 | *
|
---|
11 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
13 | * for the specific language governing rights and limitations under the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * Alternatively, the contents of this file may be used under the terms of the
|
---|
17 | * Free Pascal modified version of the GNU Lesser General Public License
|
---|
18 | * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
|
---|
19 | * of this license are applicable instead of those above.
|
---|
20 | * Please see the file LICENSE.txt for additional information concerning this
|
---|
21 | * license.
|
---|
22 | *
|
---|
23 | * The Original Code is Graphics32
|
---|
24 | *
|
---|
25 | * The Initial Developer of the Original Code is
|
---|
26 | * Alex A. Denisov
|
---|
27 | *
|
---|
28 | * Portions created by the Initial Developer are Copyright (C) 2000-2009
|
---|
29 | * the Initial Developer. All Rights Reserved.
|
---|
30 | *
|
---|
31 | * Contributor(s):
|
---|
32 | * Christian-W. Budde
|
---|
33 | *
|
---|
34 | * ***** END LICENSE BLOCK ***** *)
|
---|
35 |
|
---|
36 | interface
|
---|
37 |
|
---|
38 | {$I GR32.inc}
|
---|
39 |
|
---|
40 | uses
|
---|
41 | Classes, SysUtils,
|
---|
42 | {$IFDEF FPC}
|
---|
43 | RTLConsts, LazIDEIntf, PropEdits, Graphics, Dialogs, Forms, Spin, ExtCtrls,
|
---|
44 | StdCtrls, Controls,
|
---|
45 | {$IFDEF Windows}
|
---|
46 | Windows, Registry,
|
---|
47 | {$ENDIF}
|
---|
48 | {$ELSE}
|
---|
49 | Consts,
|
---|
50 | DesignIntf, DesignEditors, VCLEditors, StdCtrls, Controls,
|
---|
51 | Windows, Registry, Graphics, Dialogs, Forms, ExtCtrls, Spin,
|
---|
52 | {$ENDIF}
|
---|
53 | GR32, GR32_ColorPicker, GR32_ColorSwatch;
|
---|
54 |
|
---|
55 | type
|
---|
56 | TFormColorPicker = class(TForm)
|
---|
57 | ButtonCancel: TButton;
|
---|
58 | ButtonOK: TButton;
|
---|
59 | ButtonPickFromScreen: TButton;
|
---|
60 | CheckBoxWebSafe: TCheckBox;
|
---|
61 | ColorPickerAlpha: TColorPickerComponent;
|
---|
62 | ColorPickerBlue: TColorPickerComponent;
|
---|
63 | ColorPickerGreen: TColorPickerComponent;
|
---|
64 | ColorPickerGTK: TColorPickerGTK;
|
---|
65 | ColorPickerRed: TColorPickerComponent;
|
---|
66 | ColorSwatch: TColorSwatch;
|
---|
67 | ColorSwatchAqua: TColorSwatch;
|
---|
68 | ColorSwatchBlack: TColorSwatch;
|
---|
69 | ColorSwatchBlue: TColorSwatch;
|
---|
70 | ColorSwatchFuchsia: TColorSwatch;
|
---|
71 | ColorSwatchGreen: TColorSwatch;
|
---|
72 | ColorSwatchRed: TColorSwatch;
|
---|
73 | ColorSwatchWhite: TColorSwatch;
|
---|
74 | ColorSwatchYellow: TColorSwatch;
|
---|
75 | EditColor: TEdit;
|
---|
76 | LabelAlpha: TLabel;
|
---|
77 | LabelBlue: TLabel;
|
---|
78 | LabelGreen: TLabel;
|
---|
79 | LabelPalette: TLabel;
|
---|
80 | LabelPreview: TLabel;
|
---|
81 | LabelRed: TLabel;
|
---|
82 | LabelWebColor: TLabel;
|
---|
83 | PanelControl: TPanel;
|
---|
84 | SpinEditAlpha: TSpinEdit;
|
---|
85 | SpinEditBlue: TSpinEdit;
|
---|
86 | SpinEditGreen: TSpinEdit;
|
---|
87 | SpinEditRed: TSpinEdit;
|
---|
88 | procedure ButtonPickFromScreenClick(Sender: TObject);
|
---|
89 | procedure ColorPickerChanged(Sender: TObject);
|
---|
90 | procedure SpinEditColorChange(Sender: TObject);
|
---|
91 | procedure CheckBoxWebSafeClick(Sender: TObject);
|
---|
92 | procedure EditColorChange(Sender: TObject);
|
---|
93 | procedure ColorSwatchClick(Sender: TObject);
|
---|
94 | private
|
---|
95 | FColor: TColor32;
|
---|
96 | FScreenColorPickerForm: TScreenColorPickerForm;
|
---|
97 |
|
---|
98 | procedure UpdateColor;
|
---|
99 | procedure ScreenColorPickerMouseMove(Sender: TObject; Shift: TShiftState; X,
|
---|
100 | Y: Integer);
|
---|
101 |
|
---|
102 | procedure SetColor(const Value: TColor32);
|
---|
103 | public
|
---|
104 | property Color: TColor32 read FColor write SetColor;
|
---|
105 | end;
|
---|
106 |
|
---|
107 | implementation
|
---|
108 |
|
---|
109 | {$R *.dfm}
|
---|
110 |
|
---|
111 | { TFormColorPicker }
|
---|
112 |
|
---|
113 | procedure TFormColorPicker.ButtonPickFromScreenClick(Sender: TObject);
|
---|
114 | begin
|
---|
115 | FScreenColorPickerForm := TScreenColorPickerForm.Create(Application);
|
---|
116 | try
|
---|
117 | FScreenColorPickerForm.OnMouseMove := ScreenColorPickerMouseMove;
|
---|
118 | if FScreenColorPickerForm.ShowModal = mrOk then
|
---|
119 | Color := FScreenColorPickerForm.SelectedColor;
|
---|
120 | finally
|
---|
121 | FreeAndNil(FScreenColorPickerForm);
|
---|
122 | end;
|
---|
123 | end;
|
---|
124 |
|
---|
125 | procedure TFormColorPicker.CheckBoxWebSafeClick(Sender: TObject);
|
---|
126 | begin
|
---|
127 | ColorPickerGTK.WebSafe := CheckBoxWebSafe.Checked;
|
---|
128 | ColorPickerRed.WebSafe := CheckBoxWebSafe.Checked;
|
---|
129 | ColorPickerGreen.WebSafe := CheckBoxWebSafe.Checked;
|
---|
130 | ColorPickerBlue.WebSafe := CheckBoxWebSafe.Checked;
|
---|
131 | ColorPickerAlpha.WebSafe := CheckBoxWebSafe.Checked;
|
---|
132 | end;
|
---|
133 |
|
---|
134 | procedure TFormColorPicker.ColorPickerChanged(Sender: TObject);
|
---|
135 | begin
|
---|
136 | Color := TCustomColorPicker(Sender).SelectedColor;
|
---|
137 | end;
|
---|
138 |
|
---|
139 | procedure TFormColorPicker.ColorSwatchClick(Sender: TObject);
|
---|
140 | begin
|
---|
141 | Color := TColorSwatch(Sender).Color;
|
---|
142 | end;
|
---|
143 |
|
---|
144 | procedure TFormColorPicker.EditColorChange(Sender: TObject);
|
---|
145 | var
|
---|
146 | ColorText: string;
|
---|
147 | Value: Integer;
|
---|
148 | begin
|
---|
149 | ColorText := StringReplace(EditColor.Text, '#', '$', []);
|
---|
150 | if TryStrToInt(ColorText, Value) then
|
---|
151 | Color := Value;
|
---|
152 | end;
|
---|
153 |
|
---|
154 | procedure TFormColorPicker.ScreenColorPickerMouseMove(Sender: TObject;
|
---|
155 | Shift: TShiftState; X, Y: Integer);
|
---|
156 | begin
|
---|
157 | Color := FScreenColorPickerForm.SelectedColor;
|
---|
158 | end;
|
---|
159 |
|
---|
160 | procedure TFormColorPicker.SetColor(const Value: TColor32);
|
---|
161 | begin
|
---|
162 | if FColor <> Value then
|
---|
163 | begin
|
---|
164 | FColor := Value;
|
---|
165 | UpdateColor;
|
---|
166 | end;
|
---|
167 | end;
|
---|
168 |
|
---|
169 | procedure TFormColorPicker.SpinEditColorChange(Sender: TObject);
|
---|
170 | begin
|
---|
171 | EditColor.OnChange := nil;
|
---|
172 | Color :=
|
---|
173 | SpinEditAlpha.Value shl 24 +
|
---|
174 | SpinEditRed.Value shl 16 +
|
---|
175 | SpinEditGreen.Value shl 8 +
|
---|
176 | SpinEditBlue.Value;
|
---|
177 | EditColor.OnChange := EditColorChange;
|
---|
178 | end;
|
---|
179 |
|
---|
180 | procedure TFormColorPicker.UpdateColor;
|
---|
181 | var
|
---|
182 | R, G, B, A: Byte;
|
---|
183 | SelStart: Integer;
|
---|
184 | begin
|
---|
185 | // disable OnChange handler
|
---|
186 | EditColor.OnChange := nil;
|
---|
187 | SpinEditRed.OnChange := nil;
|
---|
188 | SpinEditGreen.OnChange := nil;
|
---|
189 | SpinEditBlue.OnChange := nil;
|
---|
190 | SpinEditAlpha.OnChange := nil;
|
---|
191 |
|
---|
192 | ColorPickerGTK.SelectedColor := FColor;
|
---|
193 | ColorPickerRed.SelectedColor := FColor;
|
---|
194 | ColorPickerGreen.SelectedColor := FColor;
|
---|
195 | ColorPickerBlue.SelectedColor := FColor;
|
---|
196 | ColorPickerAlpha.SelectedColor := FColor;
|
---|
197 | ColorSwatch.Color := FColor;
|
---|
198 |
|
---|
199 | // update spin edits
|
---|
200 | Color32ToRGBA(FColor, R, G, B, A);
|
---|
201 | SpinEditRed.Value := R;
|
---|
202 | SpinEditGreen.Value := G;
|
---|
203 | SpinEditBlue.Value := B;
|
---|
204 | SpinEditAlpha.Value := A;
|
---|
205 |
|
---|
206 | // update color edit
|
---|
207 | SelStart := EditColor.SelStart;
|
---|
208 | EditColor.Text := '#' + IntToHex(A, 2) + IntToHex(R, 2) + IntToHex(G, 2) + IntToHex(B, 2);
|
---|
209 | EditColor.SelStart := SelStart;
|
---|
210 |
|
---|
211 | // re-enable OnChange handler
|
---|
212 | SpinEditRed.OnChange := SpinEditColorChange;
|
---|
213 | SpinEditGreen.OnChange := SpinEditColorChange;
|
---|
214 | SpinEditBlue.OnChange := SpinEditColorChange;
|
---|
215 | SpinEditAlpha.OnChange := SpinEditColorChange;
|
---|
216 | EditColor.OnChange := EditColorChange;
|
---|
217 | end;
|
---|
218 |
|
---|
219 | end.
|
---|
220 |
|
---|