1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-13 07:22:52 +03:00

wezterm: revise opacity configuration

This commit revises the opacity configuration to make it more
consistently applied.

* `window_background_opacity` controls the overall window capacity,
  whether a background image is present or not.
* When a background image is present, or if the window is transparent,
  then text whose background color is the default background is
  changed to have a fully transparent background.
* `text_background_opacity` controls the alpha channel value for
  text whose background color is NOT the default background.
  It defaults to 1.0 (fully opaque), but can be set to be
  transparent by setting it to a number between 0.0 and 1.0.
* The inactive pane hue, saturation, brightness multipliers
  have been factored out into their own struct which changes
  that set of options to:

```lua
return {
  inactive_pane_hsb = {
    hue = 1.0,
    saturation = 1.0,
    brightness = 1.0,
  },
}
```

* `window_background_image_hsb` is a new option that can apply
  a hue, saturation, brightness transformation to a background
  image.  This is primarily useful to make a background image
  darker:

```lua
return {
  window_background_image = "/some/pic.png",
  window_background_image_hsb = {
    brightness = 0.3,
  },
}
```

refs: #302
refs: #297
This commit is contained in:
Wez Furlong 2020-10-23 23:16:00 -07:00
parent 18c2c6838a
commit 7f82f333f1
7 changed files with 82 additions and 72 deletions

View File

