Created
November 15, 2011 15:21
-
-
Save JoshClose/1367327 to your computer and use it in GitHub Desktop.
Changing Color Tint with C#
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
public static class ColorExtensions | |
{ | |
/// <summary> | |
/// Tints the color by the given percent. | |
/// </summary> | |
/// <param name="color">The color being tinted.</param> | |
/// <param name="percent">The percent to tint. Ex: 0.1 will make the color 10% lighter.</param> | |
/// <returns>The new tinted color.</returns> | |
public static Color Lighten( this Color color, float percent ) | |
{ | |
var lighting = color.GetBrightness(); | |
lighting = lighting + lighting * percent; | |
if( lighting > 1.0 ) | |
{ | |
lighting = 1; | |
} | |
else if( lighting <= 0 ) | |
{ | |
lighting = 0.1f; | |
} | |
var tintedColor = ColorHelper.FromHsl( color.A, color.GetHue(), color.GetSaturation(), lighting ); | |
return tintedColor; | |
} | |
/// <summary> | |
/// Tints the color by the given percent. | |
/// </summary> | |
/// <param name="color">The color being tinted.</param> | |
/// <param name="percent">The percent to tint. Ex: 0.1 will make the color 10% darker.</param> | |
/// <returns>The new tinted color.</returns> | |
public static Color Darken( this Color color, float percent ) | |
{ | |
var lighting = color.GetBrightness(); | |
lighting = lighting - lighting * percent; | |
if( lighting > 1.0 ) | |
{ | |
lighting = 1; | |
} | |
else if( lighting <= 0 ) | |
{ | |
lighting = 0; | |
} | |
var tintedColor = ColorHelper.FromHsl( color.A, color.GetHue(), color.GetSaturation(), lighting ); | |
return tintedColor; | |
} | |
} |
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
public static class ColorHelper | |
{ | |
/// <summary> | |
/// Converts the HSL values to a Color. | |
/// </summary> | |
/// <param name="alpha">The alpha.</param> | |
/// <param name="hue">The hue.</param> | |
/// <param name="saturation">The saturation.</param> | |
/// <param name="lighting">The lighting.</param> | |
/// <returns></returns> | |
public static Color FromHsl( int alpha, float hue, float saturation, float lighting ) | |
{ | |
if( 0 > alpha || 255 < alpha ) | |
{ | |
throw new ArgumentOutOfRangeException( "alpha" ); | |
} | |
if( 0f > hue || 360f < hue ) | |
{ | |
throw new ArgumentOutOfRangeException( "hue" ); | |
} | |
if( 0f > saturation || 1f < saturation ) | |
{ | |
throw new ArgumentOutOfRangeException( "saturation" ); | |
} | |
if( 0f > lighting || 1f < lighting ) | |
{ | |
throw new ArgumentOutOfRangeException( "lighting" ); | |
} | |
if( 0 == saturation ) | |
{ | |
return Color.FromArgb( alpha, Convert.ToInt32( lighting * 255 ), Convert.ToInt32( lighting * 255 ), Convert.ToInt32( lighting * 255 ) ); | |
} | |
float fMax, fMid, fMin; | |
int iSextant, iMax, iMid, iMin; | |
if( 0.5 < lighting ) | |
{ | |
fMax = lighting - ( lighting * saturation ) + saturation; | |
fMin = lighting + ( lighting * saturation ) - saturation; | |
} | |
else | |
{ | |
fMax = lighting + ( lighting * saturation ); | |
fMin = lighting - ( lighting * saturation ); | |
} | |
iSextant = (int)Math.Floor( hue / 60f ); | |
if( 300f <= hue ) | |
{ | |
hue -= 360f; | |
} | |
hue /= 60f; | |
hue -= 2f * (float)Math.Floor( ( ( iSextant + 1f ) % 6f ) / 2f ); | |
if( 0 == iSextant % 2 ) | |
{ | |
fMid = hue * ( fMax - fMin ) + fMin; | |
} | |
else | |
{ | |
fMid = fMin - hue * ( fMax - fMin ); | |
} | |
iMax = Convert.ToInt32( fMax * 255 ); | |
iMid = Convert.ToInt32( fMid * 255 ); | |
iMin = Convert.ToInt32( fMin * 255 ); | |
switch( iSextant ) | |
{ | |
case 1: | |
return Color.FromArgb( alpha, iMid, iMax, iMin ); | |
case 2: | |
return Color.FromArgb( alpha, iMin, iMax, iMid ); | |
case 3: | |
return Color.FromArgb( alpha, iMin, iMid, iMax ); | |
case 4: | |
return Color.FromArgb( alpha, iMid, iMin, iMax ); | |
case 5: | |
return Color.FromArgb( alpha, iMax, iMin, iMid ); | |
default: | |
return Color.FromArgb( alpha, iMax, iMid, iMin ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment