HSV

Share your ZGE-development tips and techniques here!

Moderator: Moderators

Post Reply
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

HSV

Post 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
Attachments
HSV.zgeproj
(1.54 KiB) Downloaded 715 times
Last edited by Kjell on Thu Jan 21, 2010 1:41 pm, edited 2 times in total.
User avatar
diki
Posts: 140
Joined: Thu Sep 11, 2008 7:53 pm
Location: GMT+1
Contact:

Post by diki »

cool, thanks for sharing!
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Post 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
User avatar
jph_wacheski
Posts: 1005
Joined: Sat Feb 16, 2008 8:10 pm
Location: Canada
Contact:

Post 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.
iterationGAMES.com
Post Reply