@ -3,6 +3,16 @@ use luahelper::impl_lua_conversion;
use termwiz::cell::CellAttributes;
use termwiz::color::{ColorSpec, RgbColor};
#[derive(Debug, Default, Copy, Deserialize, Serialize, Clone)]
pub struct HsbTransform {
#[serde(default = "default_one_point_oh")]
pub hue: f32,
#[serde(default = "default_one_point_oh")]
pub saturation: f32,
#[serde(default = "default_one_point_oh")]
pub brightness: f32,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Palette {
/// The text color to use when the attributes are reset to default

View File

@ -700,6 +700,8 @@ pub struct Config {
/// The image will be scaled to fit the window.
#[serde(default)]
pub window_background_image: Option<PathBuf>,
#[serde(default)]
pub window_background_image_hsb: Option<HsbTransform>,
/// Specifies the alpha value to use when rendering the background
/// of the window. The background is taken either from the
@ -743,28 +745,11 @@ pub struct Config {
/// A subtle dimming effect can be achieved by setting:
/// inactive_pane_saturation = 0.9
/// inactive_pane_brightness = 0.8
#[serde(default = "default_one_point_oh")]
pub inactive_pane_hue: f32,
#[serde(default = "default_one_point_oh")]
pub inactive_pane_saturation: f32,
#[serde(default = "default_one_point_oh")]
pub inactive_pane_brightness: f32,
#[serde(default)]
pub inactive_pane_hsb: Option<HsbTransform>,
/// Specifies the alpha value to use when applying the default
/// background color in a cell. This is useful to apply a kind
/// of "tint" to the background image if either window_background_image
/// or window_background_opacity are in used.
///
/// It can be a number between 0.0 and 1.0.
/// The default is 0.0
///
/// Larger numbers increase the amount of the color scheme's
/// background color that is applied over the background image.
///
/// This can be useful to increase effective contrast for text
/// that is rendered over the top.
#[serde(default = "default_zero_point_oh")]
pub window_background_tint: f32,
#[serde(default = "default_one_point_oh")]
pub text_background_opacity: f32,
/// Specifies how often a blinking cursor transitions between visible
/// and invisible, expressed in milliseconds.
@ -832,10 +817,6 @@ pub struct Config {
pub enable_csi_u_key_encoding: bool,
}
fn default_zero_point_oh() -> f32 {
0.0
}
fn default_one_point_oh() -> f32 {
1.0
}

View File

@ -229,7 +229,30 @@ PNG, JPEG, GIF, BMP, ICO, TIFF, PNM, DDS, TGA and farbfeld files
can be loaded.
The image will be scaled to fit the window contents. Very large
images may decrease render performance.
images may decrease render performance and take up VRAM from the
GPU, so you may wish to resize the image file before using it.
You can optionally transform the background image by specifying
a hue, saturation, brightness multiplier:
```lua
return {
window_background_image = "/path/to/wallpaper.jpg",
window_background_image_hsb = {
-- Darken the background image by reducing it to 1/3rd
brightness = 0.3,
-- You can adjust the hue by scaling its value.
-- a multiplier of 1.0 leaves the value unchanged.
hue = 1.0,
-- You can adjust the saturation also.
saturation = 1.0,
},
}
```
## Window Background Opacity
@ -257,7 +280,7 @@ return {
}
```
## Window Backing Tint
## Text Background Opacity
*since: nightly builds only*
@ -265,24 +288,18 @@ When using a background image or background opacity, the image content can
have relatively low contrast with respect to the text you are trying to
read in your terminal.
The `window_background_tint` setting specifies the alpha channel value to use
for the background color of cells when either `window_background_image` or
`window_background_opacity` are in use.
The `text_background_opacity` setting specifies the alpha channel value to use
for the background color of cells other than the default background color.
The default for this setting is `0.0` such that the background image or
desktop content shows through the window relatively unimpeded.
Increasing `window_background_tint` causes whatever your color scheme's
background color to be mixed into the background. If you have a dark
color this effectively will darken the content behind and increase the
contrast for your text, making it a bit more readable.
The default for this setting is `1.0`, which means that the background
color is fully opaque.
The range of values permitted are `0.0` (completely translucent)
through to `1.0` (completely opaque).
```lua
return {
window_background_tint = 0.0,
text_background_opacity = 0.3,
}
```

View File

@ -64,17 +64,20 @@ void main() {
if (o_has_color == 2.0) {
// We're the window background image.
color = texture(glyph_tex, o_tex);
// Apply window_background_opacity to the background image
// Apply window_background_image_opacity to the background image
color.a = o_bg_color.a;
} else if (!has_background_image) {
// If there is no background image then take the cell background
color = o_bg_color;
} else {
// Nothing else should render on the background layer
// color = vec4(0,0,0,0);
discard;
}
} else if (bg_and_line_layer) {
if (o_has_color == 2.0) {
// Don't render the background image on anything other than
// the window_bg_layer.
discard;
return;
}
// Note that o_bg_color is set to transparent if the background
// color is "default" and there is a window background attachment
color = o_bg_color;
@ -102,7 +105,6 @@ void main() {
if (o_has_color == 2.0) {
// Don't render the background image on anything other than
// the window_bg_layer.
// color = vec4(0,0,0,0);
discard;
} else {
color = texture(glyph_tex, o_tex);

View File

@ -192,8 +192,10 @@ impl<'a> Quad<'a> {
}
}
pub fn set_hsv(&mut self, hsv: Option<(f32, f32, f32)>) {
let s = hsv.unwrap_or((1., 1., 1.));
pub fn set_hsv(&mut self, hsv: Option<config::HsbTransform>) {
let s = hsv
.map(|t| (t.hue, t.saturation, t.brightness))
.unwrap_or((1., 1., 1.));
for v in self.vert.iter_mut() {
v.hsv = s;
}

View File

@ -2263,10 +2263,10 @@ impl TermWindow {
let foreground = rgbcolor_to_window_color(palette.split);
let background = rgbcolor_alpha_to_window_color(
palette.background,
if self.window_background.is_none() && config.window_background_opacity == 1.0 {
0xff
if self.window_background.is_some() || config.window_background_opacity != 1.0 {
0x00
} else {
(config.window_background_tint * 255.0) as u8
(config.text_background_opacity * 255.0) as u8
},
);
@ -2386,9 +2386,6 @@ impl TermWindow {
let palette = pos.pane.palette();
let background_color = palette.resolve_bg(wezterm_term::color::ColorAttribute::Default);
let background_alpha = (config.window_background_opacity * 255.0) as u8;
let background = rgbcolor_alpha_to_window_color(palette.background, background_alpha);
let first_line_offset = if self.show_tab_bar { 1 } else { 0 };
let mut term = pos.pane.renderer();
@ -2498,7 +2495,8 @@ impl TermWindow {
quad.set_cursor(white_space);
quad.set_has_color(false);
let color = Color::rgba(0, 0, 0, background_alpha);
let background_image_alpha = (config.window_background_opacity * 255.0) as u8;
let color = Color::rgba(0, 0, 0, background_image_alpha);
quad.set_texture_adjust(0., 0., 0., 0.);
quad.set_texture(white_space);
@ -2507,10 +2505,10 @@ impl TermWindow {
quad.set_texture(sprite.texture_coords());
quad.set_is_background_image();
}
quad.set_hsv(None);
quad.set_hsv(config.window_background_image_hsb);
quad.set_cursor_color(color);
quad.set_fg_color(color);
quad.set_bg_color(background);
quad.set_bg_color(color);
quad.set_cursor_color(color);
}
@ -2672,13 +2670,12 @@ impl TermWindow {
let hsv = if params.is_active {
None
} else {
Some((
params.config.inactive_pane_hue,
params.config.inactive_pane_saturation,
params.config.inactive_pane_brightness,
))
params.config.inactive_pane_hsb
};
let window_is_transparent =
self.window_background.is_some() || params.config.window_background_opacity != 1.0;
// Break the line into clusters of cells with the same attributes
let cell_clusters = params.line.cluster();
let mut last_cell_idx = 0;
@ -2690,6 +2687,7 @@ impl TermWindow {
};
let style = self.fonts.match_style(params.config, attrs);
let bg_is_default = attrs.background == ColorAttribute::Default;
let bg_color = params.palette.resolve_bg(attrs.background);
let fg_color = match attrs.foreground {
wezterm_term::color::ColorAttribute::Default => {
@ -2717,26 +2715,27 @@ impl TermWindow {
_ => params.palette.resolve_fg(attrs.foreground),
};
let (fg_color, bg_color) = {
let (fg_color, bg_color, bg_is_default) = {
let mut fg = fg_color;
let mut bg = bg_color;
let mut bg_default = bg_is_default;
if attrs.reverse() {
std::mem::swap(&mut fg, &mut bg);
bg_default = false;
}
(fg, bg)
(fg, bg, bg_default)
};
let glyph_color = rgbcolor_to_window_color(fg_color);
let bg_color = rgbcolor_alpha_to_window_color(
bg_color,
if self.window_background.is_none()
&& params.config.window_background_opacity == 1.0
{
0xff
if window_is_transparent && bg_is_default {
0x00
} else {
(params.config.window_background_tint * 255.0) as u8
(params.config.text_background_opacity * 255.0) as u8
},
);
@ -2945,7 +2944,11 @@ impl TermWindow {
fg_color: params.foreground,
bg_color: rgbcolor_alpha_to_window_color(
params.palette.resolve_bg(ColorAttribute::Default),
(params.config.window_background_tint * 255.0) as u8,
if window_is_transparent {
0x00
} else {
(params.config.text_background_opacity * 255.0) as u8
},
),
palette: params.palette,
is_active_pane: params.pos.is_active,

View File

@ -46,11 +46,6 @@ void main() {
if (o_has_color == 2.0) {
// Background image takes up its full coordinates
gl_Position = projection * vec4(position, 0.0, 1.0);
} else if (!has_background_image) {
// If there is no background attachment, then we're
// rendering the cell background color and need to
// fill the cell with that color
gl_Position = projection * vec4(position, 0.0, 1.0);
} else {
// Nothing else should render on the background layer
gl_Position = off_screen();