Skip to content Skip to sidebar Skip to footer

How To Detect All Color Tones By Using Hex Codes?

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

Solution 1:

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': return0;
        case '1': return1;
        case '2': return2;
        case '3': return3;
        case '4': return4;
        case '5': return5;
        case '6': return6;
        case '7': return7;
        case '8': return8;
        case '9': return9;
        case 'A': return10;
        case 'B': return11;
        case 'C': return12;
        case 'D': return13;
        case 'E': return14;
        case 'F': return15;
        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...

Post a Comment for "How To Detect All Color Tones By Using Hex Codes?"