Convert Color Spaces (RGB->HSL)

Math

{r}\in[0,1] 
{g}\in[0,1] 
{b}\in[0,1] 
max=\max(r,g,b) 
min=\min(r,g,b) 

 h = \begin{cases}     {0}, & if {max}={min} \\     ({60}\times\frac{{g}-{b}}{{max}-{min}}+{360})\mod{360}, & if {max}={r} \\     ({60}\times\frac{{b}-{r}}{{max}-{min}}+{120}), & if {max}={g} \\     ({60}\times\frac{{r}-{g}}{{max}-{min}}+{240}), & if {max}-{b} \end{cases} 

\frac{1}{2}({{max}+{min}}) 

 s = \begin{cases}     0, & if {max}={min} \\     \frac{{max}-{min}}{2l}, & if l\le{\frac{1}{2}} \\     \frac{{max}-{min}}{{2}-2l}, & if l>{\frac{1}{2}} \end{cases} 

C/C++

pixeltype RGBtoHSV(pixeltype palette_color){
	//0.0f <= r <= 1.0f
	//0.0f <= g <= 1.0f
	//0.0f <= b <= 1.0f
    double r = (double)(RED_OF(palette_color));
    double g = (double)(GREEN_OF(palette_color));
    double b = (double)(BLUE_OF(palette_color));
    double max, min, h, s, l;

	if(r > g){
		max = r;
	}else{
		max = g;
	}
	if(b > max){
		max = b;
	}

	if(r < g){
		min = r;
	}else{
		min = g;
	}
	if(b < min){
		min = b;
	}

    if(max == min){
        h = 0.0f;
    }else if(max == r){
        h = ((60.0f * ((g - b) / (max - min)) + 360) % 360.0f);
    }else if(max == g){
        h = ((60.0f * ((b - r) / (max - min)) + 120));
    }else if(max == b){
        h = ((60.0f * ((r - g) / (max - min)) + 240));
    }

	l = (max + min) / 2.0f;

	if(max == min){
		s = 0;
	}else if(l <= 0.5f){
		s = ((max - min) / (2 * l));
	}else if(l > 0.5f){
		s = ((max - min) / (2 - (2 * l)));
	}

	//0.0f <= h <= 360.0f
	//0.0f <= s <= 1.0f
	//0.0f <= l <= 1.0f
    return HSL_OF(h, s, l);
}
}

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