HLSL Pixel Shader - Change Image Color For Specific Hue

Mark P Neyer picture Mark P Neyer · Sep 8, 2009 · Viewed 8.7k times · Source

I'd like to write a pixel shader that takes an input image, and converts all of the colors of one Hue range (i.e. HSV) into another Hue Range.

My motivation is simple: I want to color a bunch of different textures differently, but i don't want to color the entire texture, just the portion with a hue in a specific range. That way, I can draw one image of a racing car, and then change the color of just the stripes and the logo on the car with a pixel shader.

I looked at the HLSL documentation online and couldn't find anything to deal with hues. Is there a library of HLSL code available online?

Here's some pseudocode for what i'm trying to accomplish:

external float SrcMinHue,SrcMaxHue,TargetMin
void changeHues(image source)
{
   foreach x,y in image:
   {
      float sourceHue = getHue(source,x,y)
      if (SrcMinHue < sourceHue < SrcNaxHue):
          setHue(source,x,y,(TargetMin + (sourceHue - MinHue))
   }
}

I'm working with XNA Game Studio, if that information matters to anyone.

Answer

Tchami picture Tchami · Sep 8, 2009

Take a look at the "post RGB to HSV" sample over at the NVidia Shader Library page. That might give you some inspiration.

Otherwise I guess you could "simply" convert an RGB color to HSV in your pixel shader using the formula from Wikipedia and then take it from there.