Convert Color Spaces (HSL->RGB)

Math

{h}\in[0,360) 

{s}\in[0,1] 

{l}\in[0,1] 

 {q}= \begin{cases}     {{l}\times({1}+{s})}, & if\quad{l}<\frac{1}{2} \\     {{l}+{s}-({l}\times{s})}, & if\quad{l}\ge\frac{1}{2} \end{cases} 

{2}\times{l}-{q} 

h_k=\frac{h}{360} 

t_R=(h_k+\frac{1}{3})\mod{1} 

t_G=(h_k)\mod{1} 

t_B=(h_k-\frac{1}{3})\mod{1} 

foreach\quad{t_C}\in\{{t_R}, {t_G}, {t_B}\} 
 {t_C}= \begin{cases}     {{p}+(({q}-{p})\times{6}\times{t_C}}, & if\quad{t_C <\frac{1}{6}} \\     {q}, & if\quad{\frac{1}{6}\le{t_C}<\frac{1}{2}} \\     {{p}+(({q}-{p})\times{6}\times{\frac{2}{3}-t_C}}, & if\quad{\frac{1}{2}\le{t_C}<\frac{2}{3}} \\     {p}, & otherwise \end{cases} 

{{t_R}\equiv{r}}\in[0,1] 

{{t_G}\equiv{g}}\in[0,1] 

{{t_B}\equiv{b}}\in[0,1] 

C/C++

pixeltype HSLtoRGB(pixeltype palette_color){
	//0.0f <= h <  360.0f
	//0.0f <= s <= 1.0f
	//0.0f <= l <= 1.0f
	double h = (double)(HUE_OF(palette_color));
    double s = (double)(SATURATION_OF(palette_color));
    double l = (double)(LIGHTNESS_OF(palette_color));
	double q, p, h_k;
	double rgb[3];

	if(l < 0.5f){
		q = l * (1 + s);
	}else{
		q = l + s - (l * s);
	}
	p = 2 * l - q;
	h_k = h / 360.0f;
	rgb[0] = h_k + (1 / 3);
	rgb[1] = h_k;
	rgb[2] = h_k - (1 / 3);
	for(size_t i = 0; i < 3; i++){
		if(rgb[i] < (1 / 6)){
			rgb[i] = p + ((q - p) * 6.0f * rgb[i]);
		}else if(rgb[i] >= (1 / 6) && rgb[i] < (1 / 2)){
			rgb[i] = q;
		}else if(rgb[i] >= (1 / 2) && rgb[i] < (2 / 3)){
			rgb[i] = p + ((q - p) * 6.0f * ((2 / 3) - rgb[i]));
		}else{
			rgb[i] = p;
		}
	}
	//0.0f <= rgb[0] <= 1.0f
	//0.0f <= rgb[1] <= 1.0f
	//0.0f <= rgb[2] <= 1.0f
	return RGB_OF(rgb[0], rgb[1], rgb[2]);
}

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

) Your Reply...

Comment moderation is enabled. Your comment may take some time to appear.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

GameProgrammerArt is Digg proof thanks to caching by WP Super Cache