admin管理员组文章数量:1125480
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";
}
}
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";
}
}
Share
Improve this question
asked Jan 9 at 7:07
FarazFirozFarazFiroz
333 bronze badges
New contributor
FarazFiroz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2 Answers
Reset to default 3In 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.
Since there are way more color values than names (as already said in another answer), the best you can do is probably the following:
- Make a table of known colors (e.g. based on this)
- Choose the color distance metric that works best for your use case
- Find the known color with the minimal distance from the given color value[*]
[*] For efficient search you might want to utilize a vector database.
本文标签: javaHow to conver argb into descriptive text colorStack Overflow
版权声明:本文标题:java - How to conver argb into descriptive text color - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736666704a1946702.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论