gpu: add colorblind mode

Co-authored-by: Adam <Adam@sigterm.info>
This commit is contained in:
Ben Poulson
2020-11-04 06:23:10 +00:00
committed by Adam
parent 7f49b88b42
commit b031d4c60f
6 changed files with 137 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
//
// Algorithm from "Analysis of Color Blindness" by Onur Fidaner, Poliang Lin and Nevran Ozguven.
// https://web.archive.org/web/20090731011248/http://scien.stanford.edu/class/psych221/projects/05/ofidaner/project_report.pdf
//
#define NONE 0
#define PROTAN 1
#define DEUTERAN 2
#define TRITAN 3
const mat3 rgb2lms = mat3(
vec3(17.8824, 43.5161, 4.11935),
vec3(3.45565, 27.1554, 3.86714),
vec3(0.0299566, 0.184309, 1.46709)
);
const mat3 lms2lmsp = mat3(
vec3(0.0, 2.02344, -2.52581),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0)
);
const mat3 lms2lmsd = mat3(
vec3(1.0, 0.0, 0.0),
vec3(0.494207, 0.0, 1.24827),
vec3(0.0, 0.0, 1.0)
);
const mat3 lms2lmst = mat3(
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(-0.395913, 0.801109, 0.0)
);
const mat3 corrections = mat3(
vec3(0.0, 0.0, 0.0),
vec3(0.7, 1.0, 0.0),
vec3(0.7, 0.0, 1.0)
);
vec3 colorblind(int mode, vec3 color)
{
vec3 LMS = color * rgb2lms;
vec3 lms;
if (mode == PROTAN) {
lms = LMS * lms2lmsp; // red deficiency
}
else if (mode == DEUTERAN) {
lms = LMS * lms2lmsd; // green deficiency
}
else if (mode == TRITAN) {
lms = LMS * lms2lmst; // blue deficiency
}
else {
// Should be impossible to get here
return color;
}
// LMS to RGB matrix conversion
mat3 lms2rgb = inverse(rgb2lms);
vec3 error = lms * lms2rgb;
// Isolate invisible colors to color vision deficiency (calculate error matrix)
error = (color - error);
// Shift colors towards visible spectrum (apply error modifications)
vec3 correction = error * corrections;
// Add compensation to original values
correction = color + correction;
return correction;
}

View File

@@ -29,6 +29,7 @@ uniform vec2 textureOffsets[64];
uniform float brightness;
uniform float smoothBanding;
uniform vec4 fogColor;
uniform int colorBlindMode;
in vec4 Color;
noperspective centroid in float fHsl;
@@ -39,6 +40,7 @@ in float fogAmount;
out vec4 FragColor;
#include hsl_to_rgb.glsl
#include colorblind.glsl
void main() {
int hsl = int(fHsl);
@@ -57,6 +59,10 @@ void main() {
smoothColor = textureColorBrightness * smoothColor;
}
if (colorBlindMode > 0) {
smoothColor.rgb = colorblind(colorBlindMode, smoothColor.rgb);
}
vec3 mixedColor = mix(smoothColor.rgb, fogColor.rgb, fogAmount);
FragColor = vec4(mixedColor, smoothColor.a);
}

View File

@@ -34,9 +34,11 @@ uniform sampler2D tex;
uniform int samplingMode;
uniform ivec2 sourceDimensions;
uniform ivec2 targetDimensions;
uniform int colorBlindMode;
#include scale/bicubic.glsl
#include scale/xbr_lv2_frag.glsl
#include colorblind.glsl
in vec2 TexCoord;
in XBRTable xbrTable;
@@ -53,5 +55,9 @@ void main() {
else if (samplingMode == SAMPLING_XBR)
c = textureXBR(tex, TexCoord, xbrTable, ceil(1.0 * targetDimensions.x / sourceDimensions.x));
if (colorBlindMode > 0) {
c.rgb = colorblind(colorBlindMode, c.rgb);
}
FragColor = c;
}