LapisSea

Untitled

Dec 11th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. private static final String[] COLOR_WORDS=      {"white",        "light_gray",   "gray",        "dark_gray", "black",  "red",      "pink",         "orange",     "yellow",     "green",    "magenta",    "cyan",       "blue"};
  2.     private static final int[][] COLOR_WORD_VALUES={{255, 255, 255},{192, 192, 192},{128, 128, 128},{64, 64, 64},{0, 0, 0},{255, 0, 0},{255, 175, 175},{255, 200, 0},{255, 255, 0},{0, 255, 0},{255, 0, 255},{0, 255, 255},{0, 0, 255}};
  3.    
  4.     public static Color parseColor(String s){
  5.         if(s==null||s.length()<4)return Color.WHITE;
  6.         int[] color;
  7.        
  8.         if(s.charAt(0)=='#'){
  9.            
  10.             color=new int[]{255,255,255,255};
  11.             s=s.substring(1);
  12.            
  13.             switch(s.length()){
  14.             case 3://RGB
  15.                 color[0]=Integer.parseInt(s.charAt(0)+""+s.charAt(0), 16);
  16.                 color[1]=Integer.parseInt(s.charAt(1)+""+s.charAt(1), 16);
  17.                 color[2]=Integer.parseInt(s.charAt(2)+""+s.charAt(2), 16);
  18.             break;
  19.             case 4://RGBA
  20.                 color[0]=Integer.parseInt(s.charAt(0)+""+s.charAt(0), 16);
  21.                 color[1]=Integer.parseInt(s.charAt(1)+""+s.charAt(1), 16);
  22.                 color[2]=Integer.parseInt(s.charAt(2)+""+s.charAt(2), 16);
  23.                 color[3]=Integer.parseInt(s.charAt(3)+""+s.charAt(3), 16);
  24.             break;
  25.             case 6://RRGGBB
  26.                 color[0]=Integer.parseInt(s.substring(0, 1), 16);
  27.                 color[1]=Integer.parseInt(s.substring(2, 3), 16);
  28.                 color[2]=Integer.parseInt(s.substring(4, 5), 16);
  29.             break;
  30.             case 8://RRGGBBAA
  31.                 color[0]=Integer.parseInt(s.substring(0, 1), 16);
  32.                 color[1]=Integer.parseInt(s.substring(2, 3), 16);
  33.                 color[2]=Integer.parseInt(s.substring(4, 5), 16);
  34.                 color[3]=Integer.parseInt(s.substring(6, 7), 16);
  35.             break;
  36.             }
  37.         }else{
  38.             int id=ArrayUtils.indexOf(COLOR_WORDS, s);
  39.             if(id!=-1)color=COLOR_WORD_VALUES[id];
  40.             else color=new int[]{255,255,255,255};
  41.         }
  42.        
  43.         return new Color(color[0], color[1], color[2], color[3]);
  44.     }
Add Comment
Please, Sign In to add comment