I only care about 12 colors:
red: RGB: 255, 0, 0
pink: RGB: 255, 192, 203
violet: RGB: 36, 10, 64
blue: RGB: 0, 0, 255
green: RGB: 0, 255, 0
yellow: RGB: 255, 255, 0
orange: RGB: 255, 104, 31
white: RGB: 255, 255, 255
black: RGB: 0, 0, 0
gray: RGB: 128, 128, 128
tea: RGB: 193, 186, 176
cream: RGB: 255, 253, 208
When i read the pixel of bitmap, i can get the Hue value:
int picw = mBitmap.getWidth();
int pich = mBitmap.getHeight();
int[] pix = new int[picw * pich];
float[] HSV = new float[3];
// get pixel array from source
mBitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
int index = 0;
// iteration through pixels
for(int y = 0; y < pich; ++y) {
for(int x = 0; x < picw; ++x) {
// get current index in 2D-matrix
index = y * picw + x;
// convert to HSV
Color.colorToHSV(pix[index], HSV);
// increase Saturation level
//HSV[0] = Hue
Log.i(getCallingPackage(), String.valueOf(HSV[0]));
}
}
Now i want to know what color of this pixel (only in 12 above colors)?
I use HSV to see the range of color. When i have a color which doesn't in this list, i want to name it as similarly color in my list How can i do it?
Thanks you so much
Based on your comments it seems you're basically trying to reduce the bitmap's full colour palette to only the 12 you specified. Obviously for every pixel in the bitmap the 'best match' from those 12 should be picked.
I still don't see why you would need the HSV values, as it's just a different representation of the RGB components - it doesn't actually change the problem, or its solution.
A straightforward approach to find the best match for any RGB colour would look something like as follows.
First build some sort of a list containing the colours you want to match against. I've used a Map, since you mentioned you (also) wanted to know the name of the colour, not just the RGB value.
Then just make a method that will tell you the best match. You can call this from within your second for loop and pass it the current pixel colour. I've added some inline comments to explain the different steps, but it's really quite trivial.
The results for a quick test using some of the predefined Color constants:
The first number inbetween the brackets is the actual int value for the Color constant, the second is the int value for the best match found, with the name right in front of it.
The result for
Color.MAGENTA
also illustrates why you should not just compare the colour's int value directly. The actual int value is-65281
, which is quite close to the value forColor.RED
(-65536). However, the best match based on the different components is 'pink', which has a -16181 value. Obviously this makes complete sense knowing that a colour is defined as 4 bytes:Source: android.graphics.Color reference.
// Edit: with HSV values it seems to work fine too. I did get a different result for 'magenta' as closest match though - violet, in stead of pink. You might want to double check the values and breakpoint some stuff. For instance, I can imagine it might be better to normalize the 'H' part. That's up to you...
Since you already have pixel color value in int. You can extract the RGB value using following methods
And then compare with the RGB values you have