IMO的MSDN帮助令人困惑,甚至是错误的。 我开发了这个代码...
/// <summary>
/// Makes the color lighter by the given factor (0 = no change, 1 = white).
/// </summary>
/// <param name="color">The color to make lighter.</param>
/// <param name="factor">The factor to make the color lighter (0 = no change, 1 = white).</param>
/// <returns>The lighter color.</returns>
public static Color Light(this Color color, float factor)
{
float min = 0.001f;
float max = 1.999f;
return System.Windows.Forms.ControlPaint.Light(color, min + factor.MinMax(0f, 1f) * (max - min));
}
/// <summary>
/// Makes the color darker by the given factor (0 = no change, 1 = black).
/// </summary>
/// <param name="color">The color to make darker.</param>
/// <param name="factor">The factor to make the color darker (0 = no change, 1 = black).</param>
/// <returns>The darker color.</returns>
public static Color Dark(this Color color, float factor)
{
float min = -0.5f;
float max = 1f;
return System.Windows.Forms.ControlPaint.Dark(color, min + factor.MinMax(0f, 1f) * (max - min));
}
/// <summary>
/// Lightness of the color between black (-1) and white (+1).
/// </summary>
/// <param name="color">The color to change the lightness.</param>
/// <param name="factor">The factor (-1 = black ... +1 = white) to change the lightness.</param>
/// <returns>The color with the changed lightness.</returns>
public static Color Lightness(this Color color, float factor)
{
factor = factor.MinMax(-1f, 1f);
return factor < 0f ? color.Dark(-factor) : color.Light(factor);
}
public static float MinMax(this float value, float min, float max)
{
return Math.Min(Math.Max(value, min), max);
}
谢谢。 MSDN帮助列出参数为:percOfLightLight 减轻指定颜色的百分比。这令人困惑。此外,似乎我需要大于1.0的数字才能达到我所要求的减轻效果。 – Matt
@Matt欢迎您,这就是为什么我链接到MSDN的帖子。 –