Code Snippet: HSV to RGB and Vice versa in Shader


This Code snipped will show two custom operation that we normally need to use in our post effect shaders. They are the conversion between the RGB space to HSV and vice versa.

The shader is pretty simple and is shown below (this code is used inside a post effect, you can grab the HSVtoRBG and the RGBtoHSV functions and use at your own):

float2 halfPixel;
float4 toAdd;
float4 toMultiply;

texture cena;
sampler cenaSampler = sampler_state
{
   Texture = ;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   MipFilter = LINEAR;
   AddressU  = Clamp;
   AddressV  = Clamp;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
    float2 Pos : TEXCOORD1;
};
VertexShaderOutput VShader( float4 Pos: POSITION, float2 Tex : TEXCOORD)
{
	VertexShaderOutput output;
    output.Position = float4(Pos);
    output.Pos = float4(Pos);
    output.TexCoord = Tex - halfPixel;
    return output;
}

float3 Hue(float H)
{
    float R = abs(H * 6 - 3) - 1;
    float G = 2 - abs(H * 6 - 2);
    float B = 2 - abs(H * 6 - 4);
    return saturate(float3(R,G,B));
}

float4 HSVtoRGB(in float3 HSV)
{
    return float4(((Hue(HSV.x) - 1) * HSV.y + 1) * HSV.z,1);
}

float4 RGBtoHSV(in float3 RGB)
{
    float3 HSV = 0;
    HSV.z = max(RGB.r, max(RGB.g, RGB.b));
    float M = min(RGB.r, min(RGB.g, RGB.b));
    float C = HSV.z - M;
    if (C != 0)
    {
        HSV.y = C / HSV.z;
        float3 Delta = (HSV.z - RGB) / C;
        Delta.rgb -= Delta.brg;
        Delta.rg += float2(2,4);
        if (RGB.r >= HSV.z)
            HSV.x = Delta.b;
        else if (RGB.g >= HSV.z)
            HSV.x = Delta.r;
        else
            HSV.x = Delta.g;
        HSV.x = frac(HSV.x / 6);
    }
    return float4(HSV,1);
}

float4 PShader(VertexShaderOutput input) : COLOR
{
    float4 cen = tex2D(cenaSampler ,input.TexCoord );
    return HSVtoRGB(toMultiply * RGBtoHSV(cen) + toAdd);

}

technique Normal
{
	pass P0
	{
		VertexShader = compile vs_3_0 VShader();
		PixelShader = compile ps_3_0 PShader();
	}
}

, ,

  1. #1 by http://morarka.com/web/303.html on 31 de agosto de 2016 - 2:06 pm

    I have your present for my mom to Christmas mainly because she was per precious jewelry freak. Your something she cannot put on a lot concerning is, bracelets. I purchased her this allure bracelet to after she launched things yesterday she absolutley enjoyed that! Today the problem is actually, her acquiring it regarding additionally off of with by herself. Haha… overall ideal item, it delivered to appeared massively very early as well as my mother is enjoying it. Thank we.

  2. #2 by http://sanjumemorialtrust.org/js/lib/339.html on 31 de agosto de 2016 - 2:20 pm

    Recieved your item before the date given in a very kind present box . Some sort of picture that’s displayed for bracelet is exactly what you’ll recieve. It is really sensitive but your perfect part of precious jewelry which will last simply because iof the particular toggle clasp and is really durable so that the bracelet eill maybe not come off.

  3. #3 by real Samurai swords on 31 de agosto de 2016 - 2:21 pm

    A++++ shipper!

  4. #4 by http://www.yondu.cn/link/re/554.html on 31 de agosto de 2016 - 2:21 pm

    It is an awesome gifts for the on your loved ones. I would highly incourage individuals to get this one that it makes a awesome present. The particular sender will outstanding day duty at buying that stuff at time. I bought our items a day back and I reveived it certainly quickly.

  5. #5 by Android Games on 31 de agosto de 2016 - 4:01 pm

    I have always been planning to move to Australia completely. Hopefully, my immigration procedure will end up being completed this season and I actually will move around in the beginning of 2011. Can someone suggest some excellent assets and sites about producing the programs for living in Australia?.

  6. #6 by best katana on 31 de agosto de 2016 - 4:59 pm

    recommend seller! Thanks for fast shipping & delivery!

  7. #7 by katana Sword on 31 de agosto de 2016 - 5:15 pm

    great seller. . . great communication, quick delivery

  8. #8 by SEO in Pakistan on 31 de agosto de 2016 - 5:34 pm

    There are some fascinating deadlines in this article however I don’t know if I see all of them heart to heart. There may be some validity however I will take hold opinion until I look into it further. Good article , thanks and we want extra! Added to FeedBurner as well

  9. #9 by SEO training in Lahore on 31 de agosto de 2016 - 6:13 pm

    F*ckin’ remarkable issues here. I am very glad to see your article. Thank you so much and i am looking forward to contact you. Will you please drop me a e-mail?

  10. #10 by handmade swords on 31 de agosto de 2016 - 6:18 pm

    Very nice item.Fast shipping.Thanks

  11. #11 by Play games online on 31 de agosto de 2016 - 6:18 pm

    please go to the internet sites we stick to, which includes this one particular, because it represents our picks in the web

  12. #14 by the samurai on 31 de agosto de 2016 - 8:23 pm

    Just as described. Awsome seller

  13. #15 by this content on 31 de agosto de 2016 - 9:01 pm

    obviously like your website however you have to take a look at the spelling on several of your posts. A number of them are rife with spelling issues and I in finding it very troublesome to tell the reality nevertheless I’ll definitely come again again.

  14. #16 by important link on 31 de agosto de 2016 - 9:40 pm

    Yay google is my world beater aided me to find this outstanding website ! .

  15. #17 by katana samurai sword on 31 de agosto de 2016 - 10:17 pm

    Wow! Fast shipping and over a holiday!

  16. #18 by Folded Steel Japanese Swords on 31 de agosto de 2016 - 10:22 pm

    A+++ Thanks!!!

1 138 139 140
(não será publicado)