How to convert Hex to RGB?

后端 未结 8 1565
暖寄归人
暖寄归人 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 11:45

    The problem, as I see it, is your calculation of rgb. You add the values together which gives you a number between 0 and 3*255 which clearly isn't the value your method expect. You will have to calculate it like this

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

    which should be equivalent to this (except for the alpha-value you don't use)

    int rgb = color.ToArgb();
    

    Lastly, as you can see in Chris Haas answer, you can skip this step by converting directly to an int.

提交回复
热议问题