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的帖子。 –