| 1 | #version 300 es |
| 2 | // Invert Colors Shader - OPTIMIZED |
| 3 | // Added: Optional luminance-preserving mode |
| 4 | |
| 5 | precision highp float; |
| 6 | |
| 7 | in vec2 v_texcoord; |
| 8 | uniform sampler2D tex; |
| 9 | out vec4 fragColor; |
| 10 | |
| 11 | // Set true for "smart invert" that preserves relative brightness |
| 12 | const bool PRESERVE_LUMINANCE = false; |
| 13 | const vec3 LUMA = vec3(0.2126, 0.7152, 0.0722); |
| 14 | |
| 15 | void main() { |
| 16 | vec4 color = texture(tex, v_texcoord); |
| 17 | vec3 inverted = 1.0 - color.rgb; |
| 18 | |
| 19 | if (PRESERVE_LUMINANCE) { |
| 20 | // Adjust inverted colors to match original luminance |
| 21 | float origLuma = dot(color.rgb, LUMA); |
| 22 | float invLuma = dot(inverted, LUMA); |
| 23 | if (invLuma > 0.001) { |
| 24 | inverted *= origLuma / invLuma; |
| 25 | inverted = clamp(inverted, 0.0, 1.0); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fragColor = vec4(inverted, color.a); |
| 30 | } |