How to convert hex to rgb using Java?

后端 未结 19 2037
-上瘾入骨i
-上瘾入骨i 2020-11-27 13:46

How can I convert hex color to RGB code in Java? Mostly in Google, samples are on how to convert from RGB to hex.

19条回答
  •  独厮守ぢ
    2020-11-27 14:15

    For shortened hex code like #fff or #000
    
    int red = "colorString".charAt(1) == '0' ? 0 : 
         "colorString".charAt(1) == 'f' ? 255 : 228;  
    int green =
         "colorString".charAt(2) == '0' ? 0 :  "colorString".charAt(2) == 'f' ?
         255 : 228;  
    int blue = "colorString".charAt(3) == '0' ? 0 : 
         "colorString".charAt(3) == 'f' ? 255 : 228;
    
    Color.rgb(red, green,blue);
    

提交回复
热议问题