2006-06-21 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ColorDialog.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Alexander Olk   xenomorph2@onlinehome.de
24 //
25
26 // COMPLETE
27
28 using System.ComponentModel;
29 using System.Drawing;
30 using System.Globalization;
31 using System.Resources;
32 using System;
33
34 namespace System.Windows.Forms {
35         [DefaultProperty ("Color")]
36         public class ColorDialog : CommonDialog {
37                 #region Local Variables
38                 private bool allowFullOpen = true;
39                 private bool anyColor = false;
40                 private Color color;
41                 private int[] customColors = null;
42                 private bool fullOpen = false;
43                 private bool showHelp = false;
44                 private bool solidColorOnly = false;
45                 
46                 private Panel selectedColorPanel;
47                 private BaseColorControl baseColorControl;
48                 private ColorMatrixControl colorMatrixControl;
49                 private BrightnessControl brightnessControl;
50                 private TriangleControl triangleControl;
51                 
52                 private Button okButton;
53                 private Button cancelButton;
54                 private Button helpButton;
55                 private Button addColoursButton;
56                 private Button defineColoursButton;
57                 
58                 private TextBox hueTextBox;
59                 private TextBox satTextBox;
60                 private TextBox briTextBox;
61                 private TextBox redTextBox;
62                 private TextBox greenTextBox;
63                 private TextBox blueTextBox;
64                 
65                 private Label briLabel;
66                 private Label satLabel;
67                 private Label hueLabel;
68                 private Label colorBaseLabel;
69                 private Label greenLabel;
70                 private Label blueLabel;
71                 private Label redLabel;
72                 #endregion      // Local Variables
73                 
74                 #region Public Constructors
75                 public ColorDialog () : base()
76                 {
77                         form.SuspendLayout ();
78                         
79                         form.Text = "Color";
80                         
81                         form.Size = new Size (221, 332); // 300
82                         
83                         form.FormBorderStyle = FormBorderStyle.FixedDialog;
84                         form.MaximizeBox = false;
85                         
86                         satTextBox = new TextBox ();
87                         briTextBox = new TextBox ();
88                         blueTextBox = new TextBox ();
89                         greenTextBox = new TextBox ();
90                         redTextBox = new TextBox ();
91                         hueTextBox = new TextBox ();
92                         
93                         redLabel = new Label ();
94                         blueLabel = new Label ();
95                         greenLabel = new Label ();
96                         colorBaseLabel = new Label ();
97                         hueLabel = new Label ();
98                         satLabel = new Label ();
99                         briLabel = new Label ();
100                         
101                         okButton = new Button ();
102                         cancelButton = new Button ();
103                         helpButton = new Button ();
104                         defineColoursButton = new Button ();
105                         addColoursButton = new Button ();
106                         
107                         baseColorControl = new BaseColorControl (this);
108                         colorMatrixControl = new ColorMatrixControl (this);
109                         brightnessControl = new BrightnessControl (this);
110                         triangleControl = new TriangleControl (this);
111                         
112                         selectedColorPanel = new Panel ();
113                         
114                         // hueTextBox
115                         hueTextBox.Location = new Point (324, 203);
116                         hueTextBox.Size = new Size (27, 21);
117                         hueTextBox.TabIndex = 11;
118                         hueTextBox.MaxLength = 3;
119                         // satTextBox
120                         satTextBox.Location = new Point (324, 225);
121                         satTextBox.Size = new Size (27, 21);
122                         satTextBox.TabIndex = 15;
123                         satTextBox.MaxLength = 3;
124                         // greenTextBox
125                         greenTextBox.Location = new Point (404, 225);
126                         greenTextBox.Size = new Size (27, 21);
127                         greenTextBox.TabIndex = 18;
128                         greenTextBox.MaxLength = 3;
129                         // briTextBox
130                         briTextBox.Location = new Point (324, 247);
131                         briTextBox.Size = new Size (27, 21);
132                         briTextBox.TabIndex = 16;
133                         briTextBox.MaxLength = 3;
134                         // blueTextBox
135                         blueTextBox.Location = new Point (404, 247);
136                         blueTextBox.Size = new Size (27, 21);
137                         blueTextBox.TabIndex = 19;
138                         blueTextBox.MaxLength = 3;
139                         // redTextBox
140                         redTextBox.Location = new Point (404, 203);
141                         redTextBox.Size = new Size (27, 21);
142                         redTextBox.TabIndex = 17;
143                         redTextBox.MaxLength = 3;
144                         
145                         // redLabel
146                         redLabel.FlatStyle = FlatStyle.System;
147                         redLabel.Location = new Point (361, 206);
148                         redLabel.Size = new Size (40, 16);
149                         redLabel.TabIndex = 25;
150                         redLabel.Text = Locale.GetText ("Red") + ":";
151                         redLabel.TextAlign = ContentAlignment.MiddleRight;
152                         // blueLabel
153                         blueLabel.FlatStyle = FlatStyle.System;
154                         blueLabel.Location = new Point (361, 250);
155                         blueLabel.Size = new Size (40, 16);
156                         blueLabel.TabIndex = 26;
157                         blueLabel.Text = Locale.GetText ("Blue") + ":";
158                         blueLabel.TextAlign = ContentAlignment.MiddleRight;
159                         // greenLabel
160                         greenLabel.FlatStyle = FlatStyle.System;
161                         greenLabel.Location = new Point (361, 228);
162                         greenLabel.Size = new Size (40, 16);
163                         greenLabel.TabIndex = 27;
164                         greenLabel.Text = Locale.GetText ("Green") + ":";
165                         greenLabel.TextAlign = ContentAlignment.MiddleRight;
166                         // colorBaseLabel
167                         colorBaseLabel.Location = new Point (228, 247);
168                         colorBaseLabel.Size = new Size (60, 25);
169                         colorBaseLabel.TabIndex = 28;
170                         colorBaseLabel.Text = Locale.GetText ("Color");
171                         colorBaseLabel.TextAlign = ContentAlignment.MiddleCenter;
172                         // hueLabel
173                         hueLabel.FlatStyle = FlatStyle.System;
174                         hueLabel.Location = new Point (287, 206);
175                         hueLabel.Size = new Size (36, 16);
176                         hueLabel.TabIndex = 23;
177                         hueLabel.Text = Locale.GetText ("Hue") + ":";
178                         hueLabel.TextAlign = ContentAlignment.MiddleRight;
179                         // satLabel
180                         satLabel.FlatStyle = FlatStyle.System;
181                         satLabel.Location = new Point (287, 228);
182                         satLabel.Size = new Size (36, 16);
183                         satLabel.TabIndex = 22;
184                         satLabel.Text = Locale.GetText ("Sat") + ":";
185                         satLabel.TextAlign = ContentAlignment.MiddleRight;
186                         // briLabel
187                         briLabel.FlatStyle = FlatStyle.System;
188                         briLabel.Location = new Point (287, 250);
189                         briLabel.Size = new Size (36, 16);
190                         briLabel.TabIndex = 24;
191                         briLabel.Text = Locale.GetText ("Bri") + ":";
192                         briLabel.TextAlign = ContentAlignment.MiddleRight;
193                         
194                         // defineColoursButton
195                         defineColoursButton.FlatStyle = FlatStyle.System;
196                         defineColoursButton.Location = new Point (5, 244);
197                         defineColoursButton.Size = new Size (210, 22);
198                         defineColoursButton.TabIndex = 6;
199                         defineColoursButton.Text = Locale.GetText ("Define Colors >>");
200                         // okButton
201                         okButton.FlatStyle = FlatStyle.System;
202                         okButton.Location = new Point (5, 271);
203                         okButton.Size = new Size (66, 22);
204                         okButton.TabIndex = 0;
205                         okButton.Text = Locale.GetText ("OK");
206                         // cancelButton
207                         cancelButton.FlatStyle = FlatStyle.System;
208                         cancelButton.Location = new Point (78, 271);
209                         cancelButton.Size = new Size (66, 22);
210                         cancelButton.TabIndex = 1;
211                         cancelButton.Text = Locale.GetText ("Cancel");
212                         // helpButton
213                         helpButton.FlatStyle = FlatStyle.System;
214                         helpButton.Location = new Point (149, 271);
215                         helpButton.Size = new Size (66, 22);
216                         helpButton.TabIndex = 5;
217                         helpButton.Text = Locale.GetText ("Help");
218                         helpButton.Hide ();
219                         
220                         // addColoursButton
221                         addColoursButton.FlatStyle = FlatStyle.System;
222                         addColoursButton.Location = new Point (227, 271);
223                         addColoursButton.Size = new Size (213, 22);
224                         addColoursButton.TabIndex = 7;
225                         addColoursButton.Text =  Locale.GetText ("Add Colors");
226                         
227                         // baseColorControl
228                         baseColorControl.Location = new Point (3, 6);
229                         baseColorControl.Size = new Size (212, 231);
230                         baseColorControl.TabIndex = 13;
231                         // colorMatrixControl
232                         colorMatrixControl.Location = new Point (227, 7);
233                         colorMatrixControl.Size = new Size (179, 190);
234                         colorMatrixControl.TabIndex = 14;
235                         // triangleControl
236                         triangleControl.Location = new Point (432, 0);
237                         triangleControl.Size = new Size (16, 204);
238                         triangleControl.TabIndex = 12;
239                         // brightnessControl
240                         brightnessControl.Location = new Point (415, 7);
241                         brightnessControl.Size = new Size (14, 190);
242                         brightnessControl.TabIndex = 20;
243                         
244                         // selectedColorPanel
245                         selectedColorPanel.BackColor = SystemColors.Desktop;
246                         selectedColorPanel.BorderStyle = BorderStyle.Fixed3D;
247                         selectedColorPanel.Location = new Point (227, 202);
248                         selectedColorPanel.Size = new Size (60, 42);
249                         selectedColorPanel.TabIndex = 10;
250                         
251                         form.Controls.Add (hueTextBox);
252                         form.Controls.Add (satTextBox);
253                         form.Controls.Add (briTextBox);
254                         form.Controls.Add (redTextBox);
255                         form.Controls.Add (greenTextBox);
256                         form.Controls.Add (blueTextBox);
257                         
258                         form.Controls.Add (defineColoursButton);
259                         form.Controls.Add (okButton);
260                         form.Controls.Add (cancelButton);
261                         form.Controls.Add (addColoursButton);
262                         
263                         form.Controls.Add (baseColorControl);
264                         form.Controls.Add (colorMatrixControl);
265                         form.Controls.Add (brightnessControl);
266                         form.Controls.Add (triangleControl);
267                         
268                         form.Controls.Add (colorBaseLabel);
269                         form.Controls.Add (greenLabel);
270                         form.Controls.Add (blueLabel);
271                         form.Controls.Add (redLabel);
272                         form.Controls.Add (briLabel);
273                         form.Controls.Add (hueLabel);
274                         form.Controls.Add (satLabel);
275                         
276                         form.Controls.Add (selectedColorPanel);
277                         
278                         form.ResumeLayout (false);
279                         
280                         Color = Color.Black;
281                         
282                         defineColoursButton.Click += new EventHandler (OnClickButtonDefineColours);
283                         addColoursButton.Click += new EventHandler (OnClickButtonAddColours);
284                         helpButton.Click += new EventHandler (OnClickHelpButton);
285                         cancelButton.Click += new EventHandler (OnClickCancelButton);
286                         okButton.Click += new EventHandler (OnClickOkButton);
287                         
288                         hueTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
289                         satTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
290                         briTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
291                         redTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
292                         greenTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
293                         blueTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
294                         
295                         hueTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
296                         satTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
297                         briTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
298                         redTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
299                         greenTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
300                         blueTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
301                         
302                         hueTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
303                         satTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
304                         briTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
305                         redTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
306                         greenTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
307                         blueTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
308                         
309                         hueTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
310                         satTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
311                         briTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
312                         redTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
313                         greenTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
314                         blueTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
315                 }
316                 #endregion      // Public Constructors
317                 
318                 #region Public Instance Properties
319                 public Color Color {
320                         get {
321                                 return selectedColorPanel.BackColor;
322                         }
323                         
324                         set {
325                                 if (value.IsEmpty) {
326                                         color = Color.Black;
327                                         baseColorControl.SetColor (color);
328                                 } else if (color != value) {
329                                         color = value;
330                                         baseColorControl.SetColor (color);
331                                 }
332                         }
333                 }
334                 
335                 [DefaultValue(true)]
336                 public virtual bool AllowFullOpen {
337                         get {
338                                 return allowFullOpen;
339                         }
340                         
341                         set {
342                                 if (allowFullOpen != value) {
343                                         allowFullOpen = value;
344                                         
345                                         if (!allowFullOpen)
346                                                 defineColoursButton.Enabled = false;
347                                         else
348                                                 defineColoursButton.Enabled = true;
349                                 }
350                         }
351                 }
352                 
353                 // Currently AnyColor internally is always true
354                 // Does really anybody still use 256 or less colors ???
355                 // Naw, cairo only supports 24bit anyway - pdb
356                 [DefaultValue(false)]
357                 public virtual bool AnyColor {
358                         get {
359                                 return anyColor;
360                         }
361                         
362                         set {
363                                 anyColor = value;
364                         }
365                 }
366                 
367                 [DefaultValue(false)]
368                 public virtual bool FullOpen {
369                         get {
370                                 return fullOpen;
371                         }
372                         
373                         set {
374                                 if (fullOpen != value) {
375                                         fullOpen = value;
376                                         
377                                         if (fullOpen && allowFullOpen) {
378                                                 defineColoursButton.Enabled = false;
379                                                 
380                                                 colorMatrixControl.ColorToShow = baseColorControl.ColorToShow;
381                                                 
382                                                 form.Size = new Size (448, 332);
383                                         } else {
384                                                 if (allowFullOpen)
385                                                         defineColoursButton.Enabled = true;
386                                                 
387                                                 form.Size = new Size (221, 332); // 300
388                                         }
389                                 }
390                         }
391                 }
392                 
393                 [Browsable(false)]
394                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
395                 public int[] CustomColors {
396                         get {
397                                 return customColors;
398                         }
399                         
400                         set {
401                                 if (allowFullOpen) {
402                                         customColors = value;
403                                         
404                                         baseColorControl.SetCustomColors ();
405                                 }
406                         }
407                 }
408                 
409                 [DefaultValue(false)]
410                 public virtual bool ShowHelp {
411                         get {
412                                 return showHelp;
413                         }
414                         
415                         set {
416                                 if (showHelp != value) {
417                                         showHelp = value;
418                                         if (showHelp)
419                                                 helpButton.Show ();
420                                         else
421                                                 helpButton.Hide ();
422                                 }
423                         }
424                 }
425                 
426                 [DefaultValue(false)]
427                 public virtual bool SolidColorOnly {
428                         get {
429                                 return solidColorOnly;
430                         }
431                         
432                         set {
433                                 solidColorOnly = value;
434                         }
435                 }
436                 #endregion      // Public Instance Properties
437                 
438                 #region Public Instance Methods
439                 public override void Reset ()
440                 {
441                         AllowFullOpen = true;
442                         anyColor = false;
443                         Color = Color.Black;
444                         CustomColors = null;
445                         FullOpen = false;
446                         ShowHelp = false;
447                         solidColorOnly = false;
448                 }
449                 
450                 public override string ToString ()
451                 {
452                         return base.ToString () + ", Color: " + Color.ToString ();
453                 }
454                 #endregion      // Public Instance Methods
455                 
456                 #region Protected Instance Properties
457                 protected virtual IntPtr Instance {
458                         get {
459                                 // MS Internal
460                                 return (IntPtr)GetHashCode ();
461                         }
462                 }
463                 
464                 protected virtual int Options {
465                         get {
466                                 // MS Internal
467                                 return 0;
468                         }
469                 }
470                 #endregion      // Protected Instance Properties
471                 
472                 #region Protected Instance Methods
473                 protected override bool RunDialog (IntPtr hwndOwner)
474                 {
475                         // currently needed, otherwise there are a lot of drawing artefacts/transparent controls if the same dialog gets opened again
476                         form.Refresh ();
477                         
478                         return true;
479                 }
480                 #endregion      // Protected Instance Methods
481                 
482                 #region Private Methods         
483                 void OnClickCancelButton (object sender, EventArgs e)
484                 {
485                         form.DialogResult = DialogResult.Cancel;
486                 }
487                 
488                 void OnClickOkButton (object sender, EventArgs e)
489                 {
490                         form.DialogResult = DialogResult.OK;
491                 }
492                 
493                 void OnClickButtonAddColours (object sender, EventArgs e)
494                 {
495                         baseColorControl.SetUserColor (selectedColorPanel.BackColor);
496                 }
497                 
498                 void OnClickButtonDefineColours (object sender, EventArgs e)
499                 {
500                         if (allowFullOpen) {
501                                 defineColoursButton.Enabled = false;
502                                 
503                                 colorMatrixControl.ColorToShow = baseColorControl.ColorToShow;
504                                 
505                                 form.Size = new Size (448, 332);
506                         }
507                 }
508                 
509                 // FIXME: Is this correct ?
510                 void OnClickHelpButton (object sender, EventArgs e)
511                 {
512                         OnHelpRequest (e);
513                 }
514                 
515                 string textBox_text_old = "";
516                 
517                 void OnGotFocusTextBoxes (object sender, EventArgs e)
518                 {
519                         TextBox textbox = sender as TextBox;
520                         textBox_text_old = textbox.Text;
521                 }
522                 
523                 void OnLostFocusTextBoxes (object sender, EventArgs e)
524                 {
525                         TextBox textbox = sender as TextBox;
526                         
527                         if (textbox.Text.Length == 0)
528                                 textbox.Text = textBox_text_old;
529                 }
530                 
531                 void OnKeyPressTextBoxes (object sender, KeyPressEventArgs e)
532                 {
533                         if (Char.IsLetter (e.KeyChar) || Char.IsWhiteSpace (e.KeyChar) || Char.IsPunctuation (e.KeyChar) || e.KeyChar == ',') {
534                                 e.Handled = true;
535                                 return; 
536                         }
537                         
538                         internal_textbox_change = true;
539                 }
540                 
541                 internal TextBox edit_textbox = null;
542                 bool internal_textbox_change = false;
543                 
544                 void OnTextChangedTextBoxes (object sender, EventArgs e)
545                 {
546                         if (!internal_textbox_change)
547                                 return;
548                         
549                         internal_textbox_change = false;
550                         
551                         TextBox tmp_box = sender as TextBox;
552                         
553                         if (tmp_box.Text.Length == 0)
554                                 return;
555                         
556                         string text = tmp_box.Text;
557                         
558                         int val = 0;
559                         
560                         try {
561                                 val = System.Convert.ToInt32 (text);
562                         } catch (Exception) {
563                                 // bla
564                         }
565                         
566                         if (sender == hueTextBox) {
567                                 if (val > 239) {
568                                         val = 239;
569                                         hueTextBox.Text = val.ToString ();
570                                 } else
571                                 if (val < 0) {
572                                         val = 0;
573                                         hueTextBox.Text = val.ToString ();
574                                 }
575                                 
576                                 edit_textbox = hueTextBox;
577                                 
578                                 UpdateFromHSBTextBoxes ();
579                                 
580                                 UpdateControls (selectedColorPanel.BackColor);
581                         } else
582                         if (sender == satTextBox) {
583                                 if (val > 240) {
584                                         val = 240;
585                                         satTextBox.Text = val.ToString ();
586                                 } else
587                                 if (val < 0) {
588                                         val = 0;
589                                         satTextBox.Text = val.ToString ();
590                                 }
591                                 
592                                 edit_textbox = satTextBox;
593                                 
594                                 UpdateFromHSBTextBoxes ();
595                                 
596                                 UpdateControls (selectedColorPanel.BackColor);
597                         } else
598                         if (sender == briTextBox) {
599                                 if (val > 240) {
600                                         val = 240;
601                                         briTextBox.Text = val.ToString ();
602                                 } else
603                                 if (val < 0) {
604                                         val = 0;
605                                         briTextBox.Text = val.ToString ();
606                                 }
607                                 
608                                 edit_textbox = briTextBox;
609                                 
610                                 UpdateFromHSBTextBoxes ();
611                                 
612                                 UpdateControls (selectedColorPanel.BackColor);
613                         } else
614                         if (sender == redTextBox) {
615                                 if (val > 255) {
616                                         val = 255;
617                                         redTextBox.Text = val.ToString ();
618                                 } else
619                                 if (val < 0) {
620                                         val = 0;
621                                         redTextBox.Text = val.ToString ();
622                                 }
623                                 
624                                 edit_textbox = redTextBox;
625                                 
626                                 UpdateFromRGBTextBoxes ();
627                         } else
628                         if (sender == greenTextBox) {
629                                 if (val > 255) {
630                                         val = 255;
631                                         greenTextBox.Text = val.ToString ();
632                                 } else
633                                 if (val < 0) {
634                                         val = 0;
635                                         greenTextBox.Text = val.ToString ();
636                                 }
637                                 
638                                 edit_textbox = greenTextBox;
639                                 
640                                 UpdateFromRGBTextBoxes ();
641                         } else
642                         if (sender == blueTextBox) {
643                                 if (val > 255) {
644                                         val = 255;
645                                         blueTextBox.Text = val.ToString ();
646                                 } else
647                                 if (val < 0) {
648                                         val = 0;
649                                         blueTextBox.Text = val.ToString ();
650                                 }
651                                 
652                                 edit_textbox = blueTextBox;
653                                 
654                                 UpdateFromRGBTextBoxes ();
655                         }
656                         
657                         textBox_text_old = edit_textbox.Text;
658                         
659                         edit_textbox = null;
660                 }
661                 
662                 internal void UpdateControls (Color acolor)
663                 {
664                         selectedColorPanel.BackColor = acolor;
665                         colorMatrixControl.ColorToShow = acolor;
666                         brightnessControl.ColorToShow = acolor;
667                         triangleControl.ColorToShow = acolor;
668                 }
669                 
670                 internal void UpdateRGBTextBoxes (Color acolor)
671                 {
672                         if (edit_textbox != redTextBox)
673                                 redTextBox.Text = acolor.R.ToString ();
674                         if (edit_textbox != greenTextBox)
675                                 greenTextBox.Text = acolor.G.ToString ();
676                         if (edit_textbox != blueTextBox)
677                                 blueTextBox.Text = acolor.B.ToString ();
678                 }
679                 
680                 internal void UpdateHSBTextBoxes (Color acolor)
681                 {
682                         HSB hsb = HSB.RGB2HSB (acolor);
683                         
684                         if (edit_textbox != hueTextBox)
685                                 hueTextBox.Text = hsb.hue.ToString ();
686                         if (edit_textbox != satTextBox)
687                                 satTextBox.Text = hsb.sat.ToString ();
688                         if (edit_textbox != briTextBox)
689                                 briTextBox.Text = hsb.bri.ToString ();
690                 }
691                 
692                 internal void UpdateFromHSBTextBoxes ()
693                 {
694                         Color col = HSB.HSB2RGB (System.Convert.ToInt32 (hueTextBox.Text),
695                                                  System.Convert.ToInt32 (satTextBox.Text),
696                                                  System.Convert.ToInt32 (briTextBox.Text));
697                         
698                         selectedColorPanel.BackColor = col;
699                         UpdateRGBTextBoxes (col);
700                 }
701                 
702                 internal void UpdateFromRGBTextBoxes ()
703                 {
704                         Color col = Color.FromArgb (System.Convert.ToInt32 (redTextBox.Text),
705                                                     System.Convert.ToInt32 (greenTextBox.Text),
706                                                     System.Convert.ToInt32 (blueTextBox.Text));
707                         
708                         selectedColorPanel.BackColor = col;
709                         
710                         UpdateHSBTextBoxes (col);
711                         
712                         UpdateControls (col);
713                 }
714                 #endregion
715                 
716                 #region Internal structs and classes
717                 internal struct HSB {
718                         public int hue;
719                         public int sat;
720                         public int bri;
721                         
722                         public static HSB RGB2HSB (Color color)
723                         {
724                                 HSB hsb = new HSB ();
725                                 
726                                 hsb.hue = (int)((color.GetHue () / 360.0f) * 240);
727                                 hsb.sat = (int)(color.GetSaturation () * 241);
728                                 hsb.bri = (int)(color.GetBrightness () * 241);
729                                 
730                                 if (hsb.hue > 239) hsb.hue = 239;
731                                 if (hsb.sat > 240) hsb.sat = 240;
732                                 if (hsb.bri > 240) hsb.bri = 240;
733                                 
734                                 return hsb;
735                         }
736                         
737                         // not using ControlPaint HBS2Color, this algo is more precise
738                         public static Color HSB2RGB (int hue, int saturation, int brightness)
739                         {
740                                 if (hue > 239)
741                                         hue = 239;
742                                 else
743                                 if (hue < 0)
744                                         hue = 0;
745                                 
746                                 if (saturation > 240)
747                                         saturation = 240;
748                                 else
749                                 if (saturation < 0)
750                                         saturation = 0;
751                                 
752                                 if (brightness > 240)
753                                         brightness = 240;
754                                 else
755                                 if (brightness < 0)
756                                         brightness = 0;
757                                 
758                                 float H = hue / 239.0f;
759                                 float S = saturation / 240.0f;
760                                 float L = brightness / 240.0f;
761                                 
762                                 float r = 0, g = 0, b = 0;
763                                 float d1, d2;
764                                 
765                                 if (L == 0) {
766                                         r = g =  b = 0;
767                                 } else {
768                                         if (S == 0) {
769                                                 r = g = b = L;
770                                         } else {
771                                                 d2 = (L <= 0.5f) ? L * (1.0f + S) : L + S - (L * S);
772                                                 d1 = 2.0f * L - d2;
773                                                 
774                                                 float[] d3 = new float [] { H + 1.0f / 3.0f , H, H - 1.0f / 3.0f };
775                                                 float[] rgb = new float [] { 0,0,0 };
776                                                 
777                                                 for (int i = 0; i < 3; i++) {
778                                                         if (d3 [i] < 0)
779                                                                 d3 [i] += 1.0f;
780                                                         if (d3 [i] > 1.0f)
781                                                                 d3 [i] -= 1.0f;
782                                                         
783                                                         if (6.0f * d3 [i] < 1.0f)
784                                                                 rgb [i] = d1 + (d2 - d1) * d3 [i] * 6.0f;
785                                                         else
786                                                         if (2.0f * d3 [i] < 1.0f)
787                                                                 rgb [i] = d2;
788                                                         else
789                                                         if (3.0f * d3 [i] < 2.0f)
790                                                                 rgb [i] = (d1 + (d2 - d1) * ((2.0f / 3.0f) - d3 [i]) * 6.0f);
791                                                         else
792                                                                 rgb [i] = d1;
793                                                 }
794                                                 
795                                                 r = rgb [0];
796                                                 g = rgb [1];
797                                                 b = rgb [2];
798                                         }
799                                 }
800                                 
801                                 r = 255.0f * r;
802                                 g = 255.0f * g;
803                                 b = 255.0f * b;
804                                 
805                                 if (r < 1)
806                                         r = 0.0f;
807                                 else
808                                 if (r > 255.0f)
809                                         r = 255.0f;
810                                 
811                                 if (g < 1)
812                                         g = 0.0f;
813                                 else
814                                 if (g > 255.0f)
815                                         g = 255.0f;
816                                 
817                                 if (b < 1)
818                                         b = 0.0f;
819                                 else
820                                 if (b > 255.0f)
821                                         b = 255.0f;
822                                 
823                                 return Color.FromArgb ((int)r, (int)g, (int)b);
824                         }
825                         
826                         public static int Brightness (Color color)
827                         {
828                                 return (int)(color.GetBrightness () * 241);
829                         }
830                         
831                         public static void GetHueSaturation (Color color, out int hue, out int sat)
832                         {
833                                 hue = (int)((color.GetHue () / 360.0f) * 240);
834                                 sat = (int)(color.GetSaturation () * 241);
835                         }
836                         
837                         // only for testing
838                         // there are some small glitches, but it is still better than ControlPaint implementation
839                         public static void TestColor (Color color)
840                         {
841                                 Console.WriteLine ("Color: " + color);
842                                 HSB hsb = HSB.RGB2HSB (color);
843                                 Console.WriteLine ("RGB2HSB: " + hsb.hue + ", " + hsb.sat + ", " + hsb.bri);
844                                 Console.WriteLine ("HSB2RGB: " + HSB.HSB2RGB (hsb.hue, hsb.sat, hsb.bri));
845                                 Console.WriteLine ();
846                         }
847                 }
848                 
849                 internal class BaseColorControl : Control {
850                         internal class SmallColorControl : Control {
851                                 private Color internalcolor;
852                                 
853                                 private bool isSelected = false;
854                                 
855                                 public SmallColorControl (Color color)
856                                 {
857                                         SuspendLayout ();
858                                         
859                                         this.internalcolor = color;
860                                         
861                                         Size = new Size (25, 23);
862                                         
863                                         ResumeLayout (false);
864                                         
865                                         SetStyle (ControlStyles.DoubleBuffer, true);
866                                         SetStyle (ControlStyles.AllPaintingInWmPaint, true);
867                                         SetStyle (ControlStyles.UserPaint, true);
868                                         SetStyle (ControlStyles.Selectable, true);
869                                 }
870                                 
871                                 public bool IsSelected {
872                                         set {
873                                                 isSelected = value;
874                                                 Invalidate ();
875                                         }
876                                 }
877                                 
878                                 public Color InternalColor {
879                                         set {
880                                                 internalcolor = value;
881                                                 Invalidate ();
882                                         }
883                                         
884                                         get {
885                                                 return internalcolor;
886                                         }
887                                 }
888                                 
889                                 protected override void OnPaint (PaintEventArgs pe)
890                                 {
891                                         base.OnPaint (pe);
892                                         
893                                         pe.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (internalcolor),
894                                                                    new Rectangle (4, 4, 17, 15));
895                                         
896                                         ControlPaint.DrawBorder3D (pe.Graphics, 3, 3, 19, 17, Border3DStyle.Sunken);
897                                         
898                                         if (isSelected) {
899                                                 pe.Graphics.DrawRectangle (ThemeEngine.Current.ResPool.GetPen (Color.Black),
900                                                                            new Rectangle (2, 2, 20, 18));
901                                         }
902                                         
903                                         if (Focused) {
904                                                 ControlPaint.DrawFocusRectangle (
905                                                         pe.Graphics, new Rectangle (0, 0, 25, 23)
906                                                 );
907                                         }
908                                 }
909                                 
910                                 protected override void OnClick (EventArgs e)
911                                 {
912                                         Focus ();
913                                         IsSelected = true;
914                                         
915                                         base.OnClick (e);
916                                 }
917                                 
918                                 
919                                 protected override void OnLostFocus (EventArgs e)
920                                 {
921                                         Invalidate ();
922                                         
923                                         base.OnLostFocus (e);
924                                 }
925                         }
926                         
927                         private SmallColorControl[] smallColorControl;
928                         
929                         private SmallColorControl[] userSmallColorControl;
930                         
931                         private Label userColorLabel;
932                         private Label baseColorLabel;
933                         
934                         private SmallColorControl selectedSmallColorControl;
935                         
936                         private int currentlyUsedUserSmallColorControl = 0;
937                         private int[] customColors = null;
938                         
939                         private ColorDialog colorDialog = null;
940                         
941                         public BaseColorControl (ColorDialog colorDialog)
942                         {
943                                 this.colorDialog = colorDialog;
944                                 
945                                 userSmallColorControl = new SmallColorControl [16];
946                                 userSmallColorControl [0] = new SmallColorControl (Color.White);
947                                 userSmallColorControl [1] = new SmallColorControl (Color.White);
948                                 userSmallColorControl [2] = new SmallColorControl (Color.White);
949                                 userSmallColorControl [3] = new SmallColorControl (Color.White);
950                                 userSmallColorControl [4] = new SmallColorControl (Color.White);
951                                 userSmallColorControl [5] = new SmallColorControl (Color.White);
952                                 userSmallColorControl [6] = new SmallColorControl (Color.White);
953                                 userSmallColorControl [7] = new SmallColorControl (Color.White);
954                                 userSmallColorControl [8] = new SmallColorControl (Color.White);
955                                 userSmallColorControl [9] = new SmallColorControl (Color.White);
956                                 userSmallColorControl [10] = new SmallColorControl (Color.White);
957                                 userSmallColorControl [11] = new SmallColorControl (Color.White);
958                                 userSmallColorControl [12] = new SmallColorControl (Color.White);
959                                 userSmallColorControl [13] = new SmallColorControl (Color.White);
960                                 userSmallColorControl [14] = new SmallColorControl (Color.White);
961                                 userSmallColorControl [15] = new SmallColorControl (Color.White);
962                                 
963                                 smallColorControl = new SmallColorControl [48];
964                                 smallColorControl [0] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(128))));
965                                 smallColorControl [1] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(128)), ((Byte)(64))));
966                                 smallColorControl [2] = new SmallColorControl (Color.Gray);
967                                 smallColorControl [3] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(0)), ((Byte)(255))));
968                                 smallColorControl [4] = new SmallColorControl (Color.Silver);
969                                 smallColorControl [5] = new SmallColorControl (Color.FromArgb (((Byte)(64)), ((Byte)(128)), ((Byte)(128))));
970                                 smallColorControl [6] = new SmallColorControl (Color.White);
971                                 smallColorControl [7] = new SmallColorControl (Color.FromArgb (((Byte)(64)), ((Byte)(0)), ((Byte)(64))));
972                                 smallColorControl [8] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(64))));
973                                 smallColorControl [9] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(64)), ((Byte)(64))));
974                                 smallColorControl [10] = new SmallColorControl (Color.Teal);
975                                 smallColorControl [11] = new SmallColorControl (Color.Lime);
976                                 smallColorControl [12] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(128)), ((Byte)(255))));
977                                 smallColorControl [13] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(64)), ((Byte)(128))));
978                                 smallColorControl [14] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(0)), ((Byte)(128))));
979                                 smallColorControl [15] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(255)), ((Byte)(0))));
980                                 smallColorControl [16] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(255)), ((Byte)(64))));
981                                 smallColorControl [17] = new SmallColorControl (Color.Red);
982                                 smallColorControl [18] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(0))));
983                                 smallColorControl [19] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(255))));
984                                 smallColorControl [20] = new SmallColorControl (Color.Fuchsia);
985                                 smallColorControl [21] = new SmallColorControl (Color.Aqua);
986                                 smallColorControl [22] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(255)), ((Byte)(128))));
987                                 smallColorControl [23] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(255)), ((Byte)(255))));
988                                 smallColorControl [24] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(128)), ((Byte)(255))));
989                                 smallColorControl [25] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(64)), ((Byte)(0))));
990                                 smallColorControl [26] = new SmallColorControl (Color.FromArgb (((Byte)(64)), ((Byte)(0)), ((Byte)(0))));
991                                 smallColorControl [27] = new SmallColorControl (Color.Maroon);
992                                 smallColorControl [28] = new SmallColorControl (Color.Purple);
993                                 smallColorControl [29] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(0)), ((Byte)(160))));
994                                 smallColorControl [30] = new SmallColorControl (Color.Blue);
995                                 smallColorControl [31] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(128)), ((Byte)(64))));
996                                 smallColorControl [32] = new SmallColorControl (Color.Green);
997                                 smallColorControl [33] = new SmallColorControl (Color.Yellow);
998                                 smallColorControl [34] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(128)), ((Byte)(192))));
999                                 smallColorControl [35] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(128)), ((Byte)(192))));
1000                                 smallColorControl [36] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(0)), ((Byte)(64))));
1001                                 smallColorControl [37] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(192))));
1002                                 smallColorControl [38] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(255)), ((Byte)(128))));
1003                                 smallColorControl [39] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(255)), ((Byte)(128))));
1004                                 smallColorControl [40] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(64)), ((Byte)(0))));
1005                                 smallColorControl [41] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(64)), ((Byte)(64))));
1006                                 smallColorControl [42] = new SmallColorControl (Color.Navy);
1007                                 smallColorControl [43] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(0)), ((Byte)(64))));
1008                                 smallColorControl [44] = new SmallColorControl (Color.FromArgb (((Byte)(64)), ((Byte)(0)), ((Byte)(64))));
1009                                 smallColorControl [45] = new SmallColorControl (Color.FromArgb (((Byte)(64)), ((Byte)(0)), ((Byte)(128))));
1010                                 smallColorControl [46] = new SmallColorControl (Color.Black); //Black
1011                                 smallColorControl [47] = new SmallColorControl (Color.Olive);
1012                                 
1013                                 baseColorLabel = new Label ();
1014                                 userColorLabel = new Label ();
1015                                 
1016                                 SuspendLayout ();
1017                                 
1018                                 // colorPanel1
1019                                 smallColorControl [0].Location = new Point (0, 15);
1020                                 smallColorControl [0].TabIndex = 51;
1021                                 smallColorControl [0].Click += new EventHandler (OnSmallColorControlClick);
1022                                 // colorPanel2
1023                                 smallColorControl [1].Location = new Point (50, 130);
1024                                 smallColorControl [1].TabIndex = 92;
1025                                 smallColorControl [1].Click += new EventHandler (OnSmallColorControlClick);
1026                                 // colorPanel3
1027                                 smallColorControl [2].Location = new Point (75, 130);
1028                                 smallColorControl [2].TabIndex = 93;
1029                                 smallColorControl [2].Click += new EventHandler (OnSmallColorControlClick);
1030                                 // colorPanel4
1031                                 smallColorControl [3].Location = new Point (175, 84);
1032                                 smallColorControl [3].TabIndex = 98;
1033                                 smallColorControl [3].Click += new EventHandler (OnSmallColorControlClick);
1034                                 // colorPanel5
1035                                 smallColorControl [4].Location = new Point (125, 130);
1036                                 smallColorControl [4].TabIndex = 95;
1037                                 smallColorControl [4].Click += new EventHandler (OnSmallColorControlClick);
1038                                 // colorPanel6
1039                                 smallColorControl [5].Location = new Point (100, 130);
1040                                 smallColorControl [5].TabIndex = 94;
1041                                 smallColorControl [5].Click += new EventHandler (OnSmallColorControlClick);
1042                                 // colorPanel7
1043                                 smallColorControl [6].Location = new Point (175, 130);
1044                                 smallColorControl [6].TabIndex = 97;
1045                                 smallColorControl [6].Click += new EventHandler (OnSmallColorControlClick);
1046                                 // colorPanel8
1047                                 smallColorControl [7].Location = new Point (150, 130);
1048                                 smallColorControl [7].TabIndex = 96;
1049                                 smallColorControl [7].Click += new EventHandler (OnSmallColorControlClick);
1050                                 // colorPanel9
1051                                 smallColorControl [8].Location = new Point (25, 61);
1052                                 smallColorControl [8].TabIndex = 68;
1053                                 smallColorControl [8].Click += new EventHandler (OnSmallColorControlClick);
1054                                 // colorPanel10
1055                                 smallColorControl [9].Location = new Point (0, 61);
1056                                 smallColorControl [9].TabIndex = 67;
1057                                 smallColorControl [9].Click += new EventHandler (OnSmallColorControlClick);
1058                                 // colorPanel11
1059                                 smallColorControl [10].Location = new Point (75, 61);
1060                                 smallColorControl [10].TabIndex = 70;
1061                                 smallColorControl [10].Click += new EventHandler (OnSmallColorControlClick);
1062                                 // colorPanel12
1063                                 smallColorControl [11].Location = new Point (50, 61);
1064                                 smallColorControl [11].TabIndex = 69;
1065                                 smallColorControl [11].Click += new EventHandler (OnSmallColorControlClick);
1066                                 // colorPanel13
1067                                 smallColorControl [12].Location = new Point (125, 61);
1068                                 smallColorControl [12].TabIndex = 72;
1069                                 smallColorControl [12].Click += new EventHandler (OnSmallColorControlClick);
1070                                 // colorPanel14
1071                                 smallColorControl [13].Location = new Point (100, 61);
1072                                 smallColorControl [13].TabIndex = 71;
1073                                 smallColorControl [13].Click += new EventHandler (OnSmallColorControlClick);
1074                                 // colorPanel15
1075                                 smallColorControl [14].Location = new Point (175, 61);
1076                                 smallColorControl [14].TabIndex = 74;
1077                                 smallColorControl [14].Click += new EventHandler (OnSmallColorControlClick);
1078                                 // colorPanel16
1079                                 smallColorControl [15].Location = new Point (50, 38);
1080                                 smallColorControl [15].TabIndex = 61;
1081                                 smallColorControl [15].Click += new EventHandler (OnSmallColorControlClick);
1082                                 // colorPanel17
1083                                 smallColorControl [16].Location = new Point (75, 38);
1084                                 smallColorControl [16].TabIndex = 62;
1085                                 smallColorControl [16].Click += new EventHandler (OnSmallColorControlClick);
1086                                 // colorPanel18
1087                                 smallColorControl [17].Location = new Point (0, 38);
1088                                 smallColorControl [17].TabIndex = 59;
1089                                 smallColorControl [17].Click += new EventHandler (OnSmallColorControlClick);
1090                                 // colorPanel19
1091                                 smallColorControl [18].Location = new Point (25, 84);
1092                                 smallColorControl [18].TabIndex = 75;
1093                                 smallColorControl [18].Click += new EventHandler (OnSmallColorControlClick);
1094                                 // colorPanel20
1095                                 smallColorControl [19].Location = new Point (175, 15);
1096                                 smallColorControl [19].TabIndex = 58;
1097                                 smallColorControl [19].Click += new EventHandler (OnSmallColorControlClick);
1098                                 // colorPanel21
1099                                 smallColorControl [20].Location = new Point (175, 38);
1100                                 smallColorControl [20].TabIndex = 66;
1101                                 smallColorControl [20].Click += new EventHandler (OnSmallColorControlClick);
1102                                 // colorPanel22
1103                                 smallColorControl [21].Location = new Point (100, 38);
1104                                 smallColorControl [21].TabIndex = 63;
1105                                 smallColorControl [21].Click += new EventHandler (OnSmallColorControlClick);
1106                                 // colorPanel23
1107                                 smallColorControl [22].Location = new Point (50, 15);
1108                                 smallColorControl [22].TabIndex = 53;
1109                                 smallColorControl [22].Click += new EventHandler (OnSmallColorControlClick);
1110                                 // colorPanel24
1111                                 smallColorControl [23].Location = new Point (100, 15);
1112                                 smallColorControl [23].TabIndex = 55;
1113                                 smallColorControl [23].Click += new EventHandler (OnSmallColorControlClick);
1114                                 // colorPanel25
1115                                 smallColorControl [24].Location = new Point (125, 15);
1116                                 smallColorControl [24].TabIndex = 56;
1117                                 smallColorControl [24].Click += new EventHandler (OnSmallColorControlClick);
1118                                 // colorPanel26
1119                                 smallColorControl [25].Location = new Point (25, 107);
1120                                 smallColorControl [25].TabIndex = 83;
1121                                 smallColorControl [25].Click += new EventHandler (OnSmallColorControlClick);
1122                                 // colorPanel27
1123                                 smallColorControl [26].Location = new Point (0, 107);
1124                                 smallColorControl [26].TabIndex = 82;
1125                                 smallColorControl [26].Click += new EventHandler (OnSmallColorControlClick);
1126                                 // colorPanel28
1127                                 smallColorControl [27].Location = new Point (0, 84);
1128                                 smallColorControl [27].TabIndex = 81;
1129                                 smallColorControl [27].Click += new EventHandler (OnSmallColorControlClick);
1130                                 // colorPanel29
1131                                 smallColorControl [28].Location = new Point (150, 84);
1132                                 smallColorControl [28].TabIndex = 80;
1133                                 smallColorControl [28].Click += new EventHandler (OnSmallColorControlClick);
1134                                 // colorPanel30
1135                                 smallColorControl [29].Location = new Point (125, 84);
1136                                 smallColorControl [29].TabIndex = 79;
1137                                 smallColorControl [29].Click += new EventHandler (OnSmallColorControlClick);
1138                                 // colorPanel31
1139                                 smallColorControl [30].Location  = new Point (100, 84);
1140                                 smallColorControl [30].TabIndex = 78;
1141                                 smallColorControl [30].Click += new EventHandler (OnSmallColorControlClick);
1142                                 // colorPanel32
1143                                 smallColorControl [31].Location = new Point (75, 84);
1144                                 smallColorControl [31].TabIndex = 77;
1145                                 smallColorControl [31].Click += new EventHandler (OnSmallColorControlClick);
1146                                 // colorPanel33
1147                                 smallColorControl [32].Location = new Point (50, 84);
1148                                 smallColorControl [32].TabIndex = 76;
1149                                 smallColorControl [32].Click += new EventHandler (OnSmallColorControlClick);
1150                                 // colorPanel34
1151                                 smallColorControl [33].Location = new Point (25, 38);
1152                                 smallColorControl [33].TabIndex = 60;
1153                                 smallColorControl [33].Click += new EventHandler (OnSmallColorControlClick);
1154                                 // colorPanel35
1155                                 smallColorControl [34].Location = new Point (150, 38);
1156                                 smallColorControl [34].TabIndex = 65;
1157                                 smallColorControl [34].Click += new EventHandler (OnSmallColorControlClick);
1158                                 // colorPanel36
1159                                 smallColorControl [35].Location = new Point (125, 38);
1160                                 smallColorControl [35].TabIndex = 64;
1161                                 smallColorControl [35].Click += new EventHandler (OnSmallColorControlClick);
1162                                 // colorPanel37
1163                                 smallColorControl [36].Location = new Point (150, 61);
1164                                 smallColorControl [36].TabIndex = 73;
1165                                 smallColorControl [36].Click += new EventHandler (OnSmallColorControlClick);
1166                                 // colorPanel38
1167                                 smallColorControl [37].Location = new Point (150, 15);
1168                                 smallColorControl [37].TabIndex = 57;
1169                                 smallColorControl [37].Click += new EventHandler (OnSmallColorControlClick);
1170                                 // colorPanel39
1171                                 smallColorControl [38].Location = new Point (75, 15);
1172                                 smallColorControl [38].TabIndex = 54;
1173                                 smallColorControl [38].Click += new EventHandler (OnSmallColorControlClick);
1174                                 // colorPanel40
1175                                 smallColorControl [39].Location = new Point (25, 15);
1176                                 smallColorControl [39].TabIndex = 52;
1177                                 smallColorControl [39].Click += new EventHandler (OnSmallColorControlClick);
1178                                 // colorPanel41
1179                                 smallColorControl [40].Location = new Point (50, 107);
1180                                 smallColorControl [40].TabIndex = 84;
1181                                 smallColorControl [40].Click += new EventHandler (OnSmallColorControlClick);
1182                                 // colorPanel42
1183                                 smallColorControl [41].Location = new Point (75, 107);
1184                                 smallColorControl [41].TabIndex = 85;
1185                                 smallColorControl [41].Click += new EventHandler (OnSmallColorControlClick);
1186                                 // colorPanel43
1187                                 smallColorControl [42].Location = new Point (100, 107);
1188                                 smallColorControl [42].TabIndex = 86;
1189                                 smallColorControl [42].Click += new EventHandler (OnSmallColorControlClick);
1190                                 // colorPanel44
1191                                 smallColorControl [43].Location = new Point (125, 107);
1192                                 smallColorControl [43].TabIndex = 87;
1193                                 smallColorControl [43].Click += new EventHandler (OnSmallColorControlClick);
1194                                 // colorPanel45
1195                                 smallColorControl [44].Location = new Point (150, 107);
1196                                 smallColorControl [44].TabIndex = 88;
1197                                 smallColorControl [44].Click += new EventHandler (OnSmallColorControlClick);
1198                                 // colorPanel46
1199                                 smallColorControl [45].Location = new Point (175, 107);
1200                                 smallColorControl [45].TabIndex = 89;
1201                                 smallColorControl [45].Click += new EventHandler (OnSmallColorControlClick);
1202                                 // colorPanel47
1203                                 smallColorControl [46].Location = new Point (0, 130);
1204                                 smallColorControl [46].TabIndex = 90;
1205                                 smallColorControl [46].Click += new EventHandler (OnSmallColorControlClick);
1206                                 // colorPanel48
1207                                 smallColorControl [47].Location = new Point (25, 130);
1208                                 smallColorControl [47].TabIndex = 91;
1209                                 smallColorControl [47].Click += new EventHandler (OnSmallColorControlClick);
1210                                 
1211                                 // userColorPane1
1212                                 userSmallColorControl [0].Location = new Point (0, 180);
1213                                 userSmallColorControl [0].TabIndex = 99;
1214                                 userSmallColorControl [0].Click += new EventHandler (OnSmallColorControlClick);
1215                                 // userColorPanel2
1216                                 userSmallColorControl [1].Location = new Point (0, 203);
1217                                 userSmallColorControl [1].TabIndex = 108;
1218                                 userSmallColorControl [1].Click += new EventHandler (OnSmallColorControlClick);
1219                                 // userColorPanel13
1220                                 userSmallColorControl [2].Location = new Point (25, 180);
1221                                 userSmallColorControl [2].TabIndex = 100;
1222                                 userSmallColorControl [2].Click += new EventHandler (OnSmallColorControlClick);
1223                                 // userColorPanel4
1224                                 userSmallColorControl [3].Location = new Point (25, 203);
1225                                 userSmallColorControl [3].TabIndex = 109;
1226                                 userSmallColorControl [3].Click += new EventHandler (OnSmallColorControlClick);
1227                                 // userColorPanel5
1228                                 userSmallColorControl [4].Location = new Point (50, 180);
1229                                 userSmallColorControl [4].TabIndex = 101;
1230                                 userSmallColorControl [4].Click += new EventHandler (OnSmallColorControlClick);
1231                                 // userColorPanel6
1232                                 userSmallColorControl [5].Location = new Point (50, 203);
1233                                 userSmallColorControl [5].TabIndex = 110;
1234                                 userSmallColorControl [5].Click += new EventHandler (OnSmallColorControlClick);
1235                                 // userColorPanel7
1236                                 userSmallColorControl [6].Location = new Point (75, 180);
1237                                 userSmallColorControl [6].TabIndex = 102;
1238                                 userSmallColorControl [6].Click += new EventHandler (OnSmallColorControlClick);
1239                                 // userColorPanel8
1240                                 userSmallColorControl [7].Location = new Point (75, 203);
1241                                 userSmallColorControl [7].TabIndex = 111;
1242                                 userSmallColorControl [7].Click += new EventHandler (OnSmallColorControlClick);
1243                                 // userColorPanel9
1244                                 userSmallColorControl [8].Location = new Point (100, 180);
1245                                 userSmallColorControl [8].TabIndex = 103;
1246                                 userSmallColorControl [8].Click += new EventHandler (OnSmallColorControlClick);
1247                                 // userColorPanel10
1248                                 userSmallColorControl [9].Location = new Point (100, 203);
1249                                 userSmallColorControl [9].TabIndex = 112;
1250                                 userSmallColorControl [9].Click += new EventHandler (OnSmallColorControlClick);
1251                                 // userColorPanel11
1252                                 userSmallColorControl [10].Location = new Point (125, 180);
1253                                 userSmallColorControl [10].TabIndex = 105;
1254                                 userSmallColorControl [10].Click += new EventHandler (OnSmallColorControlClick);
1255                                 // userColorPanel2
1256                                 userSmallColorControl [11].Location = new Point (125, 203);
1257                                 userSmallColorControl [11].TabIndex = 113;
1258                                 userSmallColorControl [11].Click += new EventHandler (OnSmallColorControlClick);
1259                                 // userColorPanel13
1260                                 userSmallColorControl [12].Location = new Point (150, 180);
1261                                 userSmallColorControl [12].TabIndex = 106;
1262                                 userSmallColorControl [12].Click += new EventHandler (OnSmallColorControlClick);
1263                                 // userColorPanel4
1264                                 userSmallColorControl [13].Location = new Point (150, 203);
1265                                 userSmallColorControl [13].TabIndex = 114;
1266                                 userSmallColorControl [13].Click += new EventHandler (OnSmallColorControlClick);
1267                                 // userColorPanel15
1268                                 userSmallColorControl [14].Location = new Point (175, 180);
1269                                 userSmallColorControl [14].TabIndex = 107;
1270                                 userSmallColorControl [14].Click += new EventHandler (OnSmallColorControlClick);
1271                                 // userColorPanel16
1272                                 userSmallColorControl [15].Location = new Point (175, 203);
1273                                 userSmallColorControl [15].TabIndex = 115;
1274                                 userSmallColorControl [15].Click += new EventHandler (OnSmallColorControlClick);
1275                                 
1276                                 // baseColorLabel
1277                                 baseColorLabel.Location = new Point (2, 0);
1278                                 baseColorLabel.Size = new Size (200, 12);
1279                                 baseColorLabel.TabIndex = 5;
1280                                 baseColorLabel.Text = Locale.GetText ("Base Colors") + ":";
1281                                 // userColorLabel
1282                                 userColorLabel.FlatStyle = FlatStyle.System;
1283                                 userColorLabel.Location = new Point (2, 164);
1284                                 userColorLabel.Size = new Size (200, 14);
1285                                 userColorLabel.TabIndex = 104;
1286                                 userColorLabel.Text = Locale.GetText ("User Colors") + ":";
1287                                 
1288                                 Controls.Add (userSmallColorControl [7]);
1289                                 Controls.Add (userSmallColorControl [6]);
1290                                 Controls.Add (userSmallColorControl [5]);
1291                                 Controls.Add (userSmallColorControl [4]);
1292                                 Controls.Add (userSmallColorControl [3]);
1293                                 Controls.Add (userSmallColorControl [2]);
1294                                 Controls.Add (userSmallColorControl [1]);
1295                                 Controls.Add (userSmallColorControl [0]);
1296                                 Controls.Add (userSmallColorControl [15]);
1297                                 Controls.Add (userSmallColorControl [14]);
1298                                 Controls.Add (userSmallColorControl [13]);
1299                                 Controls.Add (userSmallColorControl [12]);
1300                                 Controls.Add (userSmallColorControl [11]);
1301                                 Controls.Add (userSmallColorControl [10]);
1302                                 Controls.Add (userSmallColorControl [9]);
1303                                 Controls.Add (userSmallColorControl [8]);
1304                                 
1305                                 Controls.Add (smallColorControl [0]);
1306                                 Controls.Add (smallColorControl [3]);
1307                                 Controls.Add (smallColorControl [6]);
1308                                 Controls.Add (smallColorControl [7]);
1309                                 Controls.Add (smallColorControl [4]);
1310                                 Controls.Add (smallColorControl [5]);
1311                                 Controls.Add (smallColorControl [2]);
1312                                 Controls.Add (smallColorControl [1]);
1313                                 Controls.Add (smallColorControl [47]);
1314                                 Controls.Add (smallColorControl [46]);
1315                                 Controls.Add (smallColorControl [45]);
1316                                 Controls.Add (smallColorControl [44]);
1317                                 Controls.Add (smallColorControl [43]);
1318                                 Controls.Add (smallColorControl [42]);
1319                                 Controls.Add (smallColorControl [41]);
1320                                 Controls.Add (smallColorControl [40]);
1321                                 Controls.Add (smallColorControl [25]);
1322                                 Controls.Add (smallColorControl [26]);
1323                                 Controls.Add (smallColorControl [27]);
1324                                 Controls.Add (smallColorControl [28]);
1325                                 Controls.Add (smallColorControl [29]);
1326                                 Controls.Add (smallColorControl [30]);
1327                                 Controls.Add (smallColorControl [31]);
1328                                 Controls.Add (smallColorControl [32]);
1329                                 Controls.Add (smallColorControl [18]);
1330                                 Controls.Add (smallColorControl [14]);
1331                                 Controls.Add (smallColorControl [36]);
1332                                 Controls.Add (smallColorControl [12]);
1333                                 Controls.Add (smallColorControl [13]);
1334                                 Controls.Add (smallColorControl [10]);
1335                                 Controls.Add (smallColorControl [11]);
1336                                 Controls.Add (smallColorControl [8]);
1337                                 Controls.Add (smallColorControl [9]);
1338                                 Controls.Add (smallColorControl [20]);
1339                                 Controls.Add (smallColorControl [34]);
1340                                 Controls.Add (smallColorControl [35]);
1341                                 Controls.Add (smallColorControl [21]);
1342                                 Controls.Add (smallColorControl [16]);
1343                                 Controls.Add (smallColorControl [15]);
1344                                 Controls.Add (smallColorControl [33]);
1345                                 Controls.Add (smallColorControl [17]);
1346                                 Controls.Add (smallColorControl [19]);
1347                                 Controls.Add (smallColorControl [37]);
1348                                 Controls.Add (smallColorControl [24]);
1349                                 Controls.Add (smallColorControl [23]);
1350                                 Controls.Add (smallColorControl [38]);
1351                                 Controls.Add (smallColorControl [22]);
1352                                 Controls.Add (smallColorControl [39]);
1353                                 
1354                                 Controls.Add (userColorLabel);
1355                                 Controls.Add (baseColorLabel);
1356                                 
1357                                 Size = new Size (212, 238);
1358                                 
1359                                 ResumeLayout (false);
1360                         }
1361                         
1362                         private void CheckIfColorIsInPanel (Color color)
1363                         {
1364                                 // check if we have a panel with a BackColor = color...
1365                                 for (int i = 0; i < smallColorControl.Length; i++) {
1366                                         if (smallColorControl [i].InternalColor == color) {
1367                                                 selectedSmallColorControl = smallColorControl [i];
1368                                                 selectedSmallColorControl.IsSelected = true;
1369                                                 break;
1370                                         }
1371                                 }
1372                         }
1373                         
1374                         void OnSmallColorControlClick (object sender, EventArgs e)
1375                         {
1376                                 // previous selected smallcolorcontrol
1377                                 if (selectedSmallColorControl != (SmallColorControl)sender)
1378                                         selectedSmallColorControl.IsSelected = false;
1379                                 
1380                                 selectedSmallColorControl = (SmallColorControl)sender;
1381                                 
1382                                 TriangleControl.CurrentBrightness = HSB.Brightness (selectedSmallColorControl.InternalColor);
1383                                 
1384                                 colorDialog.UpdateControls (selectedSmallColorControl.InternalColor);
1385                                 colorDialog.UpdateRGBTextBoxes (selectedSmallColorControl.InternalColor);
1386                                 colorDialog.UpdateHSBTextBoxes (selectedSmallColorControl.InternalColor);
1387                         }
1388                         
1389                         public Color ColorToShow {
1390                                 get {
1391                                         return selectedSmallColorControl.InternalColor;
1392                                 }
1393                         }
1394                         
1395                         
1396                         public void SetColor (Color acolor)
1397                         {
1398                                 if (selectedSmallColorControl != null)
1399                                         selectedSmallColorControl.IsSelected = false;
1400                                 
1401                                 CheckIfColorIsInPanel (acolor);
1402                                 
1403                                 TriangleControl.CurrentBrightness = HSB.Brightness (acolor);
1404                                 
1405                                 colorDialog.UpdateControls (acolor);
1406                                 colorDialog.UpdateRGBTextBoxes (acolor);
1407                                 colorDialog.UpdateHSBTextBoxes (acolor);
1408                         }
1409                         
1410                         public void SetUserColor (Color col)
1411                         {
1412                                 userSmallColorControl [currentlyUsedUserSmallColorControl].InternalColor = col;
1413                                 
1414                                 // check if this.customColors already exists
1415                                 if (customColors == null) {
1416                                         customColors = new int [16];
1417                                         int white = Color.White.ToArgb ();
1418                                         
1419                                         for (int i = 0; i < customColors.Length; i++)
1420                                                 customColors [i] = white;
1421                                 }
1422                                 
1423                                 customColors [currentlyUsedUserSmallColorControl] = col.ToArgb ();
1424                                 
1425                                 // update ColorDialog dialog property
1426                                 colorDialog.CustomColors = customColors;
1427                                 
1428                                 currentlyUsedUserSmallColorControl++;
1429                                 if (currentlyUsedUserSmallColorControl > 15)
1430                                         currentlyUsedUserSmallColorControl = 0;
1431                         }
1432                         
1433                         public void SetCustomColors ()
1434                         {
1435                                 int[] customColors = colorDialog.CustomColors;
1436                                 
1437                                 if (customColors != null) {
1438                                         for (int i = 0; i < customColors.Length; i++) {
1439                                                 userSmallColorControl [i].InternalColor = Color.FromArgb (customColors [i]);
1440                                         }
1441                                 } else {
1442                                         for (int i = 0; i < userSmallColorControl.Length; i++) {
1443                                                 userSmallColorControl [i].InternalColor = Color.White;
1444                                         }
1445                                 }
1446                         }
1447                 }
1448                 
1449                 internal class ColorMatrixControl : Panel {
1450                         internal class DrawingBitmap {
1451                                 private Bitmap bitmap;
1452                                 
1453                                 public DrawingBitmap (Size size)
1454                                 {
1455                                         bitmap = new Bitmap (size.Width, size.Height);
1456                                         
1457                                         float hueadd = 240.0f / (size.Width - 1);
1458                                         float satsub = 241.0f / (size.Height - 1);
1459                                         float satpos = 240.0f;
1460                                         
1461                                         // paint the matrix to the bitmap
1462                                         for (int height = 0; height < size.Height; height++) {
1463                                                 float huepos = 0.0f;
1464                                                 
1465                                                 for (int width = 0; width < size.Width; width++) {
1466                                                         HSB hsb = new HSB ();
1467                                                         
1468                                                         hsb.hue = (int)huepos;
1469                                                         hsb.sat = (int)satpos;
1470                                                         hsb.bri = 120; // paint it with 120 to get a nice bitmap
1471                                                         
1472                                                         bitmap.SetPixel (width, height, HSB.HSB2RGB (hsb.hue, hsb.sat, hsb.bri));
1473                                                         
1474                                                         huepos += hueadd;
1475                                                 }
1476                                                 
1477                                                 satpos -= satsub;
1478                                         }
1479                                 }
1480                                 
1481                                 public Bitmap Bitmap {
1482                                         set {
1483                                                 bitmap = value;
1484                                         }
1485                                         
1486                                         get {
1487                                                 return bitmap;
1488                                         }
1489                                 }
1490                         }
1491                         
1492                         internal class CrossCursor {
1493                                 private Bitmap bitmap;
1494                                 
1495                                 private Color cursorColor;
1496                                 
1497                                 public CrossCursor ()
1498                                 {
1499                                         bitmap = new Bitmap (22, 22);
1500                                         
1501                                         cursorColor = Color.Black;
1502                                         
1503                                         Draw ();
1504                                 }
1505                                 
1506                                 public void Draw ()
1507                                 {
1508                                         using (Pen pen = new Pen (ThemeEngine.Current.ResPool.GetSolidBrush (cursorColor), 3)) {
1509                                                 using (Graphics graphics = Graphics.FromImage (bitmap)) {
1510                                                         graphics.DrawLine (pen, 11, 0, 11, 7);
1511                                                         graphics.DrawLine (pen, 11, 14, 11, 21);
1512                                                         graphics.DrawLine (pen, 0, 11, 7, 11);
1513                                                         graphics.DrawLine (pen, 14, 11, 21, 11);
1514                                                 }
1515                                         }
1516                                 }
1517                                 
1518                                 public Bitmap Bitmap {
1519                                         set {
1520                                                 bitmap = value;
1521                                         }
1522                                         
1523                                         get {
1524                                                 return bitmap;
1525                                         }
1526                                 }
1527                                 
1528                                 public Color CursorColor {
1529                                         set {
1530                                                 cursorColor = value;
1531                                         }
1532                                         
1533                                         get {
1534                                                 return cursorColor;
1535                                         }
1536                                 }
1537                         }
1538                         
1539                         private DrawingBitmap drawingBitmap = null;
1540                         
1541                         private CrossCursor crossCursor = new CrossCursor();
1542                         
1543                         private bool mouseButtonDown = false;
1544                         
1545                         private bool drawCross = true;
1546                         
1547                         private Color color;
1548                         
1549                         private int currentXPos;
1550                         private int currentYPos;
1551                         
1552                         private float xstep;
1553                         private float ystep;
1554                         
1555                         private ColorDialog colorDialog;
1556                         
1557                         public ColorMatrixControl (ColorDialog colorDialog)
1558                         {
1559                                 this.colorDialog = colorDialog;
1560                                 
1561                                 SuspendLayout ();
1562                                 
1563                                 BorderStyle = BorderStyle.Fixed3D;
1564                                 Location = new Point (0, 0);
1565                                 Size = new Size (179, 190);
1566                                 TabIndex = 0;
1567                                 TabStop = false;
1568                                 
1569                                 ResumeLayout (false);
1570                                 
1571                                 xstep = 240.0f / (ClientSize.Width - 1);
1572                                 ystep = 241.0f / (ClientSize.Height - 1);
1573                                 
1574                                 SetStyle (ControlStyles.DoubleBuffer, true);
1575                                 SetStyle (ControlStyles.AllPaintingInWmPaint, true);
1576                                 SetStyle (ControlStyles.UserPaint, true);
1577                         }
1578                         
1579                         protected override void OnPaint (PaintEventArgs e)
1580                         {
1581                                 if (drawingBitmap == null)
1582                                         drawingBitmap = new DrawingBitmap (ClientSize);
1583                                 
1584                                 Draw (e);
1585                                 
1586                                 base.OnPaint (e);
1587                         }
1588                         
1589                         private void Draw (PaintEventArgs e)
1590                         {
1591                                 e.Graphics.DrawImage (drawingBitmap.Bitmap, ClientRectangle.X, ClientRectangle.Y);
1592                                 
1593                                 // drawCross is false if the mouse gets moved...
1594                                 if (drawCross) {
1595                                         e.Graphics.DrawImage (crossCursor.Bitmap, currentXPos - 11 , currentYPos - 11);
1596                                 }
1597                         }
1598                         
1599                         protected override void OnMouseDown (MouseEventArgs e)
1600                         {
1601                                 mouseButtonDown = true;
1602                                 currentXPos = e.X;
1603                                 currentYPos = e.Y;
1604                                 if (drawCross) {
1605                                         drawCross = false;
1606                                         Invalidate ();
1607                                         Update ();
1608                                 }
1609                                 
1610                                 UpdateControls ();
1611                                 XplatUI.GrabWindow (Handle, Handle);
1612                                 
1613                                 base.OnMouseDown (e);
1614                         }
1615                         
1616                         protected override void OnMouseMove (MouseEventArgs e)
1617                         {
1618                                 if (mouseButtonDown)
1619                                         if ((e.X < ClientSize.Width && e.X >= 0) && (e.Y < ClientSize.Height && e.Y >= 0)) {
1620                                                 currentXPos = e.X;
1621                                                 currentYPos = e.Y;
1622                                                 UpdateControls ();
1623                                         }
1624                                 
1625                                 base.OnMouseMove (e);
1626                         }
1627                         
1628                         protected override void OnMouseUp (MouseEventArgs e)
1629                         {
1630                                 XplatUI.UngrabWindow (Handle);
1631                                 mouseButtonDown = false;
1632                                 drawCross = true;
1633                                 Invalidate ();
1634                                 Update ();
1635                         }
1636                         
1637                         public Color ColorToShow {
1638                                 set {
1639                                         ComputePos (value);
1640                                 }
1641                         }
1642                         
1643                         private void ComputePos (Color acolor)
1644                         {
1645                                 if (acolor != color) {
1646                                         color = acolor;
1647                                         
1648                                         HSB hsb = HSB.RGB2HSB (color);
1649                                         
1650                                         currentXPos = (int)((float)hsb.hue / xstep);
1651                                         currentYPos = ClientSize.Height - 1 - (int)((float)hsb.sat / ystep);
1652                                         
1653                                         if (currentXPos < 0)
1654                                                 currentXPos = 0;
1655                                         if (currentYPos < 0)
1656                                                 currentYPos = 0;
1657                                         
1658                                         Invalidate ();
1659                                         Update ();
1660                                 }
1661                         }
1662                         
1663                         private Color GetColorFromHSB ()
1664                         {
1665                                 int hue = (int)((float)currentXPos * xstep);
1666                                 int sat = 240 - ((int)((float)currentYPos * ystep));
1667                                 int bri = TriangleControl.CurrentBrightness;
1668                                 
1669                                 return HSB.HSB2RGB (hue, sat, bri);
1670                         }
1671                         
1672                         private void UpdateControls ()
1673                         {
1674                                 Color tmpColor = GetColorFromHSB ();
1675                                 
1676                                 // update the brightness control
1677                                 colorDialog.brightnessControl.ShowColor ((int)((float)currentXPos * xstep), 240 - ((int)((float)currentYPos * ystep)));
1678                                 
1679                                 // update saturation text box
1680                                 int satvalue = (240 - ((int)((float)currentYPos * ystep)));
1681                                 colorDialog.satTextBox.Text = satvalue.ToString ();
1682                                 
1683                                 // update hue text box
1684                                 int huevalue = (int)((float)currentXPos * xstep);
1685                                 if (huevalue > 239)
1686                                         huevalue = 239;
1687                                 colorDialog.hueTextBox.Text = huevalue.ToString ();
1688                                 
1689                                 // update the main selected color panel
1690                                 colorDialog.selectedColorPanel.BackColor = tmpColor;
1691                                 
1692                                 // and finally the rgb text boxes
1693                                 colorDialog.UpdateRGBTextBoxes (tmpColor);
1694                         }
1695                 }
1696                 
1697                 
1698                 internal class BrightnessControl : Panel {
1699                         internal class DrawingBitmap {
1700                                 private Bitmap bitmap;
1701                                 
1702                                 public DrawingBitmap ()
1703                                 {
1704                                         bitmap = new Bitmap (14, 190);
1705                                 }
1706                                 
1707                                 public Bitmap Bitmap {
1708                                         set {
1709                                                 bitmap = value;
1710                                         }
1711                                         
1712                                         get {
1713                                                 return bitmap;
1714                                         }
1715                                 }
1716                                 
1717                                 // only hue and saturation are needed.
1718                                 // color will be computed with an iteration
1719                                 public void Draw (int hue, int sat)
1720                                 {
1721                                         float brisub = 241.0f / 190.0f;
1722                                         float bri = 241.0f;
1723                                         
1724                                         for (int height = 0; height < 190; height++) {
1725                                                 for (int width = 0; width < 14; width++) {
1726                                                         Color pixcolor = HSB.HSB2RGB (hue, sat, (int)bri);
1727                                                         bitmap.SetPixel (width, height, pixcolor);
1728                                                 }
1729                                                 bri = bri - brisub;
1730                                         }
1731                                 }
1732                         }
1733                         
1734                         private const float step = 241.0f/189.0f;
1735                         
1736                         private DrawingBitmap bitmap;
1737                         
1738                         private ColorDialog colorDialog = null;
1739                         
1740                         public BrightnessControl (ColorDialog colorDialog)
1741                         {
1742                                 this.colorDialog = colorDialog;
1743                                 
1744                                 bitmap = new DrawingBitmap ();
1745                                 
1746                                 SuspendLayout ();
1747                                 
1748                                 BorderStyle = BorderStyle.Fixed3D;
1749                                 Location = new Point (0, 0);
1750                                 Size = new Size (14, 190);
1751                                 TabIndex = 0;
1752                                 TabStop = false;
1753                                 Size = new Size (14, 190);
1754                                 
1755                                 ResumeLayout (false);
1756                                 
1757                                 SetStyle (ControlStyles.DoubleBuffer, true);
1758                                 SetStyle (ControlStyles.AllPaintingInWmPaint, true);
1759                                 SetStyle (ControlStyles.UserPaint, true);
1760                         }
1761                         
1762                         
1763                         protected override void OnPaint (PaintEventArgs e)
1764                         {
1765                                 e.Graphics.DrawImage (bitmap.Bitmap, 0, 0);
1766                                 
1767                                 base.OnPaint (e);
1768                         }
1769                         
1770                         protected override void OnMouseDown (MouseEventArgs e)
1771                         {
1772                                 colorDialog.triangleControl.TrianglePosition = (int)((float)(189 - e.Y) * step);
1773                                 
1774                                 base.OnMouseDown (e);
1775                         }
1776                         
1777                         // this one is for ColorMatrixControl
1778                         public void ShowColor (int hue, int sat)
1779                         {
1780                                 bitmap.Draw (hue, sat);
1781                                 Invalidate ();
1782                                 Update ();
1783                         }
1784                         
1785                         // this one for the other controls
1786                         public Color ColorToShow {
1787                                 set {
1788                                         int hue, sat;
1789                                         HSB.GetHueSaturation (value, out hue, out sat);
1790                                         bitmap.Draw (hue, sat);
1791                                         Invalidate ();
1792                                         Update ();
1793                                 }
1794                         }
1795                 }
1796                 
1797                 
1798                 internal class TriangleControl : Panel {
1799                         private bool mouseButtonDown = false;
1800                         
1801                         private int currentTrianglePosition = 195;
1802 //                      private Rectangle clipRectangle;
1803                         
1804                         private const float briStep = 241.0f/186.0f;
1805                         
1806                         private static int currentBrightness = 0;
1807                         
1808                         private ColorDialog colorDialog;
1809                         
1810                         public TriangleControl (ColorDialog colorDialog)
1811                         {
1812                                 this.colorDialog = colorDialog;
1813                                 
1814                                 SuspendLayout ();
1815                                 
1816                                 Size = new Size (16, 203);
1817                                 
1818                                 ResumeLayout (false);
1819                                 
1820                                 SetStyle (ControlStyles.DoubleBuffer, true);
1821                                 SetStyle (ControlStyles.AllPaintingInWmPaint, true);
1822                                 SetStyle (ControlStyles.UserPaint, true);
1823                         }
1824                         
1825                         public static int CurrentBrightness {
1826                                 set {
1827                                         currentBrightness = value;
1828                                 }
1829                                 
1830                                 get {
1831                                         return currentBrightness;
1832                                 }
1833                         }
1834                         
1835                         protected override void OnPaint (PaintEventArgs e)
1836                         {
1837                                 Draw (e);
1838                                 
1839                                 base.OnPaint (e);
1840                         }
1841                         
1842                         private void Draw (PaintEventArgs e)
1843                         {
1844                                 e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), new Rectangle (0, 0, 16, 203));
1845                                 
1846                                 Point[] trianglePoints = new Point [3]
1847                                 {
1848                                         new Point (0, currentTrianglePosition),
1849                                         new Point (8, currentTrianglePosition - 8),
1850                                         new Point (8, currentTrianglePosition + 8)
1851                                 };
1852                                 
1853                                 e.Graphics.FillPolygon (ThemeEngine.Current.ResPool.GetSolidBrush (Color.Black), trianglePoints);
1854                         }
1855                         
1856                         protected override void OnMouseDown (MouseEventArgs e)
1857                         {
1858                                 if (e.Y > 195 || e.Y < 9) return; // helper until Cursor.Clip works
1859                                 
1860                                 mouseButtonDown = true;
1861                                 currentTrianglePosition = e.Y;
1862                                 
1863                                 // Cursor.Clip doesn't yet work in Managed.Windows.Forms
1864 //                              clipRectangle = Cursor.Clip;
1865 //                              Point p = Location;
1866 //                              p.Y += 8;
1867 //                              Size s = Size;
1868 //                              s.Width -= 5;
1869 //                              s.Height -= 16;
1870 //                              Cursor.Clip = new Rectangle( Parent.PointToScreen( p ), s );
1871                                 
1872                                 colorDialog.briTextBox.Text = TrianglePosition.ToString ();
1873                                 colorDialog.UpdateFromHSBTextBoxes ();
1874                                 
1875                                 Invalidate ();
1876                                 Update ();
1877                                 
1878                                 base.OnMouseDown (e);
1879                         }
1880                         
1881                         protected override void OnMouseMove (MouseEventArgs e)
1882                         {
1883                                 if (mouseButtonDown)
1884                                         if (e.Y < 196 && e.Y > 8) {
1885                                                 currentTrianglePosition = e.Y;
1886                                                 
1887                                                 colorDialog.briTextBox.Text = TrianglePosition.ToString ();
1888                                                 colorDialog.UpdateFromHSBTextBoxes ();
1889                                                 
1890                                                 Invalidate ();
1891                                                 Update ();
1892                                         }
1893                                 
1894                                 base.OnMouseMove (e);
1895                         }
1896                         
1897                         protected override void OnMouseUp (MouseEventArgs e)
1898                         {
1899                                 mouseButtonDown = false;
1900 //                              Cursor.Clip = clipRectangle;
1901                                 
1902                                 base.OnMouseUp (e);
1903                         }
1904                         
1905                         public int TrianglePosition {
1906                                 get {
1907                                         float tmp = (float)(currentTrianglePosition - 9);
1908                                         tmp = tmp * briStep;
1909                                         
1910                                         int retval = 240 - (int)tmp;
1911                                         
1912                                         TriangleControl.CurrentBrightness = retval;
1913                                         
1914                                         return retval;
1915                                 }
1916                                 
1917                                 set {
1918                                         float tmp = (float)value / briStep;
1919                                         currentTrianglePosition = 186 - (int)tmp + 9;
1920                                         
1921                                         colorDialog.briTextBox.Text = TrianglePosition.ToString ();
1922                                         
1923                                         colorDialog.UpdateFromHSBTextBoxes ();
1924                                         
1925                                         Invalidate ();
1926                                         Update ();
1927                                 }
1928                         }
1929                         
1930                         public Color ColorToShow {
1931                                 set {
1932                                         SetColor (value);
1933                                 }
1934                         }
1935                         
1936                         public void SetColor (Color color)
1937                         {
1938                                 int pos_raw = HSB.Brightness (color);
1939                                 
1940                                 float tmp = (float)pos_raw / briStep;
1941                                 currentTrianglePosition = 186 - (int)tmp + 9;
1942                                 
1943                                 if (colorDialog.edit_textbox == null)
1944                                         colorDialog.briTextBox.Text = TrianglePosition.ToString ();
1945                                 
1946                                 Invalidate ();
1947                         }
1948                 }
1949                 #endregion
1950         }
1951 }
1952