Android Color Parse parseColor(String inColor)

Here you can find the source of parseColor(String inColor)

Description

parse Color

License

Open Source License

Declaration

public static int parseColor(String inColor) 

Method Source Code

//package com.java2s;
/*//from   ww w .jav  a2  s .  co  m
 *  Copyright (C) 2014 The AppCan Open Source Project.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.

 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    public static int parseColor(String inColor) {
        int reColor = 0;
        try {
            if (inColor != null && inColor.length() != 0) {
                inColor = inColor.replace(" ", "");
                if (inColor.charAt(0) == 'r') { //rgba
                    int start = inColor.indexOf('(') + 1;
                    int off = inColor.indexOf(')');
                    inColor = inColor.substring(start, off);
                    String[] rgba = inColor.split(",");
                    int r = Integer.parseInt(rgba[0]);
                    int g = Integer.parseInt(rgba[1]);
                    int b = Integer.parseInt(rgba[2]);
                    int a = Integer.parseInt(rgba[3]);
                    reColor = (a << 24) | (r << 16) | (g << 8) | b;
                } else { // #
                    inColor = inColor.substring(1);
                    if (3 == inColor.length()) {
                        char[] t = new char[6];
                        t[0] = inColor.charAt(0);
                        t[1] = inColor.charAt(0);
                        t[2] = inColor.charAt(1);
                        t[3] = inColor.charAt(1);
                        t[4] = inColor.charAt(2);
                        t[5] = inColor.charAt(2);
                        inColor = String.valueOf(t);
                    } else if (6 == inColor.length()) {
                        ;
                    }
                    long color = Long.parseLong(inColor, 16);
                    reColor = (int) (color | 0x00000000ff000000);
                }
            }
        } catch (Exception e) {
            ;
        }
        return reColor;
    }
}

Related

  1. parseColor(String colorName)
  2. parseHexColor(String colorStr)
  3. toARGB(String colorString)
  4. toARGB(int alpha, int red, int green, int blue)