Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (6 posts)

Hue rotation problems

Last post 9/22/2018 6:12 AM by Halloween2018. 5 replies.
  • 6/19/2010 1:22 AM

    Hue rotation problems

    Hi everyone,

    I'm trying to build an HLSL pixel shader that will allow me to dynamically alter the hue, saturation and value of a sprite. I'm using a method described here:


    Basically, the idea is to convert to YIQ space, alter the hue, saturation and value of the pixel, and convert back to RGB. On a very coarse level, the shader I have seems to work -- if the value is set to zero, the pixel turns black, and if saturation is zero, it becomes grayscale. However, nothing in between seems to do anything. Furthermore, coarse hue rotations seem to work -- a 180 rotation of red will result in cyan, for example. However, I can't for the life of me get a smooth transition between the hues. Maybe 4 or 5 different hues will cycle, instead of getting a smooth transition.

    I'm not SUPER familiar with this kind of thing, so I'm wondering, is the problem with my implementation, or with the algorithm itself? Here's my code:

    1 sampler s; 
    2 float3 lightColor; 
    3 float rotAmount; 
    4 float rotOffset; 
    5 float value; 
    6  
    7 static const float PI = 3.14159265f; 
    8  
    9 float4 PixelShaderFunction(float2 uv : TEXCOORD0) : COLOR0 
    10
    11     // Initialize variables for hue rotation. 
    12     float4 color = tex2D(s, uv); 
    13     float saturation = 1; // Set arbitrarily for testing. 
    14      
    15     float v = value; 
    16     float VSU = v * saturation *  
    17                 cos(rotAmount * PI / 180.0f); 
    18     float VSW = v * saturation *  
    19                 sin(rotAmount * PI / 180.0f); 
    20      
    21     // Create the transformation matrix. 
    22     float3x3 hsvTransform = 
    23     {(0.299f*v)+(0.701f*VSU)+(0.168f*VSW), 
    24     (0.587f*v)-(0.587f*VSU)+(0.330f*VSW), 
    25     (0.114f*v)-(0.114f*VSU)-(0.497f*VSW), 
    26     (0.299f*v)-(0.299f*VSU)-(0.328f*VSW), 
    27     (0.587f*v)+(0.413f*VSU)+(0.035f*VSW), 
    28     (0.114f*v)-(0.114f*VSU)+(0.292f*VSW), 
    29     (0.299f*v)-(0.3f*VSU)+(1.25f*VSW), 
    30     (0.587f*v)-(0.588f*VSU)-(1.05f*VSW), 
    31     (0.114f*v)+(0.886f*VSU)-(0.203f*VSW)}; 
    32      
    33     color.rgb = mul(hsvTransform, lightColor); 
    34      
    35     return color; 
    36
    37  
    38 technique Technique1 
    39
    40     pass Pass1 
    41     { 
    42         PixelShader = compile ps_2_0 PixelShaderFunction(); 
    43     } 
    44

  • 6/25/2010 12:41 AM In reply to

    Re: Hue/Saturation/Value transform using YIQ space (HLSL)

    I think you mean the HSL/HSV colour space, since you have individual channels for hue, saturation, and luma/brightness. Check out this: http://mouaif.wordpress.com/2009/01/08/photoshop-math-with-hlsl-shaders/


    In YIQ you have 1 channel for luma and a 2d colour coordinate (also known as UV or XY in other colourspaces) that combines both hue and saturation.
  • 6/25/2010 9:02 AM In reply to

    Re: Hue/Saturation/Value transform using YIQ space (HLSL)

    Thanks for the response. I realize HSL/HSV color space is the simplest to work within, but I thought that performing a single transform using the YIQ method would be way simpler and more efficient. Did you take a look at the website I linked to in the original post? Does that method work and there's a problem with my implementation, or is the algorithm simply not accurate enough?
  • 7/21/2010 3:06 PM In reply to

    Still having problems with hue rotation matrix

    EDIT: Just went back into my code, removed some lines I had in to debug the code, and it works fine now! Very odd...
  • 9/22/2018 6:07 AM In reply to

    Re: Still having problems with hue rotation matrix

    We are glade to announce when is halloween
  • 9/22/2018 6:12 AM In reply to

    Re: Hue/Saturation/Value transform using YIQ space (HLSL)

    CRICKET WORLD CUP 2019 IS THE MEGA EVENT OF THE ICC. THE VENUE FOR THIS MEGA EVENT DECIDED ENGLAND AND WALES. THE ICC WORLD CUP WILL BE PLAYED FROM MAY 30 TO JULY 14, 2019
Page 1 of 1 (6 posts) Previous Discussion Next Discussion