Duplicate
How do I adjust the brightness of a color?
How do I determine darker or lighter color variant of a given color?
Programmatically Lighten a Color
Say I have
var c = Color.Red;
Now I want to create a new Color
that is lighter or darker than that color. How can I do that without too much hassle?
ControlPaint.Light .Dark .DarkDark, etc.
Color lightRed = ControlPaint.Light( Color.Red );
I recently blogged about this. The main idea is to apply a given correction factor to each of the color components. The following static method modifies the brightness of a given color with a specified correction factor and produces a darker or a lighter variant of that color:
/// <summary> /// Creates color with corrected brightness. /// </summary> /// <param name="color">Color to correct.</param> /// <param name="correctionFactor">The brightness correction factor. Must be between -1 and 1. /// Negative values produce darker colors.</param> /// <returns> /// Corrected <see cref="Color"/> structure. /// </returns> public static Color ChangeColorBrightness(Color color, float correctionFactor) { float red = (float)color.R; float green = (float)color.G; float blue = (float)color.B; if (correctionFactor < 0) { correctionFactor = 1 + correctionFactor; red *= correctionFactor; green *= correctionFactor; blue *= correctionFactor; } else { red = (255 - red) * correctionFactor + red; green = (255 - green) * correctionFactor + green; blue = (255 - blue) * correctionFactor + blue; } return Color.FromArgb(color.A, (int)red, (int)green, (int)blue); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With