Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find nearest Color from a colors list

Tags:

android

I have a list of colors with R,G,B values. Some of data is shown below using "Color.rgb(R,G,B)". Then I have image processing, when user touches somewhere on an image, I get an RGB value for that touched pixel. How do I search this RGB value from my colors list. Search function should be:if RGB exists in list return position or value. If RGB does not exist, return nearest value from list. My search function to is:

List<Colors> colors;
touchedRGB = bitmap.getPixel(x, y);
        for (int i = 0; i < colors.size(); i++) {
            Colors color=colors.get(i);
            if (touchedRGB==Color.rgb(color.getRcolor(),color.getGcolor(), color.getBcolor())) {
                Log.v("OK", "Color found at:"+i);
            }
        }

it only checks and returns if color exists not closest one. Here is code for list of colors:

    public class Colors {

public int rowid;
public int Rcolor;
public int Gcolor;
public int Bcolor;
public int RGB;
public int getRowid() {
    return rowid;
}

public void setRowid(int rowid) {
    this.rowid = rowid;
}
public int getRcolor() {
    return Rcolor;
}

public void setRcolor(int Rcolor) {
    this.Rcolor = Rcolor;
}

public int getGcolor() {
    return Gcolor;
}

public void setGcolor(int Gcolor) {
    this.Gcolor = Gcolor;
}

public int getBcolor() {
    return Bcolor;
}

public void setBcolor(int Bcolor) {
    this.Bcolor = Bcolor;
}

}

Log Cat:

06-02 04:40:59.911: V/Color(1539): RGB:-2064 R:255 G:247 B:240
06-02 04:40:59.921: V/Color(1539): RGB:-139303 R:253 G:223 B:217
06-02 04:40:59.981: V/Color(1539): RGB:-147017 R:253 G:193 B:183
06-02 04:41:00.031: V/Color(1539): RGB:-352102 R:250 G:160 B:154
06-02 04:41:00.121: V/Color(1539): RGB:-555139 R:247 G:135 B:125
06-02 04:41:00.171: V/Color(1539): RGB:-2136478 R:223 G:102 B:98
06-02 04:41:00.181: V/Color(1539): RGB:-3781045 R:198 G:78 B:75
06-02 04:41:00.221: V/Color(1539): RGB:-197923 R:252 G:250 B:221
06-02 04:41:00.361: V/Color(1539): RGB:-3403 R:255 G:242 B:181
like image 263
Ali Hassan Avatar asked Sep 03 '25 04:09

Ali Hassan


1 Answers

Create a "distance" function that compares 2 colors and gives you the distance. Equal colors will have a distance of 0.

There are many ways to implement the distance, based on what "closest color" means to you. The simplest would be to just use the sum of the RGB differences:

public int Distance(Color a, Color b) {
    return Math.Abs(a.r - b.r) + Math.Abs(a.g - b.g) + Math.Abs(a.b - b.b);
}

Or you could interpret RGB as a point in 3D space and get the geometric distance, which would look something like this:

public double Distance(Color a, Color b) {
    return Math.Sqrt(Math.Pow(a.r - b.r, 2) + Math.Pow(a.g - b.g, 2) + Math.Pow(a.b - b.b, 2));
}

This is usually good enough for most simple applications. If you want a more realistic comparison, you would probably translate the RGB color to something like HSV and compare mostly based on hue.

like image 156
CaptainCodeman Avatar answered Sep 04 '25 17:09

CaptainCodeman