Java Color From toColor(String hexString)

Here you can find the source of toColor(String hexString)

Description

0xfffff,#ffffff

License

Apache License

Parameter

Parameter Description
hexString a parameter

Declaration

public static int toColor(String hexString) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from  w  w  w .  j av a2  s. c om*/
     * 0xfffff,#ffffff
     * @param hexString
     * @return
     */
    public static int toColor(String hexString) {
        return toColor(toRGB(hexString));
    }

    public static int toColor(int[] rgb) {
        int r = rgb[0];
        int g = rgb[1];
        int b = rgb[2];
        return (r << 16) | (g << 8) | b;
    }

    public static int toColor(int r, int g, int b) {
        return (r << 16) | (g << 8) | b;
    }

    public static int[] toRGB(int value) {
        int[] rgb = new int[3];
        rgb[0] = value >> 16 & 0xff;
        rgb[1] = value >> 8 & 0xff;
        rgb[2] = value & 0xff;
        return rgb;
    }

    public static int[] toRGB(String hexString) {
        if (hexString.startsWith("0x")) {
            hexString = hexString.substring(2);
        } else if (hexString.startsWith("#")) {
            hexString = hexString.substring(1);
        }

        int value = Integer.parseInt(hexString, 16);
        return toRGB(value);
    }
}

Related

  1. toColor(boolean val)
  2. toColor(String str)