Java Hex to Color hexToColor(String hexStr, boolean throwException, int[] buffer)

Here you can find the source of hexToColor(String hexStr, boolean throwException, int[] buffer)

Description

hex To Color

License

Open Source License

Declaration

public static final boolean hexToColor(String hexStr, boolean throwException, int[] buffer) 

Method Source Code


//package com.java2s;
import java.awt.Color;

public class Main {
    public static final int ALPHA_OPAQUE = 255;

    public static final boolean hexToColor(String hexStr, boolean throwException, int[] buffer) {
        int type = checkHexString(hexStr, throwException);
        if (type < 0)
            return (false);
        int start = ((type & 1) != 0) ? 1 : 0;

        if (((type & 2) != 0) && buffer.length < 4) {
            if (throwException)
                throw new IllegalArgumentException("buffer too small");

            return (false);
        }/*  w  w w .j a  v a 2s  . c o  m*/

        int j = 0;
        for (int i = start; i < hexStr.length(); i += 2, j++) {
            int c = calcColorComp(hexStr, start + j * 2, throwException);
            if (c < 0)
                return (false);

            buffer[j] = c;
        }

        if ((type & 2) != 0) // has alpha?
        {
            return (true);
        }

        if (buffer.length >= 4)
            buffer[3] = ALPHA_OPAQUE;

        return (true);
    }

    public static final boolean hexToColor(String hexStr, int[] buffer) {
        return (hexToColor(hexStr, true, buffer));
    }

    public static final Color hexToColor(String hexStr, boolean throwException) {
        int[] values = new int[4];

        values[3] = ALPHA_OPAQUE;

        if (!hexToColor(hexStr, throwException, values))
            return (null);

        if (values[3] != ALPHA_OPAQUE)
            return (new Color(values[0], values[1], values[2], values[3]));

        return (new Color(values[0], values[1], values[2]));
    }

    public static final Color hexToColor(String hexStr) {
        return (hexToColor(hexStr, true));
    }

    /**
     * Checks the hex-string for validity.
     * 
     * @param hexStr
     * @param throwException
     * 
     * @return 0 for RGB hex-string without leading #,
     *         1 for RGB hex-string with leading #,
     *         2 for RGBA hex-string without leading #,
     *         3 for RGBA hex-string with leading #
     */
    public static final int checkHexString(String hexStr, boolean throwException) {
        if (hexStr == null) {
            if (throwException)
                throw new NullPointerException("hexStr");

            return (-1);
        }

        int type = -1;

        if (hexStr.startsWith("#")) {
            if (hexStr.length() == 7)
                type = 1;
            else if (hexStr.length() == 9)
                type = 3;
            else if (throwException)
                throw new IllegalArgumentException("Illegal hex-string.");
            else
                return (-2);
        } else {
            if (hexStr.length() == 6)
                type = 0;
            else if (hexStr.length() == 8)
                type = 2;
            else if (throwException)
                throw new IllegalArgumentException("Illegal hex-string (" + hexStr + ").");
            else
                return (-2);
        }

        return (type);
    }

    /**
     * Checks the hex-string for validity.
     * 
     * @param hexStr
     * 
     * @return 0 for RGB hex-string without leading #,
     *         1 for RGB hex-string with leading #,
     *         2 for RGBA hex-string without leading #,
     *         3 for RGBA hex-string with leading #
     */
    public static final int checkHexString(String hexStr) {
        return (checkHexString(hexStr, true));
    }

    private static final int calcColorComp(String str, int start, boolean throwException) {
        final int h = hexToInt(str.charAt(start + 0));
        final int l = hexToInt(str.charAt(start + 1));

        if ((h == -1) || (l == -1)) {
            if (throwException)
                throw new IllegalArgumentException("given hex string is not valid");

            return (-1);
        }

        return ((h * 16) + l);
    }

    /**
     * Convertes the char interpreded as hex to its corresponding int. Returns
     * -1, if the char is not a hex char.
     * 
     * @param ch
     * 
     * @return the corresponding int or -1
     */
    public static final int hexToInt(char ch) {
        if ((ch >= 'A') && (ch <= 'Z')) {
            return (ch - 55);
        }

        if ((ch >= 'a') && (ch <= 'z')) {
            return (ch - 87);
        }

        if ((ch >= '0') && (ch <= '9')) {
            return (ch - 48);
        }

        return (-1);
    }
}

Related

  1. hexToColor(final String hex)
  2. hexToColor(String color)
  3. hexToColor(String color)
  4. hexToColor(String colorHex)
  5. hexToColor(String hex)
  6. hexToColor(String hexString)
  7. hexToColor(String hexString)
  8. hexToColor(String input)
  9. hexToColor(String s)