Optimised Hue shift function in GLSL
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
vec3 hueShift( vec3 color, float hueAdjust ){ | |
const vec3 kRGBToYPrime = vec3 (0.299, 0.587, 0.114); | |
const vec3 kRGBToI = vec3 (0.596, -0.275, -0.321); | |
const vec3 kRGBToQ = vec3 (0.212, -0.523, 0.311); | |
const vec3 kYIQToR = vec3 (1.0, 0.956, 0.621); | |
const vec3 kYIQToG = vec3 (1.0, -0.272, -0.647); | |
const vec3 kYIQToB = vec3 (1.0, -1.107, 1.704); | |
float YPrime = dot (color, kRGBToYPrime); | |
float I = dot (color, kRGBToI); | |
float Q = dot (color, kRGBToQ); | |
float hue = atan (Q, I); | |
float chroma = sqrt (I * I + Q * Q); | |
hue += hueAdjust; | |
Q = chroma * sin (hue); | |
I = chroma * cos (hue); | |
vec3 yIQ = vec3 (YPrime, I, Q); | |
return vec3( dot (yIQ, kYIQToR), dot (yIQ, kYIQToG), dot (yIQ, kYIQToB) ); | |
} |
@l375cd It doesn't do anything with alpha channel, so just use GLSL swizzling to only pass in the RGB channels, and then re-apply the alpha channel to the result. Something like the pseudo-code below.
in vec2 texCoord;
out vec4 outFrag;
uniform sampler2D image;
uniform float hue
void main()
vec4 color = texture(image, texCoord);
outFrag = vec4(hueShift(color.rgb, hue), color.a);
}
@ForeverZer0, thank you so much!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
vmedea, thank you for the code.
How to adapt your code fro RGBA (alpha) image, vec4?