-1

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:

What is the best way to implement Color Detection in Android?

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.

How can I do this by using these hex codes?

Thank you in advance.

Community
  • 1
  • 1
cgbs
  • 83
  • 3
  • 13

1 Answers1

0

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() 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...

cgbs
  • 83
  • 3
  • 13