2011-03-23 35 views

回答

6
Color c = Color.FromArgb(1, 2, 3); 
float h, s, b; 
h = c.GetHue(); 
s = c.GetSaturation(); 
b = c.GetBrightness(); 
+0

謝謝。這樣,是否有任何快速轉換回RGB的方法? – Seva 2011-03-23 14:27:31

+0

請參閱SuperOli對ColorFromHSBFunction – Scoregraphic 2011-03-23 14:44:12

+0

的回答,該方法無效。全部變黑= x。但我可能會搜索另一個 – Seva 2011-03-23 14:54:04

2

你可以使用擴展方法,像這樣:

public static class Extensions 
{ 
    public static Tuple<float, float, float> GetPixelHSB(this Bitmap bitmap, int x, int y) 
    { 
     Color c = bitmap.GetPixel(x, y); 
     float h, s, b; 
     h = c.GetHue(); 
     s = c.GetSaturation(); 
     b = c.GetBrightness(); 

     return Tuple.Create<float, float, float>(h, s, b); 
    } 

    public static void SetPixelHSB(this Bitmap bitmap, int x, int y, Tuple<float, float, float> hsb) 
    { 
     bitmap.SetPixelHSB(x, y, hsb.Item1, hsb.Item2, hsb.Item3); 
    } 

    public static void SetPixelHSB(this Bitmap bitmap, int x, int y, float h, float s, float b) 
    { 
     Color c = ColorFromHSBFunction(h, s, b); 
     bitmap.SetPixel(x, y, c); 
    } 
} 

一個ColorFromHSBFunction可以發現here

+0

我試過ColorFromHSBFunction,但它不工作: X – Seva 2011-03-23 14:52:59

+0

不要忘記GetSaturation()和GetBrightness()以浮點形式返回值(範圍從0.0f到1.0f)。我給你的ColorFromHSBFunction的鏈接將這些值視爲從0到100的整數。你需要調整函數使其工作。 – SuperOli 2011-03-23 16:43:43

+0

我試過了。但也許我的代碼訪問該方法是錯誤的。謝謝。 – Seva 2011-03-23 19:09:16

相關問題