Page 1 of 1

HSV

Posted: Wed Jan 20, 2010 6:31 pm
by Kjell
8)

HSV to RGB converter. Hue uses 0 to 360 ( but automatically wraps around when you go outside that range ), while Saturation and Value go to 100.

Uses a 1D ( SizeDim1=3 ) Array named Color to return the values to.

Code: Select all

float angle(float X)
{
  if(X >= 0 && X < 360)return X;
  if(X > 360)return X-floor(X/360)* 360;
  if(X <   0)return X+floor(X/360)*-360;
}

void hsv(float H, float S, float V)
{
  float R,G,B,I,F,P,Q,T;
  
  H = angle(H);
  S = clamp(S,0,100);
  V = clamp(V,0,100);

  H /= 60;
  S /= 100;
  V /= 100;
  
  if(S == 0)
  {
    Color[0] = V;
    Color[1] = V;
    Color[2] = V;
    return;
  }

  I = floor(H);
  F = H-I;

  P = V*(1-S);
  Q = V*(1-S*F);
  T = V*(1-S*(1-F));

  if(I == 0){R = V; G = T; B = P;}
  if(I == 1){R = Q; G = V; B = P;}
  if(I == 2){R = P; G = V; B = T;}
  if(I == 3){R = P; G = Q; B = V;}
  if(I == 4){R = T; G = P; B = V;}
  if(I == 5){R = V; G = P; B = Q;}
  
  Color[0] = R;
  Color[1] = G;
  Color[2] = B;
}
Attached is a simple example.

K

Posted: Wed Jan 20, 2010 7:12 pm
by diki
cool, thanks for sharing!

Posted: Thu Jan 21, 2010 1:42 pm
by Kjell
:oops:

Angle should have been ..

Code: Select all

float angle(float X)
{
  if(X >= 0 && X < 360)return X;
  if(X > 360)return X-floor(X/360)* 360;
  if(X <   0)return X+floor(X/360)*-360;
}
.. first post and attachment updated.

K

Posted: Thu Jan 21, 2010 2:55 pm
by jph_wacheski
Ah, very nice! I like this system much better in most situations, although I have gotten rather handy with the RGB setup,. this will still be very usefull for sure. not only the ability to wrap color cycles but for finding colors of the same levels,. Thanks Kjell.