Skip to content

Instantly share code, notes, and snippets.

@jcdickinson
Last active April 28, 2024 23:27
Show Gist options
  • Save jcdickinson/580b7fb5cc145cee8740 to your computer and use it in GitHub Desktop.
Save jcdickinson/580b7fb5cc145cee8740 to your computer and use it in GitHub Desktop.
Colorblind Simulation Shader
/*-----------------------------------------------------------.
/ ColorBlind correction /
'-----------------------------------------------------------*/
// Daltonize (source http://www.daltonize.org/search/label/Daltonize)
// Modified to simulate color blindness.
float4 Daltonize( float4 input, float2 tex )
{
// RGB to LMS matrix conversion
float3 L = (17.8824f * input.r) + (43.5161f * input.g) + (4.11935f * input.b);
float3 M = (3.45565f * input.r) + (27.1554f * input.g) + (3.86714f * input.b);
float3 S = (0.0299566f * input.r) + (0.184309f * input.g) + (1.46709f * input.b);
// Simulate color blindness
#if ( COLORBLIND_MODE == 1) // Protanope - reds are greatly reduced (1% men)
float l = 0.0f * L + 2.02344f * M + -2.52581f * S;
float m = 0.0f * L + 1.0f * M + 0.0f * S;
float s = 0.0f * L + 0.0f * M + 1.0f * S;
#endif
#if ( COLORBLIND_MODE == 2) // Deuteranope - greens are greatly reduced (1% men)
float l = 1.0f * L + 0.0f * M + 0.0f * S;
float m = 0.494207f * L + 0.0f * M + 1.24827f * S;
float s = 0.0f * L + 0.0f * M + 1.0f * S;
#endif
#if ( COLORBLIND_MODE == 3) // Tritanope - blues are greatly reduced (0.003% population)
float l = 1.0f * L + 0.0f * M + 0.0f * S;
float m = 0.0f * L + 1.0f * M + 0.0f * S;
float s = -0.395913f * L + 0.801109f * M + 0.0f * S;
#endif
// LMS to RGB matrix conversion
float4 error;
error.r = (0.0809444479f * l) + (-0.130504409f * m) + (0.116721066f * s);
error.g = (-0.0102485335f * l) + (0.0540193266f * m) + (-0.113614708f * s);
error.b = (-0.000365296938f * l) + (-0.00412161469f * m) + (0.693511405f * s);
error.a = 1;
return error.rgba;
// Isolate invisible colors to color vision deficiency (calculate error matrix)
// error = (input - error);
// Shift colors towards visible spectrum (apply error modifications)
// float4 correction;
// correction.r = 0; // (error.r * 0.0) + (error.g * 0.0) + (error.b * 0.0);
// correction.g = (error.r * 0.7) + (error.g * 1.0); // + (error.b * 0.0);
// correction.b = (error.r * 0.7) + (error.b * 1.0); // + (error.g * 0.0);
// Add compensation to original values
// correction = input + correction;
// correction.a = input.a;
// return correction.rgba;
}
@MatthewDLudwig
Copy link

Hey found this while doing some googling, looking to use it to better test my games for color blind accessibility. Could you elaborate on the commented lines from 42 to 55? I think they're implementing step 3 from the daltonize.org source material you reference, but not exactly sure since you have it placed after the LMS to RGB conversion has been done.

I'll be messing with it myself and will comment again if I figure anything out for future readers, but if you have the chance to answer it'd probably be better than what I'll figure out through tinkering.

@jcdickinson
Copy link
Author

@MatthewDLudwig as far as I recall, the commented out lines are for color-blind correction, where-as the active lines simulate it. The code should be correct, as the correct happens in RGB.

@jzaffy
Copy link

jzaffy commented Oct 5, 2020

Hey! If I wanted to run this, how would I go about it? I have zero experience with coding, but would still like to make sure my art is well contrasted for people with colorblindness! Thanks for any help!

@MatthewDLudwig
Copy link

Hey! If I wanted to run this, how would I go about it? I have zero experience with coding, but would still like to make sure my art is well contrasted for people with colorblindness! Thanks for any help!

What game engine are you using? You need to find a way to load this as a shader on your whole game. Most game engines have viewport shaders or some way to emulate them.

Side note, if you're inexperienced you shouldn't be worrying too much about things like this. Unless your game is heavily focused around colors and you can't use existing resources for picking good color blind friendly colors, this is something that you can look into later down the line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment