Created
December 17, 2017 00:17
-
-
Save zihotki/09fc41d52981fb6f93a81ebf20b35cd5 to your computer and use it in GitHub Desktop.
.net C# make a color lighter or darker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing.
ChangeColorBrightness(Color.Green, -0.3f);