aeon-199x/shaders/module/color.frag

130 lines
2.9 KiB
GLSL
Raw Normal View History

#include "/var/color.glsl"
#define colorMode 1 // [0 1 2 3 4]
#define firstBits 2 // [0 1 2 3 4 5 6 7 8]
#define secondBits 2 // [0 1 2 3 4 5 6 7 8]
#define thirdBits 2 // [0 1 2 3 4 5 6 7 8]
#define monoPalette 0 // [0 1 2 3]
float bit_max(int bits) { return pow(2, bits); }
float firstMax = bit_max(firstBits);
#if colorMode == COLOR_MONO
float secondMax = firstMax;
float thirdMax = firstMax;
#else
float secondMax = bit_max(secondBits);
float thirdMax = bit_max(thirdBits);
#endif
#if monoPalette == MONO_BW
vec3 monoHigh = vec3(1);
vec3 monoLow = vec3(0);
#elif monoPalette == MONO_DAMEGAME
vec3 monoHigh = vec3(0.31, 0.40, 0.03);
vec3 monoLow = vec3(0.17, 0.29, 0.13);
#elif monoPalette == MONO_MOTIONSICK
vec3 monoHigh = vec3(1, 0, 0);
vec3 monoLow = vec3(0);
#elif monoPalette == MONO_NOIR
vec3 monoHigh = vec3(0.73, 0.67, 0.55);
vec3 monoLow = vec3(0.26, 0.23, 0.19);
#endif
float luminance(vec3 color) {
return dot(color, vec3(0.299, 0.587, 0.114));
}
vec3 toHsv(vec3 rgb) {
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g));
vec4 q = mix(vec4(p.xyw, rgb.r), vec4(rgb.r, p.yzx), step(p.x, rgb.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
vec3 fromHsv(vec3 hsv) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(hsv.xxx + K.xyz) * 6 - K.www);
return hsv.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), hsv.y);
}
vec3 toYiq(vec3 rgb) {
float y = luminance(rgb);
float i = dot(rgb, vec3(0.596, -.274, -.322));
float q = dot(rgb, vec3(0.211, -.523, 0.312));
return vec3(y, i, q);
}
vec3 fromYiq(vec3 yiq) {
float r = dot(yiq, vec3(1, 0.956, 0.619));
float g = dot(yiq, vec3(1, -.272, -.674));
float b = dot(yiq, vec3(1, -1.106, 1.703));
return vec3(r, g, b);
}
vec3 toYuv(vec3 rgb) {
float y = luminance(rgb);
float u = 0.492 * (rgb.b - y);
float v = 0.877 * (rgb.r - y);
return vec3(y, u, v);
}
vec3 fromYuv(vec3 yuv) {
float r = yuv.x + (1.140 * yuv.z);
float g = yuv.x - (0.395 * yuv.y) - (0.581 * yuv.z);
float b = yuv.x + (2.033 * yuv.y);
return vec3(r, g, b);
}
vec3 toMono(vec3 rgb) {
float luma = luminance(rgb);
return vec3(luma, luma, luma);
}
vec3 fromMono(vec3 mono) {
return (mono.x * (monoHigh - monoLow)) + monoLow;
}
vec3 to(vec3 rgb) {
#if colorMode == COLOR_RGB
return rgb;
#elif colorMode == COLOR_HSV
return toHsv(rgb);
#elif colorMode == COLOR_YIQ
return toYiq(rgb);
#elif colorMode == COLOR_YUV
return toYuv(rgb);
#elif colorMode == COLOR_MONO
return toMono(rgb);
#endif
}
vec3 from(vec3 color) {
#if colorMode == COLOR_RGB
return color;
#elif colorMode == COLOR_HSV
return fromHsv(color);
#elif colorMode == COLOR_YIQ
return fromYiq(color);
#elif colorMode == COLOR_YUV
return fromYuv(color);
#elif colorMode == COLOR_MONO
return fromMono(color);
#endif
}