/// <summary> /// Increases the darkness of the color based on the specified <paramref name="percent"/>. /// </summary> /// <param name="color">The input color.</param> /// <param name="percent">The amount of darkness (specified in percent) that should be added to the color.</param> /// <returns>An instance of <see cref="IColor"/> representing the output color.</returns> public static IColor Darken(this IColor color, float percent) { if (color is IAlphaColor) { // Make sure that the color is in the HSLA color space HslaColor hsla = color as HslaColor ?? color.ToHsla(); // Increase the darkness by the specified percent double lightness = hsla.Lightness - percent / 100f; // Return a new HSLA color return(new HslaColor(hsla.Hue, hsla.Saturation, Math.Min(1, lightness), hsla.Alpha)); } else { // Make sure that the color is in the HSL color space HslColor hsl = color as HslColor ?? color.ToHsl(); // Increase the darkness by the specified percent double lightness = hsl.Lightness - percent / 100f; // Return a new HSL color return(new HslColor(hsl.Hue, hsl.Saturation, Math.Min(1, lightness))); } }
/// <summary> /// Decreases the saturation of a color in the HSL color space by an absolute amount. /// </summary> /// <param name="color">The input color.</param> /// <param name="percent">A percentage.</param> /// <returns>An instance of <see cref="IColor"/> representing the output color.</returns> public static IColor Desaturate(this IColor color, float percent) { if (color is IAlphaColor) { // Make sure that the color is in the HSLA color space HslaColor hsla = color as HslaColor ?? color.ToHsla(); // Decrease the saturation by the specified percent double saturation = hsla.Saturation - percent / 100f; // Return a new HSLA color return(new HslaColor(hsla.Hue, Math.Max(0, saturation), hsla.Lightness, hsla.Alpha)); } else { // Make sure that the color is in the HSL color space HslColor hsl = color as HslColor ?? color.ToHsl(); // Decrease the saturation by the specified percent double saturation = hsl.Saturation - percent / 100f; // Return a new HSL color return(new HslColor(hsl.Hue, Math.Max(0, saturation), hsl.Lightness)); } }