parse Color - Android Graphics

Android examples for Graphics:Color Value

Description

parse Color

Demo Code


//package com.java2s;
import android.graphics.Color;

public class Main {
    public static int parseColor(String colorString) {
        try {/*from w ww.ja  v a 2  s  . c  om*/
            return Color.parseColor(colorString);
        } catch (IllegalArgumentException e) {
            if (isShortColorCode(colorString)) {
                return parseShortColorCode(colorString);
            } else {
                return Color.BLACK;
            }
        }
    }

    private static boolean isShortColorCode(String colorString) {
        return colorString.length() == 4 && colorString.startsWith("#")
                && isHexadecimalColor(colorString);
    }

    private static int parseShortColorCode(String colorString) {
        String rgb = colorString.substring(1);
        int r = Integer.parseInt(rgb.substring(0, 1) + rgb.substring(0, 1),
                16);
        int g = Integer.parseInt(rgb.substring(1, 2) + rgb.substring(1, 2),
                16);
        int b = Integer.parseInt(rgb.substring(2, 3) + rgb.substring(2, 3),
                16);
        return Color.rgb(r, g, b);
    }

    private static boolean isHexadecimalColor(String colorString) {
        try {
            Long.parseLong(colorString.substring(1), 16);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}

Related Tutorials