0

I have alredy done the converter RGB to HEX but i don't find the function to do the HEX to RGB converter. For the RGB to HEX converter i have used 3 seekbar and I've done the project(like the code).

But now i want to use a seekbar that have only the HEX value for HEX to RGB converter.But I don't find the right function, What I've to do?

enter image description here

gabriele.pacor
  • 107
  • 1
  • 2
  • 9

3 Answers3

2

Might I suggest:

int color = Color.parseColor("#123456");

Additionally, you might try:

public static int[] getRGB(final int hex) {
    int r = (hex & 0xFF0000) >> 16;
    int g = (hex & 0xFF00) >> 8;
    int b = (hex & 0xFF);
    return new int[] {r, g, b};
}

int hex = 0x123456; getRGB(hex);


Or, if you need it from a string:

public static int[] getRGB(final String rgb)
{
    int r = Integer.parseInt(rgb.substring(0, 2), 16); // 16 for hex
    int g = Integer.parseInt(rgb.substring(2, 4), 16); // 16 for hex
    int b = Integer.parseInt(rgb.substring(4, 6), 16); // 16 for hex
    return new int[] {r, g, b};
}

getRGB("123456");

Jacob Holloway
  • 887
  • 8
  • 24
  • Thank you, I noticed. You mistaked in param name on function `getRGB`. Param name must be final String **hexColor** – HardCoder Jun 13 '20 at 13:06
2

In Kotlin:

fun getRgbFromHex(hex: String): IntArray {
            val initColor = Color.parseColor(hex)
            val r = Color.red(initColor)
            val g = Color.green(initColor)
            val b = Color.blue(initColor)
            return intArrayOf(r, g, b, )
}
Amine Harbaoui
  • 1,247
  • 2
  • 17
  • 34
0

for ARGB add this function to your Android code:

public static int[] getARGB(final int hex) {
    int a = (hex & 0xFF000000) >> 24;
    int r = (hex & 0xFF0000) >> 16;
    int g = (hex & 0xFF00) >> 8;
    int b = (hex & 0xFF);
    return new int[] {a, r, g, b};
}

Parse your String color to Color:

int color = Color.parseColor("#FFFFFFFF");

Execute:

int argb[] = getARGB(color);
Log.d("getARGB", argb[0] + " " + argb[1] + " " + argb[2] + " " + argb[3]);