views:

364

answers:

2
+1  Q: 

Find Colour Range

I want to find a range of colours from the value of one RGB value

If I was given rgb(0,100,200) for example it would give me everything between rgb(0,0,255) and rgb(0,255,255). However not rgb(255,0,255).

Similarly rgb(150,50,0). Return: rgb(255,0,0) and rgb(255,255,0). Not rgb(255,0,255).

Making sense?

Im using PHP

A: 

Hm, not sure I understand this correctly, but I think you are saying that one of the numbers is higher than the other two, and one of the values is always zero. If this is the case, you should be able to use a simple if-else statement similar to

if (r > g && r > b) {
  if (g > 0) {
    color1 = rgb(255, 0, 0);
    color2 = rgb(255, 255, 0);
  }
  else {
    color1 = rgb(255, 0, 0);
    color2 = rgb(255, 0, 255);
  }
}
else if (r < g && g > b) {
   .
   .
   .
}

Hope that will help you out with your problem.

a_m0d
+3  A: 

The algorithm you explain is basically: "A color consists of two RGB components. Let the strongest RGB component be S and the other component O. Create 255 variations where you let S be (255) and O ranges from 0 to 255."

E.g. all of the below example yield the same result:

a) rgb(0,100,200) 
b) rgb(0,199,200)
c) rgb(0,254,255)
d) rgb(0,1,2)

Result: rgb(0,[0-255],255)

This means you only have a 6 variations.

  1. Red is the largest component, Green next largest => rgb(255,[0-255],0)
  2. Red is the largest component, Blue next largest => rgb(255,0,[0-255])
  3. Green is the largest component, Red next largest => rgb([0-255],255,0)
  4. Green is the largest component, Blue next largest => rgb([0-255],0,255)
  5. Blue is the largest component, Red next largest => rgb([0-255],0,255)
  6. Blue is the largest component, Green next largest => rgb(0,[0-255],255)

The intention of your algorithm is not clear, so I am guessing your use case is actually different from what you explain. It does not handle colors with either 1 or 3 components (most colors actually).

If your goal is to find similar colors (e.g. a color in a certain distance) there are better approaches. For example you could convert your colors to a HSV color space (Hue, Saturation, Value) and then say that the color is similar if the either of the components H, S or V are +/- 10 steps from your original.

E.g.:

rgb(255,100,0) => hsv(24,100,100)

Your range is then hsv([14-34],[90-110],[90-110])

danglund