Question

I want to use the algorithm from converting an HSV color value into RGB color to compute a 200 x 200 RGB bitmap with the colors red, green, blue, and white in its corners and bilinearly interpolated HSV colors elsewhere and compute also the bitmap with bilinearly interpolated RGB colors. I have found the formula in the wikipedia but I am confused on how to do this.

Any help would be appreciated.

Was it helpful?

Solution

OK, I think I know what you are getting at. I haven't tried to run any of this, so it probably has some bugs...

First you need to work out the HSV values for red, green, blue and white. Call these, clockwise, a,b,c,d - for example, white would be [0,0,1] or something scaled like that

For a position in the grid, (x,y) with 0 <= x <= 1 The interpolation bit is something like, putting the values into the array out:

for(int i=0; i<3; i++){
  out[i] = y*((x*a[i]) + ((1-x)*b[i])) + (1-y)*((x*d[i]) + ((1-x)*c[i]));
}

As finding the linearly interpolated value the a fraction of x between A and B is given by x*A + (1-x)*B. Just do it once for each direction.

Then just convert them to RGB, using the convention from the wikipedia article

void HSVtoRGB(double H, double S, double V, double[] out){
   double C = S*V;
   double H_prime = H/60; // a number in [0,3]
   double X = C*(1 - abs((H_prime%2)-1));

   // Do the big if bit
   switch((int)X){
     case 0:
       out[0] = C;
       out[1] = X;
       out[2] = 0;
     case 1:
       out[0] = X;
       out[1] = C;
       out[2] = 0;

      // etc etc

  }

  double m = V - C;
  for(int i=0; i<3; i++){
    out[i] += m;
  }
}

That should do it, give or take. Well, should give you a rough idea at least.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top