Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Create a lighter/darker color based on a system color [duplicate]

Tags:

c#

colors

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?

like image 377
Svish Avatar asked Apr 29 '09 08:04

Svish


Video Answer


2 Answers

ControlPaint.Light .Dark .DarkDark, etc.

Color lightRed = ControlPaint.Light( Color.Red ); 
like image 116
Paul Alexander Avatar answered Sep 18 '22 13:09

Paul Alexander


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); } 
like image 20
Pavel Vladov Avatar answered Sep 20 '22 13:09

Pavel Vladov