1

I have list of colors in HEX format (for example #000000) and I would like to detect color type (blue, red, green etc.) and then change color type to another color type. Is this possible and are there any frameworks/libraries for this task?

Example:

I have color #EB1369 (red) then I convert it to blue and it becomes for example #1313EB (blue).

2
  • How do you know that #eb1369 is red ? I mean I know that it is, but what definition or colorimetry concept do you want to use ? Commented Feb 22, 2012 at 12:07
  • If it looks red, then it is red to me:) But I have no idea how computers can detect color type, so that is why I'm asking about it... Commented Feb 22, 2012 at 12:10

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

Try convert RGB values to HSV (HSB exactly) - it is format for colors which is more comfortable for human. After conversion, all u need to do is change H V (probably) and convert it back to RGB.

1 Comment

He'd need to change the Hue (the color) not he Value (~= brightness). Also note that java's Color natively supports HSB but not HSV so the former might be easier to use.
0

I guess that you like to convert RGB color to HSB. YOu can do this wuth:

java.awt.Color.RGBtoHSB(...) 

then you can easily determine whetther H value fits in your definition of blue, and modify it to whatever you like. After this, you can easily convert it back to RGB via:

java.awt.Color.getHSBColor(...) 

And ifg you do not like jawa.awt.color just multiply color vector by transofrmation matrix.

Comments

0

Each HEX Color has three parts in it, red, green and blue the # identifies a HEX color, the following two letters are the amount of red; the next two are green and the next two are blue. i.e: RGB

The two letters can have a maximum hexidecimal value of FF which is 255, and a minimum of 00 which is zero.

So you can argue like this, I want a color with 2 red parts, 7 green parts, and zero blue parts, which will give you #020700

That is why #FFFFFF is white (all the colors together) and #000000 is black (no colors at all)

With this logic you can modify the color in any way you want; The Color class can also help a lot.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.