Setting CustomColors in a ColorDialog
Asked Answered
C

3

17

Custom color set in the color dialog are supposed to be set to {Blue, Blue} using the following code:

colorDialog1.CustomColors = new int[] { System.Drawing.Color.Blue.ToArgb(), 0xFF0000 };
colorDialog1.ShowDialog();

But, I am getting a different set {Black, Blue}:

enter image description here

Any idea What I am doing wrong here? Thanks.

Chambless answered 18/7, 2012 at 17:38 Comment(4)
"0xFF0000" isn't it red?! might be wrong though..Simpatico
@Arash, nope, it's blue. 0x0000FF is red.Bach
@FrédéricHamidi: nthelp.com/colorcodes.htm (BRG)Simpatico
@Arash, the COLORREF values used by the dialog actually use BGR instead of RGB, thus the difference.Bach
Q
27

You need to use OLE colors. The simplist way to achieve this is using the built in ColorTranslator object, e.g.

colorDialog1.CustomColors = new int[] { 
                                        ColorTranslator.ToOle(Color.Blue), 
                                        ColorTranslator.ToOle(Color.Red)
                                      };
colorDialog1.ShowDialog(); 

If you need to convert from HTML colors, you can also use the ColorTranslator.FromHtml method, e.g.

colorDialog1.CustomColors = new int[]
                                {
                                    ColorTranslator.ToOle(Color.Blue), 
                                    ColorTranslator.ToOle(ColorTranslator.FromHtml("#FF0000"))
                                };
Quarrel answered 18/7, 2012 at 17:51 Comment(1)
Interesting that this worked for you Afshin. For me, ColorTranslator.ToOle() did not work - I had to use ColorTranslator.ToWin32(). In any case, this post pointed me in the right direction to solve things...so thx to both you and George.Madera
E
1

If you have an array of colors, you can translate them using Linq:

colorDialog1.CustomColors = ThemeColors.Select(x => ColorTranslator.ToOle(x)).ToArray()

The ThemeColors array would be something like this:

public static Color[] ThemeColors
{
   get => new[]
   {
      Color.FromArgb(255, 185, 0),
      Color.FromArgb(231, 72, 86),
      Color.FromArgb(0, 120, 215),
      Color.FromArgb(0, 153, 188),
      Color.DarkOrange
   }
}

Note: Don't forget to add:

using System.Linq;
Excommunicatory answered 30/11, 2019 at 20:25 Comment(0)
P
0

If you use ColorTranslator FromArgb, you can keep RGB colors in their proper order. For example, ColorTranslator.ToOle(Color.FromArgb(255, 0, 0)), is red. You can also use this to set colors that don't have a name, for example, the bright red ColorTranslator.ToOle(Color.FromArgb(255, 31, 33)),.

Pleomorphism answered 9/3, 2015 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.