ControlPaint.Light将Orange更改为Magenta

时间:2013-01-18 03:28:16

标签: .net rgb

我想制作一些颜色较浅的版本。但橘子(以及其他颜色)给我带来了麻烦。 当我使用50%的System.Windows.Forms.ControlPaint.Light时,它会将颜色更改为洋红色。

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

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

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

(这是我正在使用的其他一些颜色,我没有使用命名颜色,而是使用ARGB值。我在这里使用了命名颜色作为示例。)

由于

2 个答案:

答案 0 :(得分:3)

我认为您的问题在于您使用50f作为百分比而不是.5f。文档没有说明它,但根据这个MSDN Forum posting,您应该使用0到1作为您的值。

答案 1 :(得分: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 );
    }