2013-01-18 69 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); 
    } 
相關問題