Move APCA contrast from terminal_view to ui utils (#36731)

In prep for using this in the editor search/select highlighting. 

Release Notes:

- N/A
This commit is contained in:
Smit Barmase 2025-08-22 10:17:37 +05:30 committed by GitHub
parent 852439452c
commit e15856a37f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 13 deletions

View file

@ -1,4 +1,3 @@
use crate::color_contrast;
use editor::{CursorLayout, HighlightedRange, HighlightedRangeLine}; use editor::{CursorLayout, HighlightedRange, HighlightedRangeLine};
use gpui::{ use gpui::{
AbsoluteLength, AnyElement, App, AvailableSpace, Bounds, ContentMask, Context, DispatchPhase, AbsoluteLength, AnyElement, App, AvailableSpace, Bounds, ContentMask, Context, DispatchPhase,
@ -27,6 +26,7 @@ use terminal::{
terminal_settings::TerminalSettings, terminal_settings::TerminalSettings,
}; };
use theme::{ActiveTheme, Theme, ThemeSettings}; use theme::{ActiveTheme, Theme, ThemeSettings};
use ui::utils::ensure_minimum_contrast;
use ui::{ParentElement, Tooltip}; use ui::{ParentElement, Tooltip};
use util::ResultExt; use util::ResultExt;
use workspace::Workspace; use workspace::Workspace;
@ -534,7 +534,7 @@ impl TerminalElement {
// Only apply contrast adjustment to non-decorative characters // Only apply contrast adjustment to non-decorative characters
if !Self::is_decorative_character(indexed.c) { if !Self::is_decorative_character(indexed.c) {
fg = color_contrast::ensure_minimum_contrast(fg, bg, minimum_contrast); fg = ensure_minimum_contrast(fg, bg, minimum_contrast);
} }
// Ghostty uses (175/255) as the multiplier (~0.69), Alacritty uses 0.66, Kitty // Ghostty uses (175/255) as the multiplier (~0.69), Alacritty uses 0.66, Kitty
@ -1598,6 +1598,7 @@ pub fn convert_color(fg: &terminal::alacritty_terminal::vte::ansi::Color, theme:
mod tests { mod tests {
use super::*; use super::*;
use gpui::{AbsoluteLength, Hsla, font}; use gpui::{AbsoluteLength, Hsla, font};
use ui::utils::apca_contrast;
#[test] #[test]
fn test_is_decorative_character() { fn test_is_decorative_character() {
@ -1713,7 +1714,7 @@ mod tests {
}; };
// Should have poor contrast // Should have poor contrast
let actual_contrast = color_contrast::apca_contrast(white_fg, light_gray_bg).abs(); let actual_contrast = apca_contrast(white_fg, light_gray_bg).abs();
assert!( assert!(
actual_contrast < 30.0, actual_contrast < 30.0,
"White on light gray should have poor APCA contrast: {}", "White on light gray should have poor APCA contrast: {}",
@ -1721,12 +1722,12 @@ mod tests {
); );
// After adjustment with minimum APCA contrast of 45, should be darker // After adjustment with minimum APCA contrast of 45, should be darker
let adjusted = color_contrast::ensure_minimum_contrast(white_fg, light_gray_bg, 45.0); let adjusted = ensure_minimum_contrast(white_fg, light_gray_bg, 45.0);
assert!( assert!(
adjusted.l < white_fg.l, adjusted.l < white_fg.l,
"Adjusted color should be darker than original" "Adjusted color should be darker than original"
); );
let adjusted_contrast = color_contrast::apca_contrast(adjusted, light_gray_bg).abs(); let adjusted_contrast = apca_contrast(adjusted, light_gray_bg).abs();
assert!(adjusted_contrast >= 45.0, "Should meet minimum contrast"); assert!(adjusted_contrast >= 45.0, "Should meet minimum contrast");
// Test case 2: Dark colors (poor contrast) // Test case 2: Dark colors (poor contrast)
@ -1744,7 +1745,7 @@ mod tests {
}; };
// Should have poor contrast // Should have poor contrast
let actual_contrast = color_contrast::apca_contrast(black_fg, dark_gray_bg).abs(); let actual_contrast = apca_contrast(black_fg, dark_gray_bg).abs();
assert!( assert!(
actual_contrast < 30.0, actual_contrast < 30.0,
"Black on dark gray should have poor APCA contrast: {}", "Black on dark gray should have poor APCA contrast: {}",
@ -1752,16 +1753,16 @@ mod tests {
); );
// After adjustment with minimum APCA contrast of 45, should be lighter // After adjustment with minimum APCA contrast of 45, should be lighter
let adjusted = color_contrast::ensure_minimum_contrast(black_fg, dark_gray_bg, 45.0); let adjusted = ensure_minimum_contrast(black_fg, dark_gray_bg, 45.0);
assert!( assert!(
adjusted.l > black_fg.l, adjusted.l > black_fg.l,
"Adjusted color should be lighter than original" "Adjusted color should be lighter than original"
); );
let adjusted_contrast = color_contrast::apca_contrast(adjusted, dark_gray_bg).abs(); let adjusted_contrast = apca_contrast(adjusted, dark_gray_bg).abs();
assert!(adjusted_contrast >= 45.0, "Should meet minimum contrast"); assert!(adjusted_contrast >= 45.0, "Should meet minimum contrast");
// Test case 3: Already good contrast // Test case 3: Already good contrast
let good_contrast = color_contrast::ensure_minimum_contrast(black_fg, white_fg, 45.0); let good_contrast = ensure_minimum_contrast(black_fg, white_fg, 45.0);
assert_eq!( assert_eq!(
good_contrast, black_fg, good_contrast, black_fg,
"Good contrast should not be adjusted" "Good contrast should not be adjusted"
@ -1788,11 +1789,11 @@ mod tests {
}; };
// With minimum contrast of 0.0, no adjustment should happen // With minimum contrast of 0.0, no adjustment should happen
let no_adjust = color_contrast::ensure_minimum_contrast(white_fg, white_bg, 0.0); let no_adjust = ensure_minimum_contrast(white_fg, white_bg, 0.0);
assert_eq!(no_adjust, white_fg, "No adjustment with min_contrast 0.0"); assert_eq!(no_adjust, white_fg, "No adjustment with min_contrast 0.0");
// With minimum APCA contrast of 15, it should adjust to a darker color // With minimum APCA contrast of 15, it should adjust to a darker color
let adjusted = color_contrast::ensure_minimum_contrast(white_fg, white_bg, 15.0); let adjusted = ensure_minimum_contrast(white_fg, white_bg, 15.0);
assert!( assert!(
adjusted.l < white_fg.l, adjusted.l < white_fg.l,
"White on white should become darker, got l={}", "White on white should become darker, got l={}",
@ -1800,7 +1801,7 @@ mod tests {
); );
// Verify the contrast is now acceptable // Verify the contrast is now acceptable
let new_contrast = color_contrast::apca_contrast(adjusted, white_bg).abs(); let new_contrast = apca_contrast(adjusted, white_bg).abs();
assert!( assert!(
new_contrast >= 15.0, new_contrast >= 15.0,
"Adjusted APCA contrast {} should be >= 15.0", "Adjusted APCA contrast {} should be >= 15.0",

View file

@ -1,4 +1,3 @@
mod color_contrast;
mod persistence; mod persistence;
pub mod terminal_element; pub mod terminal_element;
pub mod terminal_panel; pub mod terminal_panel;

View file

@ -3,12 +3,14 @@
use gpui::App; use gpui::App;
use theme::ActiveTheme; use theme::ActiveTheme;
mod apca_contrast;
mod color_contrast; mod color_contrast;
mod corner_solver; mod corner_solver;
mod format_distance; mod format_distance;
mod search_input; mod search_input;
mod with_rem_size; mod with_rem_size;
pub use apca_contrast::*;
pub use color_contrast::*; pub use color_contrast::*;
pub use corner_solver::{CornerSolver, inner_corner_radius}; pub use corner_solver::{CornerSolver, inner_corner_radius};
pub use format_distance::*; pub use format_distance::*;