2011-04-20 70 views
15

我想用它來計算出,如果顏色是淺色或深色現在如何將Hex轉換爲RGB?

Evaluate whether a HEX value is dark or light

。它需要在int

float calcLuminance(int rgb) 
{ 
     int r = (rgb & 0xff0000) >> 16; 
     int g = (rgb & 0xff00) >> 8; 
     int b = (rgb & 0xff); 

     return (r*0.299f + g*0.587f + b*0.114f)/256; 
} 

雖然我有一個十六進制顏色。

我試圖做到這一點

var color = System.Drawing.ColorTranslator.FromHtml("#FFFFFF"); 
    int rgb = color.R + color.G + color.B; 
    var a = calcLuminance(rgb); 

我0.11725我認爲這必須是在0-256或類似的東西的範圍。

我在做什麼錯?我是否需要將R轉換爲int?或者我剛剛離開?

回答

8

我試圖用它來計算出,如果顏色是淺色或深色

只需使用Color.GetBrightness()


[編輯]

我想確定我的文本是否應該使用白色或黑色。所以,任何東西≤5,我應該使用白色和> .5黑色?

有一個numberofways,以確定在給定的背景下,沒有一個是完美的使用什麼顏色。

最後一個鏈接實際上建議僅使用黑色/白色,但選擇0.73而不是0.5的截止點。我認爲你應該堅持這一點,如果你覺得它不適合你,就改變它。

+0

@BlueRaja - Danny Pflughoeft-所以像這樣System.Drawing.Color someColor = System.Drawing.Color.FromArgb(color.R,color.G,color.B); float ab = someColor.GetBrightness();無論如何要做到這一點,而不是先把它轉換成rgb? – chobo2 2011-04-20 20:12:25

+3

@ chobo2只是'float ab = System.Drawing.ColorTranslator.FromHtml(「#FFFFFF」)。GetBrightness();':) – 2011-04-20 20:14:10

+0

@Chobo:那麼,你怎麼輸入*顏色到電腦中?你給**的例子是** RGB:''#FFFFFF「',第一個'」FF「'是R *(十六進制)*,第二個'」FF「'是G,第三個''FF''是B.在上面註釋中,@lasseespeholt表示,你可以從你的RGB字符串中得到一行中的亮度。 – 2011-04-20 20:15:15

0

來自Color結構的R,GB的範圍是0-255。

爲了讓你在你的函數所期望的RGB值,則需要相應地向左移位:

int rgb = (int)color.R << 16 + (int)color.G << 8 + color.B; 
2

的問題,在我看來,是你的rgb計算。你將這些值加在一起給出一個介於0和3 * 255之間的數字,這顯然不是你方法期望的值。你將不得不計算出它像這樣

int rgb = (int)color.R << 16 + (int)color.G << 8 + color.B; 

這應該是等同於本(除阿爾法值不使用)

int rgb = color.ToArgb(); 

最後,你可以在克里斯看到哈斯回答,你可以直接轉換爲int來跳過這一步。

1

calcLuminance只返回一個百分比。

19

就在十六進制字符串轉換爲整數:

int color = Convert.ToInt32("FFFFFF", 16); 
+0

哎呦,我upvoted這一點,因爲我誤解了問題。他實際上並沒有問「如何從十六進制轉換爲RGB」 - 他真正想知道的是如何獲得RGB顏色的亮度(亮度)。 – 2011-04-20 20:09:28

+1

他有兩個不同的潛在問題。 「如何做x」和「有沒有比x更好的方法」。我回答了第一個,你回答了第二個! – 2011-04-20 20:12:33

3

有一點話題,但這裏是我創建的Color結構的擴展方法,用於計算不同算法的亮度。希望它可以幫助你。

public static class ColorExtensions 
{ 
    /// <summary> 
    /// Gets the luminance of the color. A value between 0 (black) and 1 (white) 
    /// </summary> 
    /// <param name="color">The color.</param> 
    /// <param name="algorithm">The type of luminance alg to use.</param> 
    /// <returns>A value between 0 (black) and 1 (white)</returns> 
    public static double GetLuminance(this Color color, LuminanceAlgorithm algorithm = LuminanceAlgorithm.Photometric) 
    { 
     switch (algorithm) 
     { 
      case LuminanceAlgorithm.CCIR601: 
       return (0.2126 * color.R + 0.7152 * color.G + 0.0722 * color.B)/255; 

      case LuminanceAlgorithm.Perceived: 
       return (Math.Sqrt(0.241 * Math.Pow(color.R, 2) + 0.691 * Math.Pow(color.G, 2) + 0.068 * Math.Pow(color.B, 2))/255); 

      case LuminanceAlgorithm.Photometric: 
       return (0.299 * color.R + 0.587 * color.G + 0.114 * color.B)/255; 
     } 

    } 

    /// <summary> 
    /// The luminances 
    /// </summary> 
    public enum LuminanceAlgorithm 
    { 
     /// <summary> 
     /// Photometric/digital ITU-R 
     /// </summary> 
     Photometric, 

     /// <summary> 
     /// Digital CCIR601 (gives more weight to the R and B components, as preciev by the human eye) 
     /// </summary> 
     CCIR601, 

     /// <summary> 
     /// A perceived luminance 
     /// </summary> 
     Perceived 
    } 
} 
7

您可以使用:

public string GenerateRgba(string backgroundColor, decimal backgroundOpacity) 
{ 
Color color = ColorTranslator.FromHtml(hexBackgroundColor); 
int r = Convert.ToInt16(color.R); 
int g = Convert.ToInt16(color.G); 
int b = Convert.ToInt16(color.B); 
return string.Format("rgba({0}, {1}, {2}, {3});", r, g, b, backgroundOpacity); 
} 

Link To original Post by jeremy clifton on git