Skip to content

Instantly share code, notes, and snippets.

@smakhtin
Last active October 15, 2021 07:39
Show Gist options
  • Save smakhtin/6417335 to your computer and use it in GitHub Desktop.
Save smakhtin/6417335 to your computer and use it in GitHub Desktop.
Found good way to convert from and to HSV in HLSL. Source - http://ploobs.com.br/?p=1499.
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment