Skip to content

Instantly share code, notes, and snippets.

@spajus
Created November 22, 2023 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spajus/5240b358b103e2970ff3403202c56c74 to your computer and use it in GitHub Desktop.
Save spajus/5240b358b103e2970ff3403202c56c74 to your computer and use it in GitHub Desktop.
float3 RGBtoHSV(float3 In)
{
float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
float4 P = lerp(float4(In.bg, K.wz), float4(In.gb, K.xy), step(In.b, In.g));
float4 Q = lerp(float4(P.xyw, In.r), float4(In.r, P.yzx), step(P.x, In.r));
float D = Q.x - min(Q.w, Q.y);
float E = 1e-10;
return float3(abs(Q.z + (Q.w - Q.y)/(6.0 * D + E)), D / (Q.x + E), Q.x);
}
float3 HSVtoRGB(float3 In)
{
float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 P = abs(frac(In.xxx + K.xyz) * 6.0 - K.www);
return In.z * lerp(K.xxx, saturate(P - K.xxx), In.y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment