Here's a function that will let you shift colors around the hue circle. You should read the wikipedia page on the HSB (or HSV) color system to really understand what is going on: http://en.wikipedia.org/wiki/HSV_color_space
/** Converts an input color given as a String such as "ab451e" to * the HSB color space. Shifts its hue from the given angle in degrees. * Then returns the new color in the same format it was given. * * For example shift("ff0000", 180); returns "80ff00" (green is the opposite of red).*/ public static String shift(String rgbS, int angle) { // Convert String to integer value int value = Integer.parseInt(rgbS, 16); // Separate red green and blue int r = value >> 16; int g = (value >> 8) & 0xff; int b = value & 0xff; // Convert to hsb float[] hsb = Color.RGBtoHSB(r, g, b, null); // Convert angle to floating point between 0 and 1.0 float angleF = (float)(angle/360.0); // Shift the hue using the angle. float newAngle = hsb[0] + angleF; if(newAngle > 1.0) newAngle = newAngle - 1.0f; hsb[0] = newAngle; // Convert back to RGB, removing the alpha component int rgb = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]); rgb = rgb & 0xffffff; // Build a new String return Integer.toHexString(rgb); }
Detecting colors can be complex, it depends on the result you really expect.
If what you want is simply an approximation (red, green, blue, yellow, etc.) then you can look at the hue circle of the HSB color-space, choose a hue value for each color you want to define, and then map the color you get in input to the closest one you chose.
You can also rely on things like named HTML colors: http://www.w3schools.com/html/html_colornames.asp . Take this list, create a mapping in your program, then all you have to do is map the color you get to the closest one in your map, and return its name. Be wary though: computing the distance between two colors can be tricky (especially in RGB) and naive approaches (such as channel-by-channel difference) can give surprisingly bad results. Colorimetry is a complex topic, and you will find good methods on this page: http://en.wikipedia.org/wiki/Color_difference