Skip to content

Commit

Permalink
feat(shaders): grayscale shader
Browse files Browse the repository at this point in the history
  • Loading branch information
loqusion committed May 12, 2024
1 parent 8119181 commit 0c14891
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions shaders/grayscale.glsl.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Grayscale
*/

precision highp float;
varying vec2 v_texcoord;
uniform sampler2D tex;

// Enum for type of grayscale conversion
const int LUMINOSITY = 0;
const int LIGHTNESS = 1;
const int AVERAGE = 2;

/**
* Type of grayscale conversion.
*/
const int Type = {{#nc}}{{type}} ? LUMINOSITY{{/nc}};

// Enum for luma coefficients
const int PAL = 0;
const int HDTV = 1;
const int HDR = 2;

/**
* Standard for calculating luma component.
*/
const int LuminosityType = {{#nc}}{{luminosity_type}} ? HDR{{/nc}};

void main() {
vec4 pixColor = texture2D(tex, v_texcoord);

float gray;
if (Type == LUMINOSITY) {
// https://en.wikipedia.org/wiki/Grayscale#Luma_coding_in_video_systems
if (LuminosityType == 0) {
gray = dot(pixColor.rgb, vec3(0.299, 0.587, 0.114));
} else if (LuminosityType == 1) {
gray = dot(pixColor.rgb, vec3(0.2126, 0.7152, 0.0722));
} else if (LuminosityType == 2) {
gray = dot(pixColor.rgb, vec3(0.2627, 0.6780, 0.0593));
}
} else if (Type == LIGHTNESS) {
float maxPixColor = max(pixColor.r, max(pixColor.g, pixColor.b));
float minPixColor = min(pixColor.r, min(pixColor.g, pixColor.b));
gray = (maxPixColor + minPixColor) / 2.0;
} else if (Type == AVERAGE) {
gray = (pixColor.r + pixColor.g + pixColor.b) / 3.0;
}
vec3 grayscale = vec3(gray);

gl_FragColor = vec4(grayscale, pixColor.a);
}

// vim: ft=glsl

0 comments on commit 0c14891

Please sign in to comment.