Browse

BlackAndWhite

by Mewatools

Converts an image into Black and White using an arithmetic average of the 3 color channels. The output contains the same black and white image in the 4 channels.

Source Code
// convert to black & white using average of the 3 color channels
// ShaderToy: https://www.shadertoy.com/view/fdSXzz
 
shaderSource = "
    void mainImage( out vec4 fragColor, in vec2 fragCoord )
    {
        //const float uBrightness = 0.4; // histogram center, brightens/darkens the image. default = 0.5
        //const float uSharpness = 0.5; // between 0.001 and 1. default = 0.5
    
    	float dev = uSharpness/2.0;
    	float a = uBrightness - dev;
    	float b = uBrightness + dev;
        vec4 pixel = texture2D(iChannel0, fragCoord);
    	float gray = (pixel.r + pixel.g + pixel.b) / 3.0;
    	fragColor= vec4( smoothstep(a, b, gray) );
    }";
    
node = ShaderNode( shaderSource, "Black&White" );
node.addShaderInput("iChannel0");
node.setHelpPage("https://www.shadertoy.com/view/fdSXzz");

uiControl = node.addFloatControl("uBrightness", 0.5);
uiControl.setName("Brightness");
uiControl.setStep(0.001);
uiControl.setRange(0.0, 1.0);

uiControl = node.addFloatControl("uSharpness", 0.5);
uiControl.setName("Sharpness");
uiControl.setStep(0.001);
uiControl.setRange(0.001, 1.0);

node.finishAdding();
File Details
  • Last Update: April 27th, 2021
  • Category:
  • License: