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