Darken or lighten the colour with C#



It was a while ago to play the colours in .NETCF. The case was drawing darker or lighter colour when user clicks my custom design buttons. Well, I believe it would be much easier to change the colour by its original colour. So I came up with two methods to extend the existing System.Drawing.Color object. These two methods can be found similar over the Internet. So, I have to thank all the developers in the Internet...


public static Color GetColorDarker(this Color color, double factor)
{
    // The factor value value cannot be greater than 1 or smaller than 0.
    // Otherwise return the original colour
    if (factor < 0 || factor > 1)
        return color;

    int r = (int)(factor * color.R);
    int g = (int)(factor * color.G);
    int b = (int)(factor * color.B);
    return Color.FromArgb(r, g, b);
}
public static Color GetColorLighter(this Color color, double factor)
{
    // The factor value value cannot be greater than 1 or smaller than 0.
    // Otherwise return the original colour
    if (factor < 0 || factor > 1)
        return color;

    int r = (int)(factor * color.R + (1 - factor) * 255);
    int g = (int)(factor * color.G + (1 - factor) * 255);
    int b = (int)(factor * color.B + (1 - factor) * 255);
    return Color.FromArgb(r, g, b);
}


Related Posts

In case you missed it

A short update from me
A short update from me
It has been a while since I post a blog page on Blogger which was created back in 2013. So I took an opportunity to...
Showcase the blog site elements
Showcase the blog site elements
The standard paragraphs Welcome to this demo page! Here, you’ll get an exclusive preview of our cutting-edge platform designed to revolutionise your digital experience. Our...
Issue with
Issue with "Don't track your own pageviews"
Do you use your own domain name with Blogger? Do you place “Blogger’s Stats Widget” on your page? Or do you regularly check up the...