From e3638869c91eaa8f6c71711a859ff0bf3b96d595 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Mon, 10 Feb 2020 11:19:17 +0100 Subject: [PATCH] Add ColorCorrection to orx-fx --- .../src/main/kotlin/color/ColorCorrection.kt | 28 +++++++++ .../extra/fx/gl3/color/color-correction.frag | 62 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 orx-fx/src/main/kotlin/color/ColorCorrection.kt create mode 100644 orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/color/color-correction.frag diff --git a/orx-fx/src/main/kotlin/color/ColorCorrection.kt b/orx-fx/src/main/kotlin/color/ColorCorrection.kt new file mode 100644 index 00000000..4467f37a --- /dev/null +++ b/orx-fx/src/main/kotlin/color/ColorCorrection.kt @@ -0,0 +1,28 @@ +package org.openrndr.extra.fx.color + +import org.openrndr.draw.* +import org.openrndr.extra.fx.filterFragmentCode +import org.openrndr.extra.parameters.Description +import org.openrndr.extra.parameters.DoubleParameter + +@Description("Color correction") +class ColorCorrection : Filter(Shader.createFromCode(filterVertexCode, filterFragmentCode("color/color-correction.frag"))) { + @DoubleParameter("brightness", -1.0, 1.0, order = 0) + var brightness: Double by parameters + + @DoubleParameter("contrast", -1.0, 1.0, order = 1) + var contrast: Double by parameters + + @DoubleParameter("saturation", -1.0, 1.0, order = 2) + var saturation: Double by parameters + + @DoubleParameter("hue shift", -180.0, 180.0, order = 3) + var hueShift: Double by parameters + + init { + contrast = 0.0 + brightness = 0.0 + saturation = 0.0 + hueShift = 0.0 + } +} \ No newline at end of file diff --git a/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/color/color-correction.frag b/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/color/color-correction.frag new file mode 100644 index 00000000..6b48e502 --- /dev/null +++ b/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/color/color-correction.frag @@ -0,0 +1,62 @@ +/* based on "Brightness, contrast, saturation" by WojtaZam: https://www.shadertoy.com/view/XdcXzn */ + +#version 330 core + +uniform float brightness; +uniform float saturation; +uniform float contrast; +uniform float hueShift; + +uniform sampler2D tex0; +in vec2 v_texCoord0; +out vec4 o_color; + +mat4 brightnessMatrix(float brightness) { + return mat4(1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + brightness, brightness, brightness, 1); +} + +mat4 contrastMatrix(float contrast) { + float t = (1.0 - contrast) / 2.0; + return mat4(contrast, 0, 0, 0, + 0, contrast, 0, 0, + 0, 0, contrast, 0, + t, t, t, 1 ); +} + +mat4 saturationMatrix(float saturation) { + vec3 luminance = vec3(0.3086, 0.6094, 0.0820); + float oneMinusSat = 1.0 - saturation; + vec3 red = vec3(luminance.x * oneMinusSat); + red += vec3(saturation, 0, 0); + + vec3 green = vec3(luminance.y * oneMinusSat); + green += vec3(0, saturation, 0); + + vec3 blue = vec3(luminance.z * oneMinusSat); + blue += vec3(0, 0, saturation); + + return mat4(red, 0, + green, 0, + blue, 0, + 0, 0, 0, 1 ); +} + +// from starea's https://www.shadertoy.com/view/MdjBRy, which in turn remixed it from mAlk's https://www.shadertoy.com/view/MsjXRt +vec3 shiftHue(in vec3 col, in float Shift) { + vec3 P = vec3(0.55735) * dot(vec3(0.55735), col); + vec3 U = col - P; + vec3 V = cross(vec3(0.55735), U); + col = U * cos(Shift * 6.2832) + V * sin(Shift * 6.2832) + P; + return col; +} + +void main() { + vec4 color = texture(tex0, v_texCoord0); + vec4 nc = (color.a == 0.0) ? vec4(0.0) : vec4(color.rgb / color.a, color.a); + nc.rgb = shiftHue(nc.rgb, (hueShift/360.0)); + vec4 cc = brightnessMatrix(brightness) * contrastMatrix((contrast + 1)) * saturationMatrix(saturation + 1) * nc; + o_color = vec4(cc.rgb, 1.0) * color.a; +} \ No newline at end of file