Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *  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/>.
 *
 */

import android.graphics.Color;

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 if (inColor.startsWith("#")) { // #
                    String tmpColor = inColor.substring(1);
                    if (3 == tmpColor.length()) {
                        char[] t = new char[6];
                        t[0] = tmpColor.charAt(0);
                        t[1] = tmpColor.charAt(0);
                        t[2] = tmpColor.charAt(1);
                        t[3] = tmpColor.charAt(1);
                        t[4] = tmpColor.charAt(2);
                        t[5] = tmpColor.charAt(2);
                        inColor = "#" + String.valueOf(t);
                    }
                    reColor = Color.parseColor(inColor);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            reColor = 0;
        }
        return reColor;
    }
}