2013-01-18 68 views
1

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

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

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

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

(这是发生在一些其他颜色我减轻,而我没有使用命名的颜色,但ARGB值。我这里使用的命名颜色作为例子。)

感谢

回答

3

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

+0

谢谢。 MSDN帮助列出参数为:percOfLightLight 减轻指定颜色的百分比。这令人困惑。此外,似乎我需要大于1.0的数字才能达到我所要求的减轻效果。 – Matt

+0

@Matt欢迎您,这就是为什么我链接到MSDN的帖子。 –

1

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); 
    } 
相关问题