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