Reputation: 107
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?
Upvotes: 0
Views: 4392
Reputation: 1
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]);
Upvotes: 0
Reputation: 1295
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, )
}
Upvotes: 2
Reputation: 887
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");
Upvotes: 2