From 6499c0d3d546e2d5fb02431bc3740df3f1618834 Mon Sep 17 00:00:00 2001 From: Manolis Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Mon, 17 May 2021 02:24:49 +0300 Subject: [PATCH] Auto invert preview colors based on the screen color Basically, when the background color of the screen at certain points is dark, the preview at that spot is white, and when it is bright, the preview is white at that spot. This helps to keep the previews always distinguishable, regardless of the background color. --- src/Shaders/AutoInvertColors.shader | 25 +++++++++++++++++++++++++ src/Tools/LineTool.gd | 2 +- src/Tools/ShapeDrawer.gd | 2 +- src/UI/Canvas/Canvas.tscn | 7 ++++++- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/Shaders/AutoInvertColors.shader diff --git a/src/Shaders/AutoInvertColors.shader b/src/Shaders/AutoInvertColors.shader new file mode 100644 index 000000000..7296e9d6d --- /dev/null +++ b/src/Shaders/AutoInvertColors.shader @@ -0,0 +1,25 @@ +shader_type canvas_item; + + +vec3 rgb2hsb(vec3 c){ + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), + vec4(c.gb, K.xy), + step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), + vec4(c.r, p.yzx), + step(p.x, c.r)); + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), + d / (q.x + e), + q.x); +} + + +void fragment() { + vec3 screen_color = rgb2hsb(textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb); + if(screen_color.z < 0.5){ + COLOR.rgb = vec3(1.0) - COLOR.rgb; + } +} diff --git a/src/Tools/LineTool.gd b/src/Tools/LineTool.gd index c6dc00ca2..3c2c1fbd5 100644 --- a/src/Tools/LineTool.gd +++ b/src/Tools/LineTool.gd @@ -112,7 +112,7 @@ func draw_preview() -> void: canvas.draw_set_transform(start - t_offsetv, canvas.rotation, canvas.scale) for line in _create_polylines(indicator): - canvas.draw_polyline(PoolVector2Array(line), tool_slot.color) + canvas.draw_polyline(PoolVector2Array(line), Color.black) canvas.draw_set_transform(canvas.position, canvas.rotation, canvas.scale) diff --git a/src/Tools/ShapeDrawer.gd b/src/Tools/ShapeDrawer.gd index e83b9683e..fb5640ce9 100644 --- a/src/Tools/ShapeDrawer.gd +++ b/src/Tools/ShapeDrawer.gd @@ -105,7 +105,7 @@ func draw_preview() -> void: canvas.draw_set_transform(rect.position - t_offsetv, canvas.rotation, canvas.scale) for line in _create_polylines(indicator): - canvas.draw_polyline(PoolVector2Array(line), tool_slot.color) + canvas.draw_polyline(PoolVector2Array(line), Color.black) canvas.draw_set_transform(canvas.position, canvas.rotation, canvas.scale) diff --git a/src/UI/Canvas/Canvas.tscn b/src/UI/Canvas/Canvas.tscn index c23c9d8c2..d7302dc96 100644 --- a/src/UI/Canvas/Canvas.tscn +++ b/src/UI/Canvas/Canvas.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=12 format=2] +[gd_scene load_steps=14 format=2] [ext_resource path="res://src/UI/Canvas/Canvas.gd" type="Script" id=1] [ext_resource path="res://src/UI/Canvas/Grid.gd" type="Script" id=2] @@ -9,6 +9,7 @@ [ext_resource path="res://src/UI/Canvas/Previews.gd" type="Script" id=7] [ext_resource path="res://src/UI/Canvas/Selection.gd" type="Script" id=8] [ext_resource path="res://src/Shaders/MarchingAntsOutline.shader" type="Shader" id=9] +[ext_resource path="res://src/Shaders/AutoInvertColors.shader" type="Shader" id=10] [sub_resource type="CanvasItemMaterial" id=1] blend_mode = 4 @@ -22,6 +23,9 @@ shader_param/width = 0.05 shader_param/frequency = 200.0 shader_param/stripe_direction = 0.5 +[sub_resource type="ShaderMaterial" id=4] +shader = ExtResource( 10 ) + [node name="Canvas" type="Node2D"] script = ExtResource( 1 ) @@ -58,4 +62,5 @@ centered = false script = ExtResource( 3 ) [node name="Previews" type="Node2D" parent="."] +material = SubResource( 4 ) script = ExtResource( 7 )