Skip to content

Rationalize dark/light mode heuristic #39

@krobelus

Description

@krobelus

I'm implementing dark/light mode detection (reusing only xterm-colors for now since I'm using a different querying implementation).

I'd like to understand what's the significance of the logic in theme_mode() as opposed to using if background.perceived_lightness() { Dark } else { Light }.
I haven't actually tried color values other than 000000/ffffff so maybe it's very obvious.

If I simplify the heuristics as follows, cargo test still passes.

diff --git a/crates/terminal-colorsaurus/src/lib.rs b/crates/terminal-colorsaurus/src/lib.rs
index 2aa77dd..88543fd 100644
--- a/crates/terminal-colorsaurus/src/lib.rs
+++ b/crates/terminal-colorsaurus/src/lib.rs
@@ -111,14 +111,10 @@ pub enum ThemeMode {
 impl ColorPalette {
     /// Determines if the terminal uses a dark or light background.
     pub fn theme_mode(&self) -> ThemeMode {
-        let fg = self.foreground.perceived_lightness();
-        let bg = self.background.perceived_lightness();
-        if bg < fg {
+        if self.background.perceived_lightness() < 0.5 {
             ThemeMode::Dark
-        } else if bg > fg || bg > 0.5 {
-            ThemeMode::Light
         } else {
-            ThemeMode::Dark
+            ThemeMode::Light
         }
     }
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions