HSV color setting

Here’s a HSV setColor i wrote for making gradients. here’s the code based on http://en.wikipedia.org/wiki/HSL-color-space.

  
  
void pSetHSV( float h, float s, float v ) {  
	// H [0, 360] S and V [0.0, 1.0].  
	int i = (int)floor(h/60.0f) % 6;  
	float f = h/60.0f - floor(h/60.0f);  
	float p = v * (float)(1 - s);  
	float q = v * (float)(1 - s * f);  
	float t = v * (float)(1 - (1 - f) * s);  
	  
	switch (i) {	  
		case 0: glColor3f(v, t, p);  
		break;  
		case 1: glColor3f(q, v, p);  
		break;  
		case 2: glColor3f(p, v, t);  
		break;  
		case 3: glColor3f(p, q, v);  
		break;  
		case 4: glColor3f(t, p, v);  
		break;  
		case 5: glColor3f(v, p, q);  
	}  
}   

[/code]

sorry to dig up an old thread but i was ecstatic to find this! here’s a version that includes alpha:

  
  
void pSetHSV( float h, float s, float v, float a ) {  
   // H [0, 360] S, V and A [0.0, 1.0].  
   int i = (int)floor(h/60.0f) % 6;  
   float f = h/60.0f - floor(h/60.0f);  
   float p = v * (float)(1 - s);  
   float q = v * (float)(1 - s * f);  
   float t = v * (float)(1 - (1 - f) * s);  
     
   switch (i) {     
      case 0: glColor4f(v, t, p, a);  
      break;  
      case 1: glColor4f(q, v, p, a);  
      break;  
      case 2: glColor4f(p, v, t, a);  
      break;  
      case 3: glColor4f(p, q, v, a);  
      break;  
      case 4: glColor4f(t, p, v, a);  
      break;  
      case 5: glColor4f(v, p, q, a);  
   }  
}   

1 Like