31

I would like to darken an existing color for use in a gradient brush. Could somebody tell me how to do this please?

C#, .net 2.0, GDI+

not my real name
  • 393
  • 4
  • 16
Brad
  • 20,302
  • 36
  • 84
  • 102

7 Answers7

39

As a simple approach, you can just factor the RGB values:

    Color c1 = Color.Red;
    Color c2 = Color.FromArgb(c1.A,
        (int)(c1.R * 0.8), (int)(c1.G * 0.8), (int)(c1.B * 0.8));

(which should darken it; or, for example, * 1.25 to brighten it)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 5
    This works, but doesn't actually give precise values for percuptual colour values. I'd suggest having a look at Richard or dommer's answer for details of the HSL/HSV colour model. – Ant Apr 29 '09 at 10:05
  • 9
    You have to be careful if you decide to use * 1.25 to make brighter as if the result is over 255 it'll throw an exception. – Tyco Kaine Nov 12 '12 at 17:29
  • You need to add control with this code like : `int R = (rgb.R * amt > 255) ? 255 : (int)(rgb.R * amt); int G = (rgb.G * amt > 255) ? 255 : (int)(rgb.G * amt); int B = (rgb.B * amt > 255) ? 255 : (int)(rgb.B * amt); Color c2 = Color.FromArgb(1, R, G, B);` – Toss Net Oct 29 '20 at 08:02
  • 2
    @TossNet `Math.Clamp(rgb.R * amt, 0, 255), Math.Clamp(rgb.G * amt, 0, 255), Math.Clamp(rgb.B * amt, 0, 255)` etc would be easier and more efficient (it only does the operations once) – Marc Gravell Oct 29 '20 at 08:11
32

You could also try using

ControlPaint.Light(baseColor, percOfLightLight)

ControlPaint.Light

or

ControlPaint.Dark(baseColor, percOfDarkDark)

ControlPaint.Dark

Alex
  • 601
  • 5
  • 5
18

Convert from RGB to HSV (or HSL), then adjust the V (or L) down and then convert back.

While System.Drawing.Color provides methods to get hue (H), saturation (S) and brightness it does not provide much in the way of other conversions, notable nothing to create a new instance from HSV (or HSV values), but the conversion is pretty simple to implement. The wikipedia articles give decent converage, starting here: "HSL and HSV".

kͩeͣmͮpͥ ͩ
  • 7,783
  • 26
  • 40
Richard
  • 106,783
  • 21
  • 203
  • 265
11

Here's some C# code for the conversions Richard mentioned:

RGB to HSL / HSL to RGB in C#

wazz
  • 4,953
  • 5
  • 20
  • 34
dommer
  • 19,610
  • 14
  • 75
  • 137
  • 1
    The link is broken. It works without the www in the url: http://geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm – Henrik Söderlund May 15 '13 at 13:11
  • 1
    There is a bug in the conversion code. Color.Red (#FF0000) -> hsl -> output: #808080. Many other colours (Color.OrangeRed, Color.Yellow, Color.Green) are converted without problems. – Julo Jan 08 '16 at 10:15
  • 3
    **FIX**: Add `if (h >= 6f) h -= 6f; if (h < 0f) h += 6f;` before `h /= 6.0;` to `RGB2HSL` function. – Julo Jan 08 '16 at 10:22
5

While the aforementioned methods do darken the color but they adjust the hue way to much so the result doesn't look very good. The best answer is to use Rich Newman's HSLColor class and adjust the luminosity.

public Color Darken(Color color, double darkenAmount) {
    HSLColor hslColor = new HSLColor(color);
    hslColor.Luminosity *= darkenAmount; // 0 to 1
    return hslColor;
}
Community
  • 1
  • 1
TugboatCaptain
  • 4,150
  • 3
  • 47
  • 79
  • The Luminosity attribute can also be increased. For example `hslColor.Luminosity *= 1.2;` Luminosity is a value from 0 to 240. If it goes above 240 it automaticaly caps at 240. – Hybris95 Sep 09 '14 at 13:03
  • My understanding is that the lightness value in HSL isn't very close to something you could call "brightness". Making something lighter will probably make it brighter, but not by the same amount for every color. https://en.wikipedia.org/wiki/HSL_and_HSV – B T Jan 18 '17 at 07:33
2

You must keep track that the value does not extend below 0 or above 255

Best approach is to use Math.Max/Math.MIn

dim newValue as integer = ...
'correct value if it is below 0 or above 255
newValue = Math.Max(Math.Min(newValue,255),0)
0

Similar to accepted answer but this uses a percentage of the difference between each rgb value and 255 to determine each new rgb value.

VB.NET

Function Lighten(orig As Color, Optional percent As Integer = 80) As Color
    'get remainders
    Dim rr As Integer = 255 - orig.R
    Dim gr As Integer = 255 - orig.G
    Dim br As Integer = 255 - orig.B

    'add a percentage of the remainder, plus original value
    Dim r As Integer = CInt(percent / 100 * rr) + orig.R
    Dim g As Integer = CInt(percent / 100 * gr) + orig.G
    Dim b As Integer = CInt(percent / 100 * br) + orig.B

    Return Color.FromArgb(r, g, b)
End Function

Function Darken(orig As Color, Optional percent As Integer = 80) As Color
    'subtract the percentage of the original value from the original value
    Dim r As Integer = orig.R - CInt(percent / 100 * orig.R)
    Dim g As Integer = orig.G - CInt(percent / 100 * orig.G)
    Dim b As Integer = orig.B - CInt(percent / 100 * orig.B)

    Return Color.FromArgb(r, g, b)
End Function

C#

public Color Lighten(Color orig, int percent = 80)
{
    // get remainders
    int rr = 255 - orig.R;
    int gr = 255 - orig.G;
    int br = 255 - orig.B;

    // add a percentage of the remainder, plus original value
    int r = System.Convert.ToInt32(percent / (double)100 * rr) + orig.R;
    int g = System.Convert.ToInt32(percent / (double)100 * gr) + orig.G;
    int b = System.Convert.ToInt32(percent / (double)100 * br) + orig.B;

    return Color.FromArgb(r, g, b);
}

public Color Darken(Color orig, int percent = 80)
{
    // subtract the percentage of the original value from the original value
    int r = orig.R - System.Convert.ToInt32(percent / (double)100 * orig.R);
    int g = orig.G - System.Convert.ToInt32(percent / (double)100 * orig.G);
    int b = orig.B - System.Convert.ToInt32(percent / (double)100 * orig.B);

    return Color.FromArgb(r, g, b);
}
torpid prey
  • 242
  • 1
  • 12