Java RGB Color Create RGBAFromHEX(String stringValue)

Here you can find the source of RGBAFromHEX(String stringValue)

Description

Returns RGBA from a HEX string (# should not be included)

License

Open Source License

Parameter

Parameter Description
stringValue a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static String RGBAFromHEX(String stringValue) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// w  w  w.  j a va2 s  .co  m
     * Returns RGBA from a HEX string (# should not be included)
     * @param stringValue
     * @return
     * @throws Exception
     */
    public static String RGBAFromHEX(String stringValue) throws Exception {
        int r = 0, g = 0, b = 0;
        if (stringValue.length() == 3) {
            String sh = stringValue.substring(0, 1);
            r = Integer.parseInt(sh + sh, 16);
            sh = stringValue.substring(1, 2);
            g = Integer.parseInt(sh + sh, 16);
            sh = stringValue.substring(2, 3);
            b = Integer.parseInt(sh + sh, 16);
        } else if (stringValue.length() == 6) {
            r = Integer.parseInt(stringValue.substring(0, 2), 16);
            g = Integer.parseInt(stringValue.substring(2, 4), 16);
            b = Integer.parseInt(stringValue.substring(4, 6), 16);
        } else {
            throw new Exception("Invalid hex value");
        }
        return String.format("rgba(%s, %s, %s, %s)", r, g, b, 1F);
    }
}

Related

  1. RGBA(int r, int g, int b, int a)
  2. rgba(int red, int green, int blue)
  3. rgba_bilinear_filter(int rgb00, int rgb01, int rgb10, int rgb11, int u, int v)
  4. rgbaColour(int red, int green, int blue, int alpha)
  5. RGBAequals(float[] rgba1, float[] rgba2, float eps)
  6. RGBAFromHSLA(float h, float s, float l, float a)
  7. rgbClamp(float c)
  8. rgbComponents(int argb)
  9. rgbDistance(int color1, int color2)