How to convert Hex to RGB?

后端 未结 8 1686
暖寄归人
暖寄归人 2021-02-12 11:13

I am trying to use this to figure out if a color is light or dark

Evaluate whether a HEX value is dark or light

Now. It takes in a int



        
8条回答
  •  生来不讨喜
    2021-02-12 12:03

    Your idea is OK, but your function is wrong, correct one is here:

    int rgb = Convert.ToInt32("#FFFFFF", 16);
    var a = calcLuminance(rgb);
    
    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;
    }
    

提交回复
热议问题