如何通过使用十六进制codeS检测所有色调? [英] How to detect all color tones by using hex codes?
问题描述
我想开发一个Android应用程序将采取一个物体的图片,然后检测物体的颜色。我想说明的颜色有对象的用户。我根据密度和亮度的答案的帮助下,我的问题在这个环节中实现的检测颜色:
I'm trying to develop an Android application which will take the picture of an object and then detect the color of that object. I want to show user which color has the object. I've implemented the detecting color according to density and luminance with the help of answers to my question in this link:
<一个href=\"http://stackoverflow.com/questions/28670901/what-is-the-best-way-to-implement-color-detection-in-android/28673282?noredirect=1#comment45727933_28673282\">What是落实颜色检测的Android的最佳途径?
在这一点上,我能够得到的颜色为十六进制code。我真正想要做的是,能够告知用户哪些颜色是十六进制code。
At this point, I'm able to get color as a hex code. What I really want to do is that being able to inform user about which color is that hex code.
我不希望只是限制我的应用程序,以检测为主色调,所以我想它来检测许多不同的颜色。
I don't want to limit my application just to detect main colors so I want it to detect many different colors.
我如何使用这些十六进制$ C $这样做CS?
How can I do this by using these hex codes?
感谢您提前。
推荐答案
很多的努力,找出我的问题的有效解决方案,终于我知道了!在这里,下面的人谁将会解决这个问题面临着能够找到一个替代的解决方案:
After a lot of effort to find out an efficient solution for my problem, finally I got it! Here below anyone who will face with this problem can find an alternative solution:
生成找到最接近的颜色名称这是你的数据库的方法:
Generate a method to find closest color name which is in your database:
private String findClosedColor(String hexColor) {
int rgb[] = hexToRGB(hexColor);
int min = 3 * (int) pow(256, 2) + 1;
ArrayList<HashMap<String, String>> colorList = getColorList();
String colorName = null;
int i;
int len = colorList.size();
for (i = 0; i < len; i++) {
HashMap<String, String> map = colorList.get(i);
String colorCode = map.get("code");
Log.w("myApp", "HashMap'ten gelen colorCode:" + colorCode);
if (colorCode != null) {
int df = rgbDistance(hexToRGB(colorCode), rgb);
if (df < min) {
min = df;
colorName = map.get("name");
}
}
}
return colorName;
}
private int rgbDistance(int[] c1, int[] c2) {
return ( (int) pow(c1[0] - c2[0], 2)) + ((int) pow(c1[1] - c2[1], 2)) + ((int) pow(c1[2] - c2[2], 2));
}
private int[] hexToRGB( String hexCode)
{
int returnValue[] = new int[3];
if (hexCode.charAt(0) == '#')
{
hexCode = hexCode.substring(1);
}
if (hexCode.length() < 6)
{
returnValue[0] = -1;
returnValue[1] = -1;
returnValue[2] = -1;
}
else
{
int r = fromHex(hexCode.substring(0, 2));
int g = fromHex(hexCode.substring(2, 4));
int b = fromHex(hexCode.substring(4, 6));
returnValue[0] = r;
returnValue[1] = g;
returnValue[2] = b;
}
return returnValue;
}
private int fromHex( String n) {
n = n.toUpperCase();
if (n.length() < 2)
return -1;
int f1 = letterToCode(n.charAt(0));
int f2 = letterToCode(n.charAt(1));
if (f1 == -1 || f2 == -1) {
return -1;
} else {
return f1 * 16 + f2;
}
}
private int letterToCode(char n) {
switch (n) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case 'E': return 14;
case 'F': return 15;
default: return -1;
}
}
getColorList()函数返回从我的数据库中的颜色列表。有了这个解决方案,我可以很容易地在我的数据库选择接近检测名每六角code。
getColorList() function returns the color list from my database. With this solution, I can easily detect every hex code by choosing closer name in my database.
最好的问候给大家......
Best Regards to everyone...
这篇关于如何通过使用十六进制codeS检测所有色调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!