2

我想制作一些颜色的浅色版本。但是橙色(和其他颜色)给我带来了问题。当我使用 50% 的 System.Windows.Forms.ControlPaint.Light 时,它会将颜色更改为洋红色。

Color color1 = Color.Orange;
Color color2 = ControlPaint.Light(color1, 50f);

这导致 ffff5ee7, {Color [A=255, R=255, G=94, B=231]},即洋红色。

如何使用 ControlPaint.Light 实际创建浅橙色而不是洋红色?

(这发生在我正在变亮的其他一些颜色上,并且我没有使用命名颜色,而是使用 ARGB 值。我在这里使用命名颜色作为示例。)

谢谢

4

2 回答 2

4

我相信您的问题在于您使用50f的是 Percentage 而不是.5f. 文档没有说明,但根据此MSDN 论坛帖子 ,您应该使用 0 到 1 作为您的值。

于 2013-01-18T03:48:00.040 回答
2

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 );
    }
于 2013-05-19T10:33:39.310 回答