There is a very handy .NET class called ControlPaint in the System.Windows.Forms namespace that enables you to draw your own controls and control elements using the standard Windows style and theme. Buried in this rich class are four methods that enable you to lighten and darken colors:
- Dark – Creates a darker color from the specified color.
- DarkDark – Creates a much darker color.
- Light – Creates a lighter color.
- LightLight – Creates a much lighter color.
All four static methods accept a Color structure argument and return the new Color. The Dark and Light methods are overloaded and can also accept a floating point percentage of their respective DarkDark or LightLight colors, where 1.0 equals 100%:
public static Color Dark( Color baseColor )
public static Color Dark( Color baseColor, float pctOfDarkDark )
public static Color DarkDark( Color baseColor )public static Color Light( Color baseColor )
public static Color Light( Color baseColor, float pctOfLightLight )
public static Color LightLight( Color baseColor )
This image demonstrates these methods on the Color.Red:
There are some interesting aspects to these methods:
- Dark( color, 0.5F ) == Dark( color )
- Dark( color, 1.0F ) == DarkDark( color )
- Dark( color, 1.0F ) == Color.Black
- Light( color, 0.5F ) == Light( color )
- Light( color, 1.0F ) == LightLight( color )
- Light( color, 2.0F ) == Color.White
- If the specified Color is one of the SystemColors, the color is converted to the SystemColors.ControlDark color (or ControlDarkDark, ControlLight or ControlLightLight system color as appropriate). Otherwise, the color’s luminosity value is increased or decreased by the percentage specified.
Thanks, was just what i was looking for!
Then:
Dark( color, 1.0F ) == DarkDark( color )
Dark( color, 1.0F ) == Color.Black
So:
DarkDark( color ) == Color.Black
DarkDark() always returns black?
Re: DarkDark() always returns black?
Yes, it appears so, but I do not have conclusive proof.
what about an dark color method?
I am trying this one: but it gives me an exception when of course it tries to make 10 – 80 (for example) it gives negative value so it is not ok:
public Color GetDarkerColor(Color color)
{
const int max = 255;
int increase = 80;
int r = Math.Min(color.R – increase, max);
int g = Math.Min(color.G – increase, max);
int b = Math.Min(color.B – increase, max);
return Color.FromArgb(r, g, b);
}
Thanks for sharing!
I was a long time looking for a possibility to in- or decrease a calor value.
It’s better not to rely on the ControlPaint class, as it is not very clear how it works. For a well written method that lightens and darkens colors in C# take a look at the following article:
Make Color Lighter or Darker in C#
Hi Pavel,
No that technique is not lightening or darkening the color unless three RBG values are equal. That technique will change the colour. If you don’t believe me, open up MS Paint and experiment with the light/dark slider. The changes to RBG are not linear.
ControlPaint is the way to go – you just need to take the time to read and understand the doco.