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;
            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ür-unternehmen.html on 16 de janeiro de 2017 - 4:58 pm

    Boy that really helps me the heck out.

  2. #2 by patagonia on sale on 16 de janeiro de 2017 - 5:04 pm

    Damn, awesome website. I actually came across this on Bing, and I am happy I did. I will definately be coming back here more often. Wish I could add to the posts here and bring a bit more to the table, but am just absorbing as much info as I can at the moment.
    patagonia on sale

  3. #3 by 5000 euro kredit ohne ksv eintrag on 16 de janeiro de 2017 - 5:38 pm

    This is both street smart and intelligent.

  4. #4 byändern.html on 16 de janeiro de 2017 - 5:46 pm

    Ich frage mich warum das Recht und Rechtsprecheung in D im Bezug und rund ums deutschen liebsten Kind dem Auto so eindeutig ausgeprägt und halterfreudlich ist wo hingegen das im übertragenen Sinne im Internetrecht fast immer auf eine Störerhaftung und Auskunftsanspruch gegenüber Rechteinhabern hinausläuft.Brabby

  5. #5 by Car Insurance Quotes Online on 16 de janeiro de 2017 - 7:35 pm

    Matter is, We have just migrated to WordPress. However , I have a number of people that have subscribed to my passes from my blogger blog. What must i do to make sure that they now receive improvements from my WordPress blog?.. FYI, I have a feedburner accounts..

  6. #6 by herve leger bandage dress knock off on 16 de janeiro de 2017 - 7:49 pm

    oh cool, this information is really useful and definately is comment worthy! hehe. I’ll see if I can try to use some of this information for my own blog. Thanks!
    herve leger bandage dress knock off

  7. #7 by insurance company consumer ratings on 16 de janeiro de 2017 - 7:51 pm

    My programmer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using WordPress on a variety of websites for about a year and am concerned about switching to another platform. I have heard excellent things about Is there a way I can transfer all my wordpress content into it? Any kind of help would be really appreciated!

  8. #8 by on 16 de janeiro de 2017 - 8:38 pm

    Now that’s subtle! Great to hear from you.

  9. #10 by christian louboutin shoes on 16 de janeiro de 2017 - 10:16 pm

    Youre so cool! I dont suppose Ive learn anything like this before. So nice to seek out somebody with some original ideas on this subject. realy thanks for starting this up. this web site is one thing that’s needed on the internet, someone with a little originality. helpful job for bringing one thing new to the internet!

  10. #11 by Car Insurance Quotes Online on 16 de janeiro de 2017 - 10:21 pm

    I bought a whole hp computer over a month ago. Is certainly turning this off every evening bad for the pc or should the computer become left on 24-7. I possess heard both ways. Any kind of computer technicians answers will be appreciated..

  11. #12 by balmain silver jeans on 16 de janeiro de 2017 - 10:50 pm

    There are usually not many websites with info like this man! Bookmarked!
    balmain silver jeans

  12. #14 by michael kors outlet online official on 16 de janeiro de 2017 - 11:05 pm

    I love that website layout . How was it made. Its very nice.
    michael kors outlet online official

  13. #15 by on 16 de janeiro de 2017 - 11:06 pm

    can you buy amoxicillin over the counter in usa amoxicillin clavulanic ta 500 125mg can you buy amoxicillin over counter 24 tablets of amoxicillin 250 mg mode of action of amoxicillin and potassium clavulanate use of amoxicillin and clavulanate potassium tablets can you buy amoxicillin walmart amoxicillin 500 mg 3 times a day for 7 days

  14. #16 by android games for pc on 17 de janeiro de 2017 - 12:00 am

    Great post. I am facing a couple of these problems.

  15. #17 by android apps for pc on 17 de janeiro de 2017 - 12:24 am

    Great work! That is the type of info that are supposed to be shared across the web. Shame on Google for not positioning this publish higher! Come on over and talk over with my site . Thanks =)

  16. #18 by berechnung vorzeitige abloesung ratenkredit günstig on 17 de janeiro de 2017 - 12:35 am

    This is a wonderful post. We teachers are trying to help children understand that it is an author's personal take on a topic that makes writing so original and compelling. Thank you for the inside scoop.

  17. #19 by kebronOn on 17 de janeiro de 2017 - 12:37 am


  18. #20 byünchen.html on 17 de janeiro de 2017 - 1:44 am

    Cool! That’s a clever way of looking at it!

  19. #21 by patagonia online sale on 17 de janeiro de 2017 - 1:52 am

    Wonderful story. I was checking continuously to this weblog and I am really impressed! Extremely educational information, especially the 4th sentences. I really want this kind of info. I used to be seeking this particular knowledge for weeks. Thanks and good luck.
    patagonia online sale

  20. #22 by patagonia coupons online on 17 de janeiro de 2017 - 2:05 am

    This is some agreeable material. It took me a while to locate this web site but it was worth the time. I noticed this post was buried in yahoo and not the first spot. This weblog has a ton of good quality material and it doesnt deserve to be burried in the search engines like that. By the way I am going to save this internet site to my favorites.
    patagonia coupons online

  21. #23 by balmain jeans biker on 17 de janeiro de 2017 - 2:21 am

    I have been examinating out many of your articles and i can state nice stuff. I will definitely bookmark your blog.
    balmain jeans biker

  22. #24 by on 17 de janeiro de 2017 - 3:00 am

    Hehe, nice to see someone that owns more Safaris than me! Heh. Thanks for the ink recommendation, it’s definitely cheaper than the Caran D’Ache and I’ve added it to my Amazon basket to try

  23. #25 by vibrator review on 17 de janeiro de 2017 - 3:07 am

    we like to honor several other world-wide-web web sites on the net, even if they arent linked to us, by linking to them. Below are some webpages worth checking out

  24. #27 by on 17 de janeiro de 2017 - 4:15 am

    Yeah, I’d like to test one out too. Not sure I’m ready to leave my iPhone, but if I were thinking about it, this would probably be a real contender. The reviews thus far have been superlative.-systemsboy

  25. #28 by samoletni bileti do viena on 17 de janeiro de 2017 - 4:41 am

    Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.

  26. #29 by kredit counseling center ärzte on 17 de janeiro de 2017 - 4:45 am

    Hearty applause from the sidelines for the new bug reporting functionality – great! :-DThanks for taking a big, progressive step to better identify user issues.We look forward to it enabling better outcomes for us all.

1 291 292 293
(não será publicado)