Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final String[] COLOR_WORDS= {"white", "light_gray", "gray", "dark_gray", "black", "red", "pink", "orange", "yellow", "green", "magenta", "cyan", "blue"};
- 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}};
- public static Color parseColor(String s){
- if(s==null||s.length()<4)return Color.WHITE;
- int[] color;
- if(s.charAt(0)=='#'){
- color=new int[]{255,255,255,255};
- s=s.substring(1);
- switch(s.length()){
- case 3://RGB
- color[0]=Integer.parseInt(s.charAt(0)+""+s.charAt(0), 16);
- color[1]=Integer.parseInt(s.charAt(1)+""+s.charAt(1), 16);
- color[2]=Integer.parseInt(s.charAt(2)+""+s.charAt(2), 16);
- break;
- case 4://RGBA
- color[0]=Integer.parseInt(s.charAt(0)+""+s.charAt(0), 16);
- color[1]=Integer.parseInt(s.charAt(1)+""+s.charAt(1), 16);
- color[2]=Integer.parseInt(s.charAt(2)+""+s.charAt(2), 16);
- color[3]=Integer.parseInt(s.charAt(3)+""+s.charAt(3), 16);
- break;
- case 6://RRGGBB
- color[0]=Integer.parseInt(s.substring(0, 1), 16);
- color[1]=Integer.parseInt(s.substring(2, 3), 16);
- color[2]=Integer.parseInt(s.substring(4, 5), 16);
- break;
- case 8://RRGGBBAA
- color[0]=Integer.parseInt(s.substring(0, 1), 16);
- color[1]=Integer.parseInt(s.substring(2, 3), 16);
- color[2]=Integer.parseInt(s.substring(4, 5), 16);
- color[3]=Integer.parseInt(s.substring(6, 7), 16);
- break;
- }
- }else{
- int id=ArrayUtils.indexOf(COLOR_WORDS, s);
- if(id!=-1)color=COLOR_WORD_VALUES[id];
- else color=new int[]{255,255,255,255};
- }
- return new Color(color[0], color[1], color[2], color[3]);
- }
Add Comment
Please, Sign In to add comment