96 lines
2.1 KiB
GLSL
96 lines
2.1 KiB
GLSL
|
|
||
|
#include "/var/color.glsl"
|
||
|
|
||
|
#define colorMode 1 // [0 1 2 3]
|
||
|
|
||
|
#define firstBits 2 // [1 2 3 4 5 6 7 8]
|
||
|
#define secondBits 2 // [1 2 3 4 5 6 7 8]
|
||
|
#define thirdBits 2 // [1 2 3 4 5 6 7 8]
|
||
|
|
||
|
float bit_max(int bits) { return pow(2, bits); }
|
||
|
|
||
|
float firstMax = bit_max(firstBits);
|
||
|
float secondMax = bit_max(secondBits);
|
||
|
float thirdMax = bit_max(thirdBits);
|
||
|
|
||
|
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 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);
|
||
|
#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);
|
||
|
#endif
|
||
|
}
|
||
|
|