-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |