Java RGB Color Convert To rgbaToHex(String color)

Here you can find the source of rgbaToHex(String color)

Description

rgba To Hex

License

Apache License

Declaration

public static String rgbaToHex(String color) 

Method Source Code

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

public class Main {
    public static String rgbaToHex(String color) {
        if (isHexColor(color)) {
            return color;
        }/*from   w ww  .jav  a2s.c o m*/
        String f = color.substring("rgba(".length());
        int l = f.lastIndexOf(",");
        if (l == -1) {
            return color;
        }
        String t = f.substring(0, l);
        String[] rgb = t.split(",");
        int r = Integer.parseInt(rgb[0], 10);
        int g = Integer.parseInt(rgb[1], 10);
        int b = Integer.parseInt(rgb[2], 10);

        //LogUtils.log(r+","+g+","+b);
        String rColor = Integer.toHexString(r);
        if (rColor.length() < 2) {
            rColor = "0" + r;
        }
        String gColor = Integer.toHexString(g);
        if (gColor.length() < 2) {
            gColor = "0" + g;
        }
        String bColor = Integer.toHexString(b);
        if (bColor.length() < 2) {
            bColor = "0" + b;
        }
        return "#" + rColor + gColor + bColor;
    }

    public static boolean isHexColor(String text) {
        if (!text.startsWith("#")) {
            return false;
        }

        if (text.length() != 4 && text.length() != 7) {
            return false;
        }

        return true;
    }
}

Related

  1. rgb2yuv(float r, float g, float b, float[] yuv)
  2. rgb565ToRGB(short pixel, byte[] rgb)
  3. rgb8ToPixel(byte[] nrgb)
  4. rgb8ToRgbRBXG(byte[] rgb)
  5. rgbaToColor(int r, int g, int b, int a)
  6. RGBAtoI(byte r, byte g, byte b, byte a)
  7. rgbBitfieldToString(int rgb)
  8. rgbToBgr(final byte[] pixels)
  9. RGBtoBGR(String color)