From d40f3b04f07957e4333ceedc6fb86e1fbf37d855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20D=C3=BCrr?= Date: Sun, 24 Dec 2017 16:25:06 +0100 Subject: [PATCH] Make visual bell flash color customizable This addresses the main feedback in jwilm/alacritty/pull/430. I've decided to go from scratch instead of basing my work on top of what markandrus has already implemented to keep it as simple as possible. If there's any stuff that I should take from the other PR, please let me know. I can also try to send a PR to markandrus. --- res/text.f.glsl | 7 ++++++- res/text.v.glsl | 4 ++++ src/config.rs | 15 +++++++++++++++ src/renderer/mod.rs | 31 ++++++++++++++++++++++++------- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/res/text.f.glsl b/res/text.f.glsl index 5915960f..c551179e 100644 --- a/res/text.f.glsl +++ b/res/text.f.glsl @@ -15,6 +15,7 @@ in vec2 TexCoords; in vec3 fg; in vec4 bg; +in vec3 vbc; flat in float vb; flat in int background; @@ -31,7 +32,11 @@ void main() discard; alphaMask = vec4(1.0); - color = vec4(bg.rgb + vb, 1.0); + if (vb == 0) { + color = vec4(bg.rgb, 1.0); + } else { + color = vec4(vbc, vb); + } } else { vec3 textColor = texture(mask, TexCoords).rgb; alphaMask = vec4(textColor, textColor.r); diff --git a/res/text.v.glsl b/res/text.v.glsl index 9580c71f..a13d99a7 100644 --- a/res/text.v.glsl +++ b/res/text.v.glsl @@ -25,18 +25,21 @@ layout (location = 3) in vec4 uv; // text fg color layout (location = 4) in vec3 textColor; + // Background color layout (location = 5) in vec4 backgroundColor; out vec2 TexCoords; out vec3 fg; out vec4 bg; +out vec3 vbc; // Terminal properties uniform vec2 termDim; uniform vec2 cellDim; uniform float visualBell; +uniform vec3 visualBellColor; uniform int backgroundPass; // Orthographic projection @@ -74,6 +77,7 @@ void main() } vb = visualBell; + vbc = visualBellColor; background = backgroundPass; bg = vec4(backgroundColor.rgb / 255.0, backgroundColor.a); fg = textColor / vec3(255.0, 255.0, 255.0); diff --git a/src/config.rs b/src/config.rs index be967150..bf545be3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -150,6 +150,10 @@ pub struct VisualBellConfig { #[serde(deserialize_with = "deserialize_visual_bell_duration")] #[serde(default="default_visual_bell_duration")] duration: u16, + + /// Visual bell flash color + #[serde(default="default_visual_bell_color", deserialize_with = "rgb_from_hex")] + color: Rgb, } fn default_visual_bell_duration() -> u16 { @@ -168,6 +172,10 @@ fn deserialize_visual_bell_duration<'a, D>(deserializer: D) -> ::std::result::Re } } +fn default_visual_bell_color() -> Rgb { + Rgb { r: 255, g: 255, b: 255 } +} + impl VisualBellConfig { /// Visual bell animation #[inline] @@ -180,6 +188,12 @@ impl VisualBellConfig { pub fn duration(&self) -> Duration { Duration::from_millis(u64::from(self.duration)) } + + /// Visual bell flash color + #[inline] + pub fn color(&self) -> Rgb { + self.color + } } impl Default for VisualBellConfig { @@ -187,6 +201,7 @@ impl Default for VisualBellConfig { VisualBellConfig { animation: VisualBellAnimation::default(), duration: default_visual_bell_duration(), + color: default_visual_bell_color(), } } } diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index c0e4a9f3..49b30203 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -118,6 +118,9 @@ pub struct ShaderProgram { /// Visual bell u_visual_bell: GLint, + /// Visual bell color + u_visual_bell_color: GLint, + /// Background pass flag /// /// Rendering is split into two passes; 1 for backgrounds, and one for text @@ -644,10 +647,12 @@ impl QuadRenderer { } } + let visual_bell_color = config.visual_bell().color(); + unsafe { self.program.activate(); self.program.set_term_uniforms(props); - self.program.set_visual_bell(visual_bell_intensity as _); + self.program.set_visual_bell(visual_bell_intensity as _, visual_bell_color); gl::BindVertexArray(self.vao); gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, self.ebo); @@ -734,16 +739,21 @@ impl QuadRenderer { } } +fn mix(x: f32, y: f32, a: f32) -> f32 { + x * (1.0 - a) + y * a +} + impl<'a> RenderApi<'a> { pub fn clear(&self, color: Rgb) { let alpha = self.config.background_opacity().get(); + let (flash, intensity) = (self.config.visual_bell().color(), self.visual_bell_intensity); unsafe { gl::ClearColor( - (self.visual_bell_intensity + f32::from(color.r) / 255.0).min(1.0) * alpha, - (self.visual_bell_intensity + f32::from(color.g) / 255.0).min(1.0) * alpha, - (self.visual_bell_intensity + f32::from(color.b) / 255.0).min(1.0) * alpha, + mix(color.r as f32 / 255.0, flash.r as f32 / 255.0, intensity).min(1.0) * alpha, + mix(color.g as f32 / 255.0, flash.g as f32 / 255.0, intensity).min(1.0) * alpha, + mix(color.b as f32 / 255.0, flash.b as f32 / 255.0, intensity).min(1.0) * alpha, alpha - ); + ); gl::Clear(gl::COLOR_BUFFER_BIT); } } @@ -998,12 +1008,13 @@ impl ShaderProgram { } // get uniform locations - let (projection, term_dim, cell_dim, visual_bell, background) = unsafe { + let (projection, term_dim, cell_dim, visual_bell, visual_bell_color, background) = unsafe { ( gl::GetUniformLocation(program, cptr!(b"projection\0")), gl::GetUniformLocation(program, cptr!(b"termDim\0")), gl::GetUniformLocation(program, cptr!(b"cellDim\0")), gl::GetUniformLocation(program, cptr!(b"visualBell\0")), + gl::GetUniformLocation(program, cptr!(b"visualBellColor\0")), gl::GetUniformLocation(program, cptr!(b"backgroundPass\0")), ) }; @@ -1016,6 +1027,7 @@ impl ShaderProgram { u_term_dim: term_dim, u_cell_dim: cell_dim, u_visual_bell: visual_bell, + u_visual_bell_color: visual_bell_color, u_background: background, padding_x: config.padding().x.floor(), padding_y: config.padding().y.floor(), @@ -1061,9 +1073,14 @@ impl ShaderProgram { } } - fn set_visual_bell(&self, visual_bell: f32) { + fn set_visual_bell(&self, visual_bell: f32, vb_color: Rgb) { unsafe { gl::Uniform1f(self.u_visual_bell, visual_bell); + gl::Uniform3f(self.u_visual_bell_color, + vb_color.r as f32 / 255., + vb_color.g as f32 / 255., + vb_color.b as f32 / 255. + ); } }