Skip to content

Instantly share code, notes, and snippets.

@destroyermanxyz
Forked from Volcanoscar/greyscale.frag
Created September 3, 2023 08:54
Show Gist options
  • Save destroyermanxyz/b2fe6512c6d0706fbd835472c89f11e8 to your computer and use it in GitHub Desktop.
Save destroyermanxyz/b2fe6512c6d0706fbd835472c89f11e8 to your computer and use it in GitHub Desktop.

Revisions

  1. @wiseoldduck wiseoldduck revised this gist May 8, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion greyscale.frag
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    // fragment shader
    // RGBA color to RGBA greyscale
    //
    // RGBA color to RGBA greyscale
    //
    // smooth transition based on u_colorFactor: 0.0 = original, 1.0 = greyscale
    //
    // http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/
    // "The luminosity method is a more sophisticated version of the average method.
    // It also averages the values, but it forms a weighted average to account for human perception.
  2. @wiseoldduck wiseoldduck revised this gist May 8, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion greyscale.frag
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    // fragment shader
    // color to greyscale
    // RGBA color to RGBA greyscale
    // smooth transition based on u_colorFactor: 0.0 = original, 1.0 = greyscale
    // http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/
    // "The luminosity method is a more sophisticated version of the average method.
    // It also averages the values, but it forms a weighted average to account for human perception.
  3. @wiseoldduck wiseoldduck revised this gist May 8, 2014. 1 changed file with 0 additions and 11 deletions.
    11 changes: 0 additions & 11 deletions greyscale.frag
    Original file line number Diff line number Diff line change
    @@ -16,14 +16,3 @@ void main()
    float grey = 0.21 * sample.r + 0.71 * sample.g + 0.07 * sample.b;
    gl_FragColor = vec4(sample.r * u_colorFactor + grey * (1.0 - u_colorFactor), sample.g * u_colorFactor + grey * (1.0 - u_colorFactor), sample.b * u_colorFactor + grey * (1.0 - u_colorFactor), 1.0);
    }

    varying vec2 v_texCoord;
    uniform sampler2D CC_Texture0;
    uniform float u_colorFactor;

    void main()
    {
    vec4 sample = texture2D(CC_Texture0, v_texCoord);
    float grey = 0.21 * sample.r + 0.71 * sample.g + 0.07 * sample.b;
    gl_FragColor = vec4(sample.r * u_colorFactor + grey * (1.0 - u_colorFactor), sample.g * u_colorFactor + grey * (1.0 - u_colorFactor), sample.b * u_colorFactor + grey * (1.0 - u_colorFactor), 1.0);
    }
  4. @wiseoldduck wiseoldduck created this gist May 8, 2014.
    29 changes: 29 additions & 0 deletions greyscale.frag
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    // fragment shader
    // color to greyscale
    // http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/
    // "The luminosity method is a more sophisticated version of the average method.
    // It also averages the values, but it forms a weighted average to account for human perception.
    // We’re more sensitive to green than other colors, so green is weighted most heavily. The formula
    // for luminosity is 0.21 R + 0.72 G + 0.07 B."

    varying vec2 v_texCoord;
    uniform sampler2D CC_Texture0;
    uniform float u_colorFactor;

    void main()
    {
    vec4 sample = texture2D(CC_Texture0, v_texCoord);
    float grey = 0.21 * sample.r + 0.71 * sample.g + 0.07 * sample.b;
    gl_FragColor = vec4(sample.r * u_colorFactor + grey * (1.0 - u_colorFactor), sample.g * u_colorFactor + grey * (1.0 - u_colorFactor), sample.b * u_colorFactor + grey * (1.0 - u_colorFactor), 1.0);
    }

    varying vec2 v_texCoord;
    uniform sampler2D CC_Texture0;
    uniform float u_colorFactor;

    void main()
    {
    vec4 sample = texture2D(CC_Texture0, v_texCoord);
    float grey = 0.21 * sample.r + 0.71 * sample.g + 0.07 * sample.b;
    gl_FragColor = vec4(sample.r * u_colorFactor + grey * (1.0 - u_colorFactor), sample.g * u_colorFactor + grey * (1.0 - u_colorFactor), sample.b * u_colorFactor + grey * (1.0 - u_colorFactor), 1.0);
    }