Last active
January 21, 2025 19:10
RGB / CMYK conversion in GLSL
This file contains hidden or 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
| // From the book: Graphics Shaders | |
| vec3 CMYKtoRGB (vec4 cmyk) { | |
| float c = cmyk.x; | |
| float m = cmyk.y; | |
| float y = cmyk.z; | |
| float k = cmyk.w; | |
| float invK = 1.0 - k; | |
| float r = 1.0 - min(1.0, c * invK + k); | |
| float g = 1.0 - min(1.0, m * invK + k); | |
| float b = 1.0 - min(1.0, y * invK + k); | |
| return clamp(vec3(r, g, b), 0.0, 1.0); | |
| } | |
| vec4 RGBtoCMYK (vec3 rgb) { | |
| float r = rgb.r; | |
| float g = rgb.g; | |
| float b = rgb.b; | |
| float k = min(1.0 - r, min(1.0 - g, 1.0 - b)); | |
| vec3 cmy = vec3(0.0); | |
| float invK = 1.0 - k; | |
| if (invK != 0.0) { | |
| cmy.x = (1.0 - r - k) / invK; | |
| cmy.y = (1.0 - g - k) / invK; | |
| cmy.z = (1.0 - b - k) / invK; | |
| } | |
| return clamp(vec4(cmy, k), 0.0, 1.0); | |
| } | |
| void mainImage( out vec4 fragColor, in vec2 fragCoord ) | |
| { | |
| vec2 uv = fragCoord.xy / iResolution.xy; | |
| vec3 rgbColor = texture(iChannel0, vec2(uv.x, 1. - uv.y)).rgb; | |
| vec4 cmykColor = RGBtoCMYK(rgbColor); | |
| vec3 rgbProof = CMYKtoRGB(cmykColor); | |
| fragColor = vec4(rgbProof.xyz,1.0); | |
| // fragColor = vec4(rgbColor,1.0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment