Forum

RGB to HSV Texture

Hi,

i got a texture with RGB params, and my boss want me to transform this texture with HSV params. I’ve found a RGB->HSV function, that i’ve transposed in HLSL. The main problem is that doesn’t transform a RGB texture into a HSV one, but take rgb params and output corresponding HSV params. I think I must use the Transform 2d node, but I don’t know how to specifiy my transformation.
Hope i’ve been clear :)

Thanks

{CODE(ln=>1)}void RGBtoHSV(float r: In,float g: In,float b: In,float h: Out,float s: Out,float v: Out)
{
float min_, max_, delta_;

min_ = min( min(r, g), b );
max_ = max( max(r, g), b );
v = max_;				// v

delta_ = max_ - min_;

if( max_ != 0 )
	s = delta_ / max_;		// s
else {
	// r = g = b = 0		// s = 0, v is undefined
	s = 0;
	h = -1;
	return;
}

if( r == max_ )
	h = ( g - b ) / delta_;		// between yellow & magenta
else if( g == max_ )
	h = 2 + ( b - r ) / delta_;	// between cyan & yellow
else
	h = 4 + ( r - g ) / delta_;	// between magenta & cyan

h *= 60;				// degrees
if( h < 0 )
	h += 360;

}
^

the HSL (Transform) node creates a matrix which can manipulate parameters like hue, saturation and lightness on any shader which multiplies rgb values with this matrix.

Paul Haeberli provides the math in his seminal Matrix Operations for Image Processing article.

so you probably do not need to convert from tgb to hsl and back in the shader - you can do this with a simple matrix multiplication in the shader.

Hi and thanks oschatz,

in my .fx, i have something like

{CODE(ln=>1)}float4 psBnW(vs2ps In):Color
{
float4 outColor = { 0.0, 0.0, 0.0, 1.00000 };
//source texture lookup
float4 src01 = tex2D(Src01Samp, In.Src01Cd);
// the mask texture lookup
float4 src02 = tex2D(Src02Samp, In.Src02Cd);
float4 temp = {0.0,0.0,0.0,1.0};

temp.r = abs(src01.r-src02.r);
temp.g = abs(src01.g-src02.g);
temp.b = abs(src01.b-src02.b);

if(temp.r > 0 && temp.g > threshold && temp.b > 0) {
          temp.r = src02.r;
          temp.g = src02.g;
          temp.b = src02.b;
}
outColor = lerp(temp, src01, 1-Src02Alpha);
return outColor;

}^

and I have to remove temp.r, temp.g … and replace that by temp.h , temp.s and temp.v.

Actually, i’m using the “Blend” patch. Maybe have I not really understood what you told me, but I don’t know how to really use it in the code.

If a good soul … ;)