diff --git a/color-types/src/lib.rs b/color-types/src/lib.rs index 33960f89c..234e6a614 100644 --- a/color-types/src/lib.rs +++ b/color-types/src/lib.rs @@ -538,6 +538,19 @@ impl SrgbaTuple { pub fn contrast_ratio(&self, other: &Self) -> f32 { self.to_linear().contrast_ratio(&other.to_linear()) } + + /// Assuming that `self` represents the foreground color + /// and `other` represents the background color, if the + /// contrast ratio is below min_ratio, returns Some color + /// that equals or exceeds the min_ratio to use as an alternative + /// foreground color. + /// If the ratio is already suitable, returns None; the caller should + /// continue to use `self` as the foreground color. + pub fn ensure_contrast_ratio(&self, other: &Self, min_ratio: f32) -> Option { + self.to_linear() + .ensure_contrast_ratio(&other.to_linear(), min_ratio) + .map(|linear| linear.to_srgb()) + } } /// Convert an RGB color space hue angle to an RYB colorspace hue angle @@ -886,16 +899,101 @@ impl LinearRgba { } pub fn contrast_ratio(&self, other: &Self) -> f32 { - let l_a = self.relative_luminance(); - let l_b = other.relative_luminance(); - let a = l_a + 0.05; - let b = l_b + 0.05; + let lum_a = self.relative_luminance(); + let lum_b = other.relative_luminance(); + Self::lum_contrast_ratio(lum_a, lum_b) + } + + fn lum_contrast_ratio(lum_a: f32, lum_b: f32) -> f32 { + let a = lum_a + 0.05; + let b = lum_b + 0.05; if a > b { a / b } else { b / a } } + + fn to_oklaba(&self) -> [f32; 4] { + let (r, g, b, alpha) = (self.0, self.1, self.2, self.3); + let l_ = (0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b).cbrt(); + let m_ = (0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b).cbrt(); + let s_ = (0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b).cbrt(); + let l = 0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_; + let a = 1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_; + let b = 0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_; + [l, a, b, alpha] + } + + fn from_oklaba(l: f32, a: f32, b: f32, alpha: f32) -> Self { + let l_ = (l + 0.3963377774 * a + 0.2158037573 * b).powi(3); + let m_ = (l - 0.1055613458 * a - 0.0638541728 * b).powi(3); + let s_ = (l - 0.0894841775 * a - 1.2914855480 * b).powi(3); + + let r = 4.0767416621 * l_ - 3.3077115913 * m_ + 0.2309699292 * s_; + let g = -1.2684380046 * l_ + 2.6097574011 * m_ - 0.3413193965 * s_; + let b = -0.0041960863 * l_ - 0.7034186147 * m_ + 1.7076147010 * s_; + + Self(r, g, b, alpha) + } + + /// Assuming that `self` represents the foreground color + /// and `other` represents the background color, if the + /// contrast ratio is below min_ratio, returns Some color + /// that equals or exceeds the min_ratio to use as an alternative + /// foreground color. + /// If the ratio is already suitable, returns None; the caller should + /// continue to use `self` as the foreground color. + pub fn ensure_contrast_ratio(&self, other: &Self, min_ratio: f32) -> Option { + if self == other { + // Intentionally the same color, don't try to fixup + return None; + } + + let fg_lum = self.relative_luminance(); + let bg_lum = other.relative_luminance(); + let ratio = Self::lum_contrast_ratio(fg_lum, bg_lum); + if ratio >= min_ratio { + // Already has desired ratio or better + return None; + } + + let [_fg_l, fg_a, fg_b, fg_alpha] = self.to_oklaba(); + + let reduced_lum = ((bg_lum + 0.05) / min_ratio - 0.05).clamp(0.05, 1.0); + let reduced_col = Self::from_oklaba(reduced_lum, fg_a, fg_b, fg_alpha); + let reduced_ratio = reduced_col.contrast_ratio(other); + + let increased_lum = ((bg_lum + 0.05) * min_ratio - 0.05).clamp(0.05, 1.0); + let increased_col = Self::from_oklaba(increased_lum, fg_a, fg_b, fg_alpha); + let increased_ratio = reduced_col.contrast_ratio(other); + + // Prefer the reduced luminance version if the fg is dimmer than bg + if fg_lum < bg_lum { + if reduced_ratio >= min_ratio { + return Some(reduced_col); + } + } + // Otherwise, let's find a satisfactory alternative + if increased_ratio >= min_ratio { + return Some(increased_col); + } + if reduced_ratio >= min_ratio { + return Some(reduced_col); + } + + // Didn't find one that satifies the min_ratio, but did we find + // one that is better than the existing ratio? + if reduced_ratio > ratio { + return Some(reduced_col); + } + if increased_ratio > ratio { + return Some(increased_col); + } + + // What they had was as good as it gets + None + } } #[cfg(test)] diff --git a/config/src/config.rs b/config/src/config.rs index e9f74223f..f40dafa68 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -639,6 +639,9 @@ pub struct Config { #[dynamic(default = "default_anim_fps")] pub animation_fps: u8, + #[dynamic(default)] + pub text_min_contrast_ratio: Option, + #[dynamic(default)] pub force_reverse_video_cursor: bool, #[dynamic(default = "default_reverse_video_cursor_min_contrast")] diff --git a/docs/changelog.md b/docs/changelog.md index 6d9c3f44d..ab2d84f96 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -122,6 +122,10 @@ As features stabilize some brief notes about them will accumulate here. * [kde_window_background_blur](config/lua/config/kde_window_background_blur.md) option to enable window blur when running under KDE Plasma on Wayland systems. Thanks to @psomani16k! #6905 +* [reverse_video_cursor_min_contrast](config/lua/config/reverse_video_cursor_min_contrast.md) + option. Thanks to @jameshurst! #6584 ?2861 +* [text_min_contrast_ratio](config/lua/config/text_min_contrast_ratio.md) to more generally + improve the contrast ratio for text in the terminal. #### Fixed * Race condition when very quickly adjusting font scale, and other improvements @@ -225,8 +229,6 @@ As features stabilize some brief notes about them will accumulate here. * Pixel aliasing issue when using [window_content_alignment](config/lua/config/window_content_alignment.md) = `Center`. Thanks to @juster-0! #6929 #6928 #6823 -* [reverse_video_cursor_min_contrast](config/lua/config/reverse_video_cursor_min_contrast.md) - option. Thanks to @jameshurst! #6584 ?2861 #### Updated * Bundled conpty.dll and OpenConsole.exe to build 1.22.250204002.nupkg diff --git a/docs/config/lua/config/text_min_contrast_ratio.md b/docs/config/lua/config/text_min_contrast_ratio.md new file mode 100644 index 000000000..43ed543cf --- /dev/null +++ b/docs/config/lua/config/text_min_contrast_ratio.md @@ -0,0 +1,39 @@ +--- +tags: + - appearance +--- +# `text_min_contrast_ratio = nil` + +{{since('nightly')}} + +An optional floating point value that defaults to `nil` (not being set). + +When set, it defines the minimum contrast ratio between the foreground +and background color of a textual cell, when the cell has differing +foreground and background colors. + +If the color attributes of a cell fall below the minimum contrast ratio, then +the foreground color will have its luminance (perceived brightness) adjusted to +try to boost the contrast ratio to meet the specified minimum. + +This may cause the foreground color to either increase or decrease in +luminance, typically making it either more white or more black. + +It may not be possible to achieve the requested minimum ratio, in which +case a slightly better or the original color (if no better color can +be computed) will be used. + +If the foreground and background colors in a cell are identical, then +that is assumed to be deliberate and the colors will remain unchanged +by this setting. + +[WCAG 2.0 level AA](https://www.w3.org/WAI/WCAG2AA-Conformance) requires a +contrast ratio of at least 4.5:1 for normal text, so setting +`text_min_contrast_ratio = 4.5` is a reasonable value if you find that your +selected color scheme has poor contrast in the applications that you run in +your terminal. + +In the screeshot below, the upper portion shows the default wezterm color scheme, +while the lower portion shows how it renders with `text_min_contrast_ratio = 4.5`. + +![Screenshot](../../../screenshots/wezterm-min-contrast-ratio.png) diff --git a/docs/screenshots/wezterm-min-contrast-ratio.png b/docs/screenshots/wezterm-min-contrast-ratio.png new file mode 100644 index 000000000..c5be9e53f Binary files /dev/null and b/docs/screenshots/wezterm-min-contrast-ratio.png differ diff --git a/wezterm-gui/src/termwindow/render/mod.rs b/wezterm-gui/src/termwindow/render/mod.rs index 881606e2e..dccdd6328 100644 --- a/wezterm-gui/src/termwindow/render/mod.rs +++ b/wezterm-gui/src/termwindow/render/mod.rs @@ -520,6 +520,15 @@ impl crate::TermWindow { Ok(()) } + fn ensure_min_contrast(&self, fg_color: LinearRgba, bg_color: LinearRgba) -> LinearRgba { + match self.config.text_min_contrast_ratio { + Some(ratio) => fg_color + .ensure_contrast_ratio(&bg_color, ratio) + .unwrap_or(fg_color), + None => fg_color, + } + } + pub fn compute_cell_fg_bg(&self, params: ComputeCellFgBgParams) -> ComputeCellFgBgResult { if params.cursor.is_some() { if let Some(bg_color_mix) = self.get_intensity_if_bell_target_ringing( @@ -533,6 +542,8 @@ impl crate::TermWindow { (params.cursor_fg, params.cursor_bg) }; + let fg_color = self.ensure_min_contrast(fg_color, bg_color); + // interpolate between the background color // and the the target color let bg_color_alt = params @@ -566,6 +577,8 @@ impl crate::TermWindow { (params.cursor_fg, params.cursor_bg) }; + let fg_color = self.ensure_min_contrast(fg_color, bg_color); + let color = params .config .resolved_palette @@ -649,6 +662,8 @@ impl crate::TermWindow { _ => (params.fg_color, params.bg_color, params.cursor_border_color), }; + let fg_color = self.ensure_min_contrast(fg_color, bg_color); + let blinking = params.cursor.is_some() && params.is_active_pane && cursor_shape.is_blinking()