Last active
August 29, 2015 14:21
-
-
Save paraself/b8f1ffa282d0d19bf538 to your computer and use it in GitHub Desktop.
Fast RGBA to HSVA in Unity3D shader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Original source | |
//http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv | |
//http://ploobs.com.br/?p=1499 | |
#ifndef RGBA2HSVA | |
#define RGBA2HSVA | |
fixed4 RGBA2HSVA(fixed4 rgba) | |
{ | |
float K = 0; | |
fixed temp; | |
if (rgba.g < rgba.b) | |
{ | |
temp = rgba.b; | |
rgba.b = rgba.a; | |
rgba.a = temp; | |
K = -1; | |
} | |
if (r < g) | |
{ | |
temp = rgba.r; | |
rgba.r = rgba.g; | |
rgba.g = temp; | |
K = -2 / 6 - K; | |
} | |
fixed chroma = r - min(g, b); | |
h = abs(K + (g - b) / (6 * chroma + 1e-20)); | |
s = chroma / (r + 1e-20); | |
v = r; | |
return float4(h,s,v,rgba.a); | |
} | |
fixed3 Hue(fixed H) | |
{ | |
fixed R = abs(H * 6 - 3) - 1; | |
fixed G = 2 - abs(H * 6 - 2); | |
fixed B = 2 - abs(H * 6 - 4); | |
return saturate(fixed3(R,G,B)); | |
} | |
fixed4 HSVtoRGB(fixed4 HSV) | |
{ | |
return fixed4(((Hue(HSV.x) - 1) * HSV.y + 1) * HSV.z,HSV.w); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment