Skip to content

Instantly share code, notes, and snippets.

@lastguest
Created April 1, 2019 16:35
Show Gist options
  • Save lastguest/5480da03dc7becd63648b74e10b38f7d to your computer and use it in GitHub Desktop.
Save lastguest/5480da03dc7becd63648b74e10b38f7d to your computer and use it in GitHub Desktop.
[SHADER] hue-lightness ordered dithering - http://alex-charlton.com/posts/Dithering_on_the_GPU/
const float lightnessSteps = 4.0;
float lightnessStep(float l) {
/* Quantize the lightness to one of `lightnessSteps` values */
return floor((0.5 + l * lightnessSteps)) / lightnessSteps;
}
vec3 dither(vec3 color) {
vec3 hsl = rgbToHsl(color);
vec3 cs[2] = closestColors(hsl.x);
vec3 c1 = cs[0];
vec3 c2 = cs[1];
float d = indexValue();
float hueDiff = hueDistance(hsl.x, c1.x) / hueDistance(c2.x, c1.x);
float l1 = lightnessStep(max((hsl.z - 0.125), 0.0));
float l2 = lightnessStep(min((hsl.z + 0.124), 1.0));
float lightnessDiff = (hsl.z - l1) / (l2 - l1);
vec3 resultColor = (hueDiff < d) ? c1 : c2;
resultColor.z = (lightnessDiff < d) ? l1 : l2;
return hslToRgb(resultColor);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment