-
-
Save PieroCastillo/ad1688737db058966d4291c0a29287da to your computer and use it in GitHub Desktop.
.net C# make a color lighter or darker
This file contains hidden or 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