2

I would think that using RGB to set the custom colors would be the default method, but that does not seem to be the case. This stackoverflow answer was very helpful in using named colors (such as red or blue) or hexadecimal values to set custom colors, but for the project I'm working on, it would be best to set the colors using RGB values. Alternatively, there may be times when HSL values could be helpful.

I'd appreciate help showing how to set the colorDialog custom colors using RGB (and maybe HSL as well).

Thank you

Community
  • 1
  • 1
AARRGGHHH
  • 95
  • 4
  • 9

1 Answers1

2

If you use the same method as the question you linked to does, you should be able to use the static Color.FromArgb method. I've never used the ColorDialog control, so this is mostly just a guess.

int r = 25; // or whatever
int g = 34;
int b = 43;

colorDialog1.CustomColors = new int[] { 
                                        ColorTranslator.ToOle(Color.FromArgb(r, g, b))
                                      };
colorDialog1.ShowDialog(); 

For HSL, you can reference this other answer. Basically, as far as I know, you have to do the conversion yourself.

Community
  • 1
  • 1
Matthew Haugen
  • 11,855
  • 5
  • 34
  • 52
  • It seems like it should be that easy. Unfortunately, using Color.FromArgb (or ColorTranslator.FromArgb) does not compile. – AARRGGHHH Feb 27 '15 at 07:41
  • Now *that* sounds fixable. What's the compiler error? – Matthew Haugen Feb 27 '15 at 07:44
  • Multiple Errors: 1. Color(System.Drawing.Color)' is a 'method', which is not valid in the given context 2. The name 'b' does not exist in the current context 3. The name 'g' does not exist in the current context 4. The name 'r' does not exist in the current context When switching from Color to ColorTranslator, the 1st error is replaced with: 1. 'System.Drawing.ColorTranslator' does not contain a definition for 'FromArgb' The other 3 errors still occur. I don't think is a fixable compile issue. However, there has to be a way to do this. – AARRGGHHH Feb 27 '15 at 18:21
  • @AARRGGHHH It sounds like you have a method with the name of `Color`? Try fully qualifying that name, with `System.Drawing.Color`. As for the others, that's pretty clear-cut--you need to change the parameters you're passing to the method, or define the ones I used somewhere. – Matthew Haugen Feb 27 '15 at 18:33
  • I don't think defining the r g b parameters will change anything, since they're arguments to a method that doesn't exist. Unfortunately, Color and ColorTranslator have no FromArgb (or FromRgb) method. – AARRGGHHH Feb 27 '15 at 19:20
  • Actually, I have a correction on that. Color. wasn't coming up with anything (using System.Drawing IS included). However, when i used System.Drawing.Color, I found a FromArgb method. What would cause that to happen: the method to fail to show up in intellisense, and to trigger an error in the compiler? In any case, it looks like I have a solution here, using for example "ColorTranslator.ToOle(System.Drawing.Color.FromArgb(128, 0, 128))," Thank you very much! There is no FromHsv (or FromHsl) method, any suggestions on that issue? – AARRGGHHH Feb 27 '15 at 19:30
  • @AARRGGHHH My suspicion is that you have some local method with the name `Color` that was tripping up the compiler. And as for HSV or HSL, that's what the last line is for--you have to do the conversion yourself, as far as I know. – Matthew Haugen Feb 27 '15 at 19:36