Helo all. I am working on a project, and i want to convert the argb value into descriptive text color and show it to the user.
I am using palette api to extract argb color from an image and I want to convert it into descriptive text color but I have no idea how to do that. Can anyone help me with that. I tried by giving pre defined values but didn't got desired result. I would like to know if there is any api or library which can do so or any alternative to do it.
Palette palette = Palette.from(bitmap).generate();
binding.colour.setText(argbToTextColor(palette.getDominantColor(0)));
public String argbToTextColor(int argb) {
int rgb = argb & 0x00FFFFFF;
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
if (red < 50 && green < 50 && blue < 50) {
return "Black";
} else if (red > 200 && green > 200 && blue > 200) {
return "White";
} else if (red > green && red > blue) {
return "Red";
} else if (green > red && green > blue) {
return "Green";
} else if (blue > red && blue > green) {
return "Blue";
} else if (red > 200 && green > 200) {
return "Yellow";
} else if (red > 200 && blue > 200) {
return "Magenta";
} else if (green > 200 && blue > 200) {
return "Cyan";
} else if (red > 150 && green > 100 && blue < 50) {
return "Orange";
} else if (red < 100 && green < 100 && blue < 100) {
return "Gray";
} else if (red > 150 && green > 50 && blue > 150) {
return "Purple";
} else if (red < 100 && green < 100 && blue > 150) {
return "Pink";
} else if (red > 100 && green > 50 && blue < 50) {
return "Brown";
} else {
return "Unknown Color";
}
}
In C# there is a table of known colors, but there are 140 of them, also there is a library of Color Names with 30261 of them, but to cover all the variants with transparency and 256^3 values (16 777 216) is unlikely to work.