Saturation.js 645 Bytes
Newer Older
1 2
import GL from "gl-react";
import React from "react";
3 4

const shaders = GL.Shaders.create({
5
  saturation: {
6 7 8 9 10 11 12 13
    frag: `
precision highp float;
varying vec2 uv;
uniform sampler2D image;
uniform float factor;

void main () {
  vec4 c = texture2D(image, uv);
14 15 16
  // Algorithm from Chapter 16 of OpenGL Shading Language
  const vec3 W = vec3(0.2125, 0.7154, 0.0721);
  gl_FragColor = vec4(mix(vec3(dot(c.rgb, W)), c.rgb, factor), c.a);
17 18 19 20 21
}
    `
  }
});

22 23
module.exports = GL.createComponent(
  ({ factor, image, ...rest }) =>
24
  <GL.Node
25 26 27 28 29
    {...rest}
    shader={shaders.saturation}
    uniforms={{ factor, image }}
  />,
{ displayName: "Saturation" });