From 850cc8339dace45b9dd762c504b15983157b90be Mon Sep 17 00:00:00 2001 From: Paul Otto Date: Thu, 5 Nov 2026 15:46:46 +0100 Subject: [PATCH 1/2] feat(tui): the terminal's colors, and your own in theme.toml From a comment on Reddit: in Ubuntu's purple Ptyxis, cloofy painted its own near-black over the terminal's background. - Settings has Colors: Auto, cloofy and Terminal. Auto is as before: Omarchy's theme where there is one, cloofy's own elsewhere. Terminal takes the terminal's background and text and mixes the shades in between with Omarchy's template, so the rest follows them the way an Omarchy theme does. The accent stays cloofy's green unless chosen. - cloofy asks for the colors (OSC 10 and 11) along with the image protocol at start, so it costs no time. A terminal that doesn't answer keeps cloofy's colors, and Settings says why. - ~/.config/cloofy/theme.toml takes the keys of a theme's cloofy.toml and goes over whichever colors are chosen. cloofy follows it as it changes, like Omarchy's theme. - colors.rs decides the palette; App polls it on every tick in place of the Omarchy watch in the event loop. omarchy.rs builds on every platform now, since its template also mixes the terminal's colors. - ratatui-image: asks for the text color too, reads color answers with fewer than four digits per channel (rgb:30/0a/24 came out black), and keeps the terminal's answers when it doesn't tell its font size. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 + README.md | 7 +- crates/cloofy-core/src/config.rs | 3 + crates/cloofy-tui/src/app/browsing.rs | 2 +- crates/cloofy-tui/src/app/mod.rs | 9 + crates/cloofy-tui/src/app/settings.rs | 22 +- crates/cloofy-tui/src/colors.rs | 256 ++++++++++++++++++++++ crates/cloofy-tui/src/graphics.rs | 34 ++- crates/cloofy-tui/src/harness.rs | 4 +- crates/cloofy-tui/src/lib.rs | 22 +- crates/cloofy-tui/src/omarchy.rs | 19 ++ crates/cloofy-tui/src/preferences.rs | 55 ++++- crates/cloofy-tui/src/theme.rs | 13 +- crates/cloofy-tui/src/ui/settings.rs | 5 +- vendor/ratatui-image | 2 +- website/src/content/docs/docs/settings.md | 24 +- 16 files changed, 434 insertions(+), 48 deletions(-) create mode 100644 crates/cloofy-tui/src/colors.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 9518b94..58f2e2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to cloofy. The format follows [Keep a Changelog](https://kee ## [Unreleased] +### Added + +- **Your terminal's colors.** Settings has Colors now. **Terminal** takes your terminal's background and text, with the shades in between mixed from them, so cloofy looks at home in a purple Ptyxis on Ubuntu or any other theme. **Auto** stays as before: Omarchy's theme there, cloofy's own dark colors elsewhere, which **cloofy** keeps everywhere. +- **Your own colors.** Any of cloofy's colors in `~/.config/cloofy/theme.toml`, such as `bg = "#2b0a3d"`, go over the others. cloofy follows the file as you edit it. + ### Changed - **Rust 1.90** or newer builds cloofy from source, for the current D-Bus library on Linux and the library that draws Sixel images. diff --git a/README.md b/README.md index 57403ff..a33d3ba 100644 --- a/README.md +++ b/README.md @@ -220,13 +220,14 @@ Installed with Homebrew, mise, a system package or Nix, cloofy leaves its update ## Settings -,, the command palette, or a click on your name in the top bar opens the Settings page: crossfade, cover art and how covers are drawn, the accent color, icons, power saving, song notifications and update checks. Changes apply at once and cloofy saves them in `~/.config/cloofy/config.toml`, so there's no need to edit the file. You still can; it's read at start: +,, the command palette, or a click on your name in the top bar opens the Settings page: crossfade, cover art and how covers are drawn, the colors, the accent color, icons, power saving, song notifications and update checks. Changes apply at once and cloofy saves them in `~/.config/cloofy/config.toml`, so there's no need to edit the file. You still can; it's read at start: ```toml client_id = "your client id" services = ["ytmusic"] # "spotify", "ytmusic" or both, in the switch's order; every one built in when missing crossfade = 6 # seconds tracks overlap, 0 to 12; off when missing covers = false # no cover art, for slower computers; on when missing +colors = "terminal" # "cloofy" or "terminal"; Omarchy's theme there, cloofy's own elsewhere, when missing accent = "violet" # "theme", "green", "violet", "pink", "blue" or "orange"; follows the cover when missing images = "blocks" # "kitty", "sixel", "iterm2" or "blocks"; detected from the terminal when missing icons = "nerd" # "nerd" or "text"; detected from the terminal when missing @@ -236,7 +237,9 @@ update_check = false # don't look for new releases at start; on when missi credentials = "keychain" # macOS: sign-ins in the Keychain instead of files (only here) ``` -The installer writes `services` with what you listen to. One config file serves every build, so a build leaves out the services it doesn't have. The `"theme"` accent is the Omarchy theme's, and cloofy's green elsewhere. +The installer writes `services` with what you listen to. One config file serves every build, so a build leaves out the services it doesn't have. The `"theme"` accent is the Omarchy theme's or `theme.toml`'s, and cloofy's green elsewhere. + +cloofy draws with its own dark colors, or with Omarchy's theme. With Colors set to Terminal, it takes your terminal's background and text instead, with the shades in between. To pick colors yourself, put any of `bg`, `surface`, `player`, `raised`, `text`, `muted`, `faint`, `accent` and the [others](integrations/omarchy/cloofy.toml.tpl) in `~/.config/cloofy/theme.toml`, as `bg = "#2b0a3d"`; they go over whichever colors are set, and cloofy follows the file as you edit it. Power saving stops animations and redraws less often. Unless set otherwise, it turns on by itself with Low Power Mode on macOS and the Power Saver profile on Linux. diff --git a/crates/cloofy-core/src/config.rs b/crates/cloofy-core/src/config.rs index 6d6619c..19f0520 100644 --- a/crates/cloofy-core/src/config.rs +++ b/crates/cloofy-core/src/config.rs @@ -28,6 +28,9 @@ pub struct Config { pub power_saving: Option, /// A color name such as `"violet"`; follows the playing cover when missing. pub accent: Option, + /// `"cloofy"` or `"terminal"`: cloofy's own colors, or the terminal's background and text. + /// Omarchy's theme where there is one, cloofy's own elsewhere, when missing. + pub colors: Option, /// How covers reach the terminal: `"kitty"`, `"sixel"`, `"iterm2"` or `"blocks"`; detected /// from the terminal when missing. pub images: Option, diff --git a/crates/cloofy-tui/src/app/browsing.rs b/crates/cloofy-tui/src/app/browsing.rs index 7c14fd0..3c2015b 100644 --- a/crates/cloofy-tui/src/app/browsing.rs +++ b/crates/cloofy-tui/src/app/browsing.rs @@ -368,7 +368,7 @@ mod tests { Paths { config: root.join("config"), data: root.join("data"), cache: root.join("cache"), keychain: false }; let (tx, _) = mpsc::unbounded_channel(); let loader = Loader::for_tests(services, problems, tx); - App::new(loader, Preferences::default(), Start::Restore(Box::new(state)), paths, None, None) + App::new(loader, Preferences::default(), Start::Restore(Box::new(state)), paths, None, None, None) } fn page(view: SavedView) -> Page { diff --git a/crates/cloofy-tui/src/app/mod.rs b/crates/cloofy-tui/src/app/mod.rs index e920ce1..063dfe1 100644 --- a/crates/cloofy-tui/src/app/mod.rs +++ b/crates/cloofy-tui/src/app/mod.rs @@ -35,6 +35,7 @@ use ratatui::{ use crate::clock::Instant; use crate::{ backend::{self, Player, Remote, Service}, + colors::Colors, commands::Command, fx::{Effect, Effects}, graphics::{INLINE_CELL, ImageProtocol, InlineLayer}, @@ -483,6 +484,8 @@ pub struct App { /// The system's settings with the user's choice for power saving applied. pub settings: Settings, settings_checked: Instant, + /// Where the palette comes from; `None` in tests, which keep cloofy's own colors. + colors: Option, pub accent: Accent, /// The color of the playing cover, for when the accent follows it. cover_accent: Option, @@ -605,8 +608,13 @@ impl App { paths: Paths, media: Option, notifier: Option, + mut colors: Option, ) -> Self { let system_settings = system_settings(); + // The palette from the first frame on, and the accent the app starts with. + if let Some(palette) = colors.as_mut().and_then(Colors::poll) { + theme::set_palette(palette); + } let mut accent = Accent::new(); if let Some(color) = prefs.accent.fixed() { accent.jump(color); @@ -656,6 +664,7 @@ impl App { slept_at: None, system_settings, settings_checked: Instant::now(), + colors, accent, cover_accent: None, me: None, diff --git a/crates/cloofy-tui/src/app/settings.rs b/crates/cloofy-tui/src/app/settings.rs index 2b54189..1c5d732 100644 --- a/crates/cloofy-tui/src/app/settings.rs +++ b/crates/cloofy-tui/src/app/settings.rs @@ -8,6 +8,7 @@ use crossterm::event::{KeyCode, KeyEvent}; use super::{App, COVER_SIZE, Focus, ImageSlot}; use crate::{ backend, + colors::Colors, commands::Command, preferences::{AccentChoice, Setting}, theme, @@ -88,6 +89,12 @@ impl App { } Setting::Covers => self.drop_covers(), Setting::Images => self.set_image_protocol(self.prefs.images), + Setting::Colors => { + if let Some(colors) = &mut self.colors { + colors.set_choice(self.prefs.colors); + } + self.poll_colors(); + } Setting::Accent => self.show_accent(), Setting::Icons => crate::icons::init(self.prefs.icons.config()), Setting::PowerSaving => self.settings = self.prefs.apply(self.system_settings), @@ -111,12 +118,19 @@ impl App { } } - /// Draws with the system theme's `palette` from the next frame on. The accent goes along - /// where it comes from the palette, and the fixed ones adapt to a light background. - #[cfg(target_os = "linux")] - pub fn set_palette(&mut self, palette: theme::Palette) { + /// Draws with new colors from the next frame on when they changed, by the setting, Omarchy's + /// theme or `theme.toml`. The accent goes along where it comes from the palette, and the + /// fixed ones adapt to a light background. Returns whether the colors changed. + pub fn poll_colors(&mut self) -> bool { + let Some(palette) = self.colors.as_mut().and_then(Colors::poll) else { return false }; theme::set_palette(palette); self.show_accent(); + true + } + + /// Whether the terminal told its colors, for the Terminal choice. + pub fn knows_terminal_colors(&self) -> bool { + self.colors.as_ref().is_some_and(Colors::knows_terminal) } /// Writes `setting` to the config file. The demo keeps changes until it closes. diff --git a/crates/cloofy-tui/src/colors.rs b/crates/cloofy-tui/src/colors.rs new file mode 100644 index 0000000..594abe4 --- /dev/null +++ b/crates/cloofy-tui/src/colors.rs @@ -0,0 +1,256 @@ +//! Where the interface's colors come from: cloofy's own, Omarchy's theme or the terminal's, as +//! the Colors setting says, with `theme.toml` from the config folder over them. Follows +//! Omarchy's theme and the file as they change, without a restart. + +use std::{ + fs, + path::{Path, PathBuf}, + time::{Duration, Instant, SystemTime}, +}; + +use ratatui_image::picker::Capability; + +use crate::{ + omarchy, + preferences::ColorChoice, + theme::{Palette, Rgb}, +}; + +/// How often to look at `theme.toml` again. +const CHECK_INTERVAL: Duration = Duration::from_secs(1); + +/// What the terminal answered at startup when asked for its colors. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct TerminalColors { + pub background: Rgb, + /// Some terminals tell only their background. + pub foreground: Option, +} + +impl TerminalColors { + /// The colors among what the terminal answered; `None` without its background. + pub fn from_capabilities(capabilities: &[Capability]) -> Option { + let background = capabilities.iter().find_map(|capability| match capability { + Capability::Background(r, g, b) => Some(Rgb(*r, *g, *b)), + _ => None, + })?; + let foreground = capabilities.iter().find_map(|capability| match capability { + Capability::Foreground(r, g, b) => Some(Rgb(*r, *g, *b)), + _ => None, + }); + Some(Self { background, foreground }) + } + + /// The palette for these colors: shades mixed like an Omarchy theme's. Without the text + /// color, cloofy's own text on a dark background and its background color on a light one. + fn palette(self) -> Palette { + let foreground = self.foreground.unwrap_or(if self.background.is_light() { + Rgb::of(Palette::DEFAULT.bg) + } else { + Rgb::of(Palette::DEFAULT.text) + }); + omarchy::terminal_palette(self.background, foreground) + } +} + +/// The palette to draw with, and when it changes. +pub struct Colors { + choice: ColorChoice, + /// The terminal's palette, when it told its colors. + terminal: Option, + /// Omarchy's theme and its palette as last read; `None` without Omarchy. + omarchy: Option<(omarchy::ThemeWatch, Option)>, + custom: CustomFile, + /// Whether the palette may differ from the one [`Colors::poll`] returned last. + stale: bool, + shown: Option, +} + +impl Colors { + /// `config` is the folder `theme.toml` is looked for in. + pub fn new(choice: ColorChoice, terminal: Option, config: &Path) -> Self { + let omarchy = if cfg!(target_os = "linux") { omarchy::ThemeWatch::new() } else { None }; + Self { + choice, + terminal: terminal.map(TerminalColors::palette), + omarchy: omarchy.map(|watch| (watch, None)), + custom: CustomFile::new(config.join("theme.toml")), + stale: true, + shown: None, + } + } + + pub fn set_choice(&mut self, choice: ColorChoice) { + self.stale |= choice != self.choice; + self.choice = choice; + } + + /// Whether the terminal told its colors at startup. + pub fn knows_terminal(&self) -> bool { + self.terminal.is_some() + } + + /// The palette the first time, then again whenever it changed: by the setting, Omarchy's + /// theme or `theme.toml`. Looks at the files at most every second, so it may be called on + /// every tick. + pub fn poll(&mut self) -> Option { + if let Some((watch, palette)) = &mut self.omarchy + && let Some(theme) = watch.poll() + { + *palette = Some(theme); + self.stale = true; + } + self.stale |= self.custom.poll(); + if !std::mem::take(&mut self.stale) { + return None; + } + let palette = self.palette(); + (self.shown != Some(palette)).then(|| { + self.shown = Some(palette); + palette + }) + } + + fn palette(&self) -> Palette { + let omarchy = self.omarchy.as_ref().and_then(|(_, palette)| *palette); + let base = match self.choice { + ColorChoice::Auto => omarchy, + ColorChoice::Cloofy => None, + ColorChoice::Terminal => self.terminal, + }; + let base = base.unwrap_or(Palette::DEFAULT); + match &self.custom.text { + Some(text) => omarchy::apply(text, base), + None => base, + } + } +} + +/// `theme.toml`, read again whenever it changes. +struct CustomFile { + path: PathBuf, + checked: Option, + /// When the file last changed, as of the last look; `None` before the first look. + seen: Option>, + text: Option, +} + +impl CustomFile { + fn new(path: PathBuf) -> Self { + Self { path, checked: None, seen: None, text: None } + } + + /// Whether the file changed since the last look, which is at most every [`CHECK_INTERVAL`]. + fn poll(&mut self) -> bool { + if self.checked.is_some_and(|at| at.elapsed() < CHECK_INTERVAL) { + return false; + } + self.checked = Some(Instant::now()); + let modified = fs::metadata(&self.path).and_then(|m| m.modified()).ok(); + if self.seen == Some(modified) { + return false; + } + self.seen = Some(modified); + let text = fs::read_to_string(&self.path).ok(); + let changed = text != self.text; + self.text = text; + changed + } +} + +#[cfg(test)] +mod tests { + use ratatui::style::Color; + + use super::*; + + /// Ubuntu's terminal: aubergine with white text. + const UBUNTU: TerminalColors = + TerminalColors { background: Rgb(0x30, 0x0a, 0x24), foreground: Some(Rgb(255, 255, 255)) }; + + fn folder(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("cloofy-colors-{name}-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + dir + } + + fn colors(choice: ColorChoice, terminal: Option, config: &Path) -> Colors { + let mut colors = Colors::new(choice, terminal, config); + // Whatever theme the machine running the tests has. + colors.omarchy = None; + colors + } + + #[test] + fn reads_the_terminal_s_answer() { + let answer = [Capability::Sixel, Capability::Foreground(1, 2, 3), Capability::Background(4, 5, 6)]; + assert_eq!( + TerminalColors::from_capabilities(&answer), + Some(TerminalColors { background: Rgb(4, 5, 6), foreground: Some(Rgb(1, 2, 3)) }) + ); + assert_eq!(TerminalColors::from_capabilities(&[Capability::Foreground(1, 2, 3)]), None); + } + + #[test] + fn the_terminal_s_colors_only_when_chosen() { + let dir = folder("choice"); + let mut colors = colors(ColorChoice::Auto, Some(UBUNTU), &dir); + assert_eq!(colors.poll(), Some(Palette::DEFAULT)); + assert_eq!(colors.poll(), None, "nothing changed"); + + colors.set_choice(ColorChoice::Terminal); + let palette = colors.poll().unwrap(); + assert_eq!(palette.bg, Color::Rgb(0x30, 0x0a, 0x24)); + assert_eq!(palette.text, Color::Rgb(255, 255, 255)); + assert_eq!(palette.accent, Palette::DEFAULT.accent, "cloofy's green"); + assert!(!palette.is_light()); + + colors.set_choice(ColorChoice::Cloofy); + assert_eq!(colors.poll(), Some(Palette::DEFAULT)); + fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn a_terminal_that_tells_only_its_background() { + let light = TerminalColors { background: Rgb(250, 250, 250), foreground: None }; + let palette = light.palette(); + assert!(palette.is_light()); + assert_eq!(palette.text, Palette::DEFAULT.bg, "dark text on a light background"); + let dark = TerminalColors { background: Rgb(0, 0, 0), foreground: None }; + assert_eq!(dark.palette().text, Palette::DEFAULT.text); + } + + #[test] + fn a_terminal_that_tells_nothing_keeps_cloofy_s_colors() { + let dir = folder("silent"); + let mut colors = colors(ColorChoice::Terminal, None, &dir); + assert!(!colors.knows_terminal()); + assert_eq!(colors.poll(), Some(Palette::DEFAULT)); + fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn theme_toml_goes_over_any_of_them_and_is_followed() { + let dir = folder("custom"); + let file = dir.join("theme.toml"); + fs::write(&file, "bg = \"#2b0a3d\"\n# text = \"#000000\"\n").unwrap(); + let mut colors = colors(ColorChoice::Terminal, Some(UBUNTU), &dir); + let palette = colors.poll().unwrap(); + assert_eq!(palette.bg, Color::Rgb(0x2b, 0x0a, 0x3d)); + assert_eq!(palette.text, Color::Rgb(255, 255, 255), "the rest from the terminal"); + + fs::write(&file, "bg = \"#101010\"\naccent = \"#ff8800\"\n").unwrap(); + // The modification time may be the same within the second; a new look reads it. + colors.custom.checked = None; + colors.custom.seen = None; + let palette = colors.poll().unwrap(); + assert_eq!(palette.bg, Color::Rgb(0x10, 0x10, 0x10)); + assert_eq!(palette.accent, Rgb(0xff, 0x88, 0x00)); + + fs::remove_file(&file).unwrap(); + colors.custom.checked = None; + assert_eq!(colors.poll().map(|p| p.bg), Some(Color::Rgb(0x30, 0x0a, 0x24)), "removed: the terminal's again"); + fs::remove_dir_all(dir).unwrap(); + } +} diff --git a/crates/cloofy-tui/src/graphics.rs b/crates/cloofy-tui/src/graphics.rs index 6cfea8e..43a5795 100644 --- a/crates/cloofy-tui/src/graphics.rs +++ b/crates/cloofy-tui/src/graphics.rs @@ -24,10 +24,10 @@ use ratatui::{ }; use ratatui_image::{ FontSize, - picker::{Capability, Picker, ProtocolType}, + picker::{Capability, Picker, ProtocolType, cap_parser::QueryStdioOptions}, }; -use crate::snapshot::Placement; +use crate::{colors::TerminalColors, snapshot::Placement}; /// What the cells of a Sixel or iTerm2 image hold while the frame is drawn. Anything drawn over /// the image replaces it, which is how cloofy tells which cells still show the picture. @@ -80,17 +80,18 @@ impl ImageProtocol { } /// What the terminal said about images at startup, kept so that the protocol can change later -/// without asking again. +/// without asking again. Its colors come with the same answer. #[derive(Clone)] pub struct ImageTerminal { picker: Picker, /// What `Auto` draws with. auto: ProtocolType, + colors: Option, } impl ImageTerminal { - /// Asks the terminal how it shows images and how big its cells are. Must run after entering - /// the alternate screen and before reading input events. + /// Asks the terminal how it shows images, how big its cells are and what its colors are. + /// Must run after entering the alternate screen and before reading input events. /// /// When `CLOOFY_IMAGES` forces a protocol, nothing is asked and cells are taken as 10×22 px: /// that is for measuring a protocol where the terminal cannot answer, like tmux in tests. @@ -99,9 +100,16 @@ impl ImageTerminal { // Deprecated because a guessed font size draws images at the wrong size; fine here. #[allow(deprecated)] let picker = Picker::from_fontsize(FontSize { width: 10, height: 22 }); - return Self { picker, auto: ProtocolType::Halfblocks }; + return Self { picker, auto: ProtocolType::Halfblocks, colors: None }; } - let picker = Picker::from_query_stdio().unwrap_or_else(|_| Picker::halfblocks()); + // The colors are in the same question, so they take no extra time. + let options = QueryStdioOptions { + terminal_background_color_osc: true, + terminal_foreground_color_osc: true, + ..QueryStdioOptions::default() + }; + let picker = Picker::from_query_stdio_with_options(options).unwrap_or_else(|_| Picker::halfblocks()); + let colors = TerminalColors::from_capabilities(picker.capabilities()); let cell_size = picker.capabilities().iter().any(|cap| matches!(cap, Capability::CellSize(Some(_)))); let set = |name| std::env::var_os(name).is_some_and(|value| !value.is_empty()); let host = Host { @@ -118,13 +126,19 @@ impl ImageTerminal { font.height, if picker.tmux_detected() { ", in tmux" } else { "" }, ); - Self { picker, auto } + tracing::info!("colors: terminal answered {colors:?}"); + Self { picker, auto, colors } } /// Half blocks, without asking a terminal, for tests. #[cfg(test)] pub fn halfblocks() -> Self { - Self { picker: Picker::halfblocks(), auto: ProtocolType::Halfblocks } + Self { picker: Picker::halfblocks(), auto: ProtocolType::Halfblocks, colors: None } + } + + /// The terminal's background and text colors, if it told them. + pub fn colors(&self) -> Option { + self.colors } /// What `Auto` draws with in this terminal. @@ -433,7 +447,7 @@ mod tests { const FONT: FontSize = FontSize { width: 10, height: 20 }; fn terminal(auto: ProtocolType) -> ImageTerminal { - ImageTerminal { picker: Picker::halfblocks(), auto } + ImageTerminal { picker: Picker::halfblocks(), auto, colors: None } } #[test] diff --git a/crates/cloofy-tui/src/harness.rs b/crates/cloofy-tui/src/harness.rs index 5fb0be8..3e883da 100644 --- a/crates/cloofy-tui/src/harness.rs +++ b/crates/cloofy-tui/src/harness.rs @@ -59,7 +59,7 @@ impl Harness { let images = paths.cache.join("images"); let loader = Loader::new(Vec::new(), service, tx, ImageTerminal::halfblocks(), images, false) .with_demo(Some(demo.clone())); - let app = App::new(loader, Preferences::default(), Start::Demo(&demo), paths, None, None); + let app = App::new(loader, Preferences::default(), Start::Demo(&demo), paths, None, None, None); Self::start(app, events, root, Vec::new(), (width, height)).await } @@ -85,7 +85,7 @@ impl Harness { let images = paths.cache.join("images"); let loader = Loader::new(started, services.first().copied(), tx, ImageTerminal::halfblocks(), images, false); let start = Start::Restore(Box::default()); - let app = App::new(loader, Preferences::default(), start, paths, None, None); + let app = App::new(loader, Preferences::default(), start, paths, None, None, None); Self::start(app, events, root, fakes, (120, 36)).await } diff --git a/crates/cloofy-tui/src/lib.rs b/crates/cloofy-tui/src/lib.rs index f889427..ce5288a 100644 --- a/crates/cloofy-tui/src/lib.rs +++ b/crates/cloofy-tui/src/lib.rs @@ -7,6 +7,7 @@ mod app; mod art; mod backend; mod clock; +mod colors; mod commands; mod datacache; pub mod demo; @@ -18,7 +19,7 @@ mod icons; mod loader; mod media; mod notify; -#[cfg(target_os = "linux")] +// Omarchy is Linux only, but its theme template also colors cloofy from the terminal's colors. pub mod omarchy; mod overlay; mod playback; @@ -230,19 +231,13 @@ async fn run_app( Some(demo) => Service::of(&demo.playing.track.uri), None => started.first().map(|(service, _)| *service), }; + let colors = colors::Colors::new(prefs.colors, images.colors(), &paths.config); let loader = Loader::new(started, service, tx.clone(), images, paths.cache.join("images"), snapshotter.is_some()) .with_demo(demo.clone()); loader.set_image_protocol(image_protocol); loader.prune_image_cache(); let update_check = prefs.update_check && demo.is_none(); - // Omarchy's colors from the first frame on, and the accent the app starts with. - #[cfg(target_os = "linux")] - let mut system_theme = omarchy::ThemeWatch::new(); - #[cfg(target_os = "linux")] - if let Some(palette) = system_theme.as_mut().and_then(omarchy::ThemeWatch::poll) { - theme::set_palette(palette); - } - let mut app = App::new(loader, prefs, start, paths.clone(), media, notifier); + let mut app = App::new(loader, prefs, start, paths.clone(), media, notifier, Some(colors)); if update_check { app.check_for_update(false); } @@ -284,14 +279,7 @@ async fn run_app( } _ = ticker.tick() => app.on_tick(), }; - #[cfg(target_os = "linux")] - let redraw = match system_theme.as_mut().and_then(omarchy::ThemeWatch::poll) { - Some(palette) => { - app.set_palette(palette); - true - } - None => redraw, - }; + let redraw = app.poll_colors() || redraw; app.sync_media(); app.announce_track(); if ticker.period() != ticker_period(app.settings.low_power) { diff --git a/crates/cloofy-tui/src/omarchy.rs b/crates/cloofy-tui/src/omarchy.rs index 8ad53f8..9b334ca 100644 --- a/crates/cloofy-tui/src/omarchy.rs +++ b/crates/cloofy-tui/src/omarchy.rs @@ -86,6 +86,25 @@ pub fn palette(cloofy: Option<&str>, colors: &str) -> Palette { } } +/// The palette for a terminal's own background and text: the shades mixed by the same template +/// as a theme's, with cloofy's accent and red. +pub fn terminal_palette(background: Rgb, foreground: Rgb) -> Palette { + let variables = [ + ("background", background), + ("foreground", foreground), + ("bright_foreground", foreground), + ("accent", Palette::DEFAULT.accent), + ("red", Rgb::of(Palette::DEFAULT.danger)), + ]; + let variables = variables.into_iter().map(|(key, color)| (key.to_owned(), color.hex())).collect(); + read_palette(&render(TEMPLATE, &variables), Palette::DEFAULT) +} + +/// `base` with the colors a `cloofy.toml`-style file such as `theme.toml` names. +pub fn apply(text: &str, base: Palette) -> Palette { + read_palette(text, base) +} + /// `colors.toml` as the variables templates use, with the fallbacks Omarchy applies for the /// ones templates here need, so older themes that name only ANSI colors work too. pub fn variables(colors: &str) -> HashMap { diff --git a/crates/cloofy-tui/src/preferences.rs b/crates/cloofy-tui/src/preferences.rs index 846db3c..f54d201 100644 --- a/crates/cloofy-tui/src/preferences.rs +++ b/crates/cloofy-tui/src/preferences.rs @@ -17,6 +17,7 @@ pub struct Preferences { pub crossfade: u64, pub covers: bool, pub images: ImageProtocol, + pub colors: ColorChoice, pub accent: AccentChoice, pub icons: IconChoice, pub power_saving: PowerSaving, @@ -36,6 +37,11 @@ impl Preferences { crossfade: config.crossfade.unwrap_or(0).min(12), covers: config.covers != Some(false), images: config.images.as_deref().and_then(ImageProtocol::parse).unwrap_or_default(), + colors: match config.colors.as_deref() { + Some("cloofy") => ColorChoice::Cloofy, + Some("terminal") => ColorChoice::Terminal, + _ => ColorChoice::Auto, + }, accent: AccentChoice::from_config(config.accent.as_deref()), icons: match config.icons.as_deref() { Some("nerd") => IconChoice::Nerd, @@ -70,6 +76,7 @@ impl Preferences { Setting::Crossfade => format!("{} s", self.crossfade), Setting::Covers => on_off(self.covers), Setting::Images => self.images.label().to_owned(), + Setting::Colors => self.colors.label().to_owned(), Setting::Accent => self.accent.label().to_owned(), Setting::Icons => self.icons.label().to_owned(), Setting::PowerSaving => self.power_saving.label().to_owned(), @@ -84,7 +91,12 @@ impl Preferences { Setting::Covers => Some(self.covers), Setting::Notifications => Some(self.notifications), Setting::UpdateCheck => Some(self.update_check), - Setting::Crossfade | Setting::Images | Setting::Accent | Setting::Icons | Setting::PowerSaving => None, + Setting::Crossfade + | Setting::Images + | Setting::Colors + | Setting::Accent + | Setting::Icons + | Setting::PowerSaving => None, } } @@ -94,6 +106,7 @@ impl Preferences { Setting::Crossfade => self.crossfade = cycle(&CROSSFADE_STEPS, &self.crossfade, forward), Setting::Covers => self.covers = !self.covers, Setting::Images => self.images = cycle(&ImageProtocol::ALL, &self.images, forward), + Setting::Colors => self.colors = cycle(&ColorChoice::ALL, &self.colors, forward), Setting::Accent => self.accent = cycle(&AccentChoice::ALL, &self.accent, forward), Setting::Icons => self.icons = cycle(&IconChoice::ALL, &self.icons, forward), Setting::PowerSaving => self.power_saving = cycle(&PowerSaving::ALL, &self.power_saving, forward), @@ -113,6 +126,7 @@ impl Preferences { "images", (self.images != ImageProtocol::Auto).then(|| Value::from(self.images.label().to_lowercase())), ), + Setting::Colors => ("colors", self.colors.config().map(Value::from)), Setting::Accent => ("accent", self.accent.config().map(Value::from)), Setting::Icons => ("icons", self.icons.config().map(Value::from)), Setting::PowerSaving => ("power_saving", self.power_saving.config().map(Value::from)), @@ -135,6 +149,7 @@ pub enum Setting { Crossfade, Covers, Images, + Colors, Accent, Icons, PowerSaving, @@ -145,7 +160,7 @@ pub enum Setting { /// The Settings page: section titles with their settings, in order. pub const SECTIONS: [(&str, &[Setting]); 4] = [ ("Playback", &[Setting::Crossfade]), - ("Display", &[Setting::Covers, Setting::Images, Setting::Accent, Setting::Icons]), + ("Display", &[Setting::Covers, Setting::Images, Setting::Colors, Setting::Accent, Setting::Icons]), ("Power", &[Setting::PowerSaving]), ("Notifications and updates", &[Setting::Notifications, Setting::UpdateCheck]), ]; @@ -161,6 +176,7 @@ impl Setting { Setting::Crossfade => "Crossfade", Setting::Covers => "Cover art", Setting::Images => "Cover images", + Setting::Colors => "Colors", Setting::Accent => "Accent color", Setting::Icons => "Icons", Setting::PowerSaving => "Power saving", @@ -174,6 +190,7 @@ impl Setting { Setting::Crossfade => "Let songs fade into each other.", Setting::Covers => "Turn off to save memory and processor time on slower computers.", Setting::Images => "How covers reach the terminal. Blocks work in every terminal.", + Setting::Colors => "Auto follows Omarchy's theme; Terminal takes the terminal's background and text.", Setting::Accent => "Follows the cover of the playing song or the system's theme, or stays one color.", Setting::Icons => "Text icons work with every font; Nerd Font icons need one installed.", Setting::PowerSaving => "Stops animations and redraws less often. Auto follows the system.", @@ -187,7 +204,7 @@ impl Setting { pub enum AccentChoice { /// The dominant color of the playing cover. Cover, - /// The palette's accent, which Omarchy's theme sets; cloofy's green elsewhere. + /// The palette's accent, which Omarchy's theme or `theme.toml` sets; cloofy's green elsewhere. Theme, Fixed(NamedColor), } @@ -276,6 +293,38 @@ impl NamedColor { } } +/// Where the interface's colors come from. `theme.toml` in the config folder goes over any of them. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ColorChoice { + /// Omarchy's theme where there is one, cloofy's own colors elsewhere. + Auto, + /// cloofy's own dark colors, also on Omarchy. + Cloofy, + /// The terminal's background and text, with the shades in between; cloofy's own colors + /// when the terminal doesn't tell them. + Terminal, +} + +impl ColorChoice { + const ALL: [ColorChoice; 3] = [ColorChoice::Auto, ColorChoice::Cloofy, ColorChoice::Terminal]; + + fn config(self) -> Option<&'static str> { + match self { + ColorChoice::Auto => None, + ColorChoice::Cloofy => Some("cloofy"), + ColorChoice::Terminal => Some("terminal"), + } + } + + fn label(self) -> &'static str { + match self { + ColorChoice::Auto => "Auto", + ColorChoice::Cloofy => "cloofy", + ColorChoice::Terminal => "Terminal", + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum IconChoice { /// Nerd Font icons in terminals that bundle the font, text icons elsewhere. diff --git a/crates/cloofy-tui/src/theme.rs b/crates/cloofy-tui/src/theme.rs index f520281..037a7e2 100644 --- a/crates/cloofy-tui/src/theme.rs +++ b/crates/cloofy-tui/src/theme.rs @@ -69,13 +69,11 @@ impl Palette { /// Whether this is a light theme, with dark text on a light background. pub fn is_light(&self) -> bool { - let Rgb(r, g, b) = Rgb::of(self.bg); - // The way Omarchy tells, by the sum of the channels. - u16::from(r) + u16::from(g) + u16::from(b) > 382 + Rgb::of(self.bg).is_light() } } -/// A lock rather than constants, so the colors can follow the system's theme. Drawing only +/// A lock rather than constants, so the colors can follow the system's theme or the terminal's. Drawing only /// reads it, which costs next to nothing while nobody writes. static PALETTE: RwLock = RwLock::new(Palette::DEFAULT); @@ -86,7 +84,6 @@ pub fn palette() -> Palette { } /// Draws with `palette` from the next frame on. -#[cfg_attr(not(target_os = "linux"), allow(dead_code))] // Only Omarchy's theme feeds it so far. pub fn set_palette(palette: Palette) { *PALETTE.write().unwrap_or_else(PoisonError::into_inner) = palette; } @@ -100,6 +97,12 @@ impl Rgb { Rgb(lerp(self.0, other.0), lerp(self.1, other.1), lerp(self.2, other.2)) } + /// Whether dark text reads better on it than light text. + pub fn is_light(self) -> bool { + // The way Omarchy tells, by the sum of the channels. + u16::from(self.0) + u16::from(self.1) + u16::from(self.2) > 382 + } + /// The parts of a palette color, which is always RGB; anything else counts as black. pub fn of(color: Color) -> Rgb { match color { diff --git a/crates/cloofy-tui/src/ui/settings.rs b/crates/cloofy-tui/src/ui/settings.rs index 758df99..5df5ca2 100644 --- a/crates/cloofy-tui/src/ui/settings.rs +++ b/crates/cloofy-tui/src/ui/settings.rs @@ -10,7 +10,7 @@ use super::text; use crate::{ app::{App, Focus, Hit}, graphics::ImageProtocol, - preferences::{SECTIONS, Setting}, + preferences::{ColorChoice, SECTIONS, Setting}, theme, views::View, }; @@ -134,6 +134,9 @@ fn about(app: &App, setting: Setting) -> Cow<'static, str> { Setting::Notifications if !app.can_notify() && !app.is_demo() => { "This terminal can't show notifications; see the README for which ones can.".into() } + Setting::Colors if app.prefs.colors == ColorChoice::Terminal && !app.knows_terminal_colors() => { + "This terminal doesn't tell its colors, so cloofy keeps its own.".into() + } Setting::Images if app.prefs.images == ImageProtocol::Auto => { format!("How covers reach the terminal. Auto picked {} here.", app.auto_image_protocol().label()).into() } diff --git a/vendor/ratatui-image b/vendor/ratatui-image index 2899c30..9f2dbe0 160000 --- a/vendor/ratatui-image +++ b/vendor/ratatui-image @@ -1 +1 @@ -Subproject commit 2899c30e4b49331e8b6a4b031672b06099277dfd +Subproject commit 9f2dbe02801294f93cd851c4b00cd139c86f6623 diff --git a/website/src/content/docs/docs/settings.md b/website/src/content/docs/docs/settings.md index 8965fbe..82165f2 100644 --- a/website/src/content/docs/docs/settings.md +++ b/website/src/content/docs/docs/settings.md @@ -3,7 +3,7 @@ title: Settings description: cloofy's Settings page, and every option of config.toml. --- -,, the command palette, or a click on your name in the top bar opens the Settings page: crossfade, cover art and how covers are drawn, the accent color, icons, power saving, song notifications and update checks. Changes apply at once and cloofy saves them in `~/.config/cloofy/config.toml`, so there's no need to edit the file. +,, the command palette, or a click on your name in the top bar opens the Settings page: crossfade, cover art and how covers are drawn, the colors, the accent color, icons, power saving, song notifications and update checks. Changes apply at once and cloofy saves them in `~/.config/cloofy/config.toml`, so there's no need to edit the file. ## config.toml @@ -14,6 +14,7 @@ client_id = "your client id" services = ["ytmusic"] # "spotify", "ytmusic" or both, in the switch's order; every one built in when missing crossfade = 6 # seconds tracks overlap, 0 to 12; off when missing covers = false # no cover art, for slower computers; on when missing +colors = "terminal" # "cloofy" or "terminal"; Omarchy's theme there, cloofy's own elsewhere, when missing accent = "violet" # "theme", "green", "violet", "pink", "blue" or "orange"; follows the cover when missing images = "blocks" # "kitty", "sixel", "iterm2" or "blocks"; detected from the terminal when missing icons = "nerd" # "nerd" or "text"; detected from the terminal when missing @@ -23,7 +24,25 @@ update_check = false # don't look for new releases at start; on when missi credentials = "keychain" # macOS: sign-ins in the Keychain instead of files (only here) ``` -The installer writes `services` with what you listen to. One config file serves every build, so a build leaves out the services it doesn't have. The `"theme"` accent is the [Omarchy](/docs/omarchy/) theme's, and cloofy's green elsewhere. +The installer writes `services` with what you listen to. One config file serves every build, so a build leaves out the services it doesn't have. The `"theme"` accent is the [Omarchy](/docs/omarchy/) theme's or `theme.toml`'s, and cloofy's green elsewhere. + +## Colors + +cloofy draws with its own dark colors, and on Omarchy with the [theme's](/docs/omarchy/). Set Colors to **Terminal** and it takes your terminal's background and text instead, with the shades in between mixed from them, so a purple terminal stays purple. Terminals that don't tell their colors keep cloofy's. **cloofy** keeps cloofy's own colors, on Omarchy too. + +To pick colors yourself, put any of them in `~/.config/cloofy/theme.toml`. They go over whichever colors the setting gives, and cloofy follows the file as you edit it, without a restart: + +```toml +bg = "#2b0a3d" # behind everything +surface = "#33124a" # the sidebar +player = "#30104a" # the player bar +raised = "#3d1a55" # cards, menus and buttons +text = "#f4eefa" +muted = "#b7a3c4" # artists, counts and other secondary text +accent = "#e95420" # the accent while no cover sets one, and the "Theme" accent +``` + +The others are `faint`, `danger`, `unfocused`, `disabled`, `placeholder`, `thumb`, `lyric_near`, `lyric_far` and `highlight`; [Omarchy's template](https://github.com/parda21/cloofy/blob/main/integrations/omarchy/cloofy.toml.tpl) says what each is for. ## Cover art @@ -46,5 +65,6 @@ On macOS, `credentials = "keychain"` keeps sign-ins in the Keychain instead of f | What | Where | | --- | --- | | Settings | `~/.config/cloofy/config.toml` | +| Your own colors | `~/.config/cloofy/theme.toml` | | Sign-ins (unless in the Keychain) and session | `~/.local/share/cloofy` | | Caches and the log | `~/.cache/cloofy` | From dfb927732a3fa44d9bd0223ce27ac025a8d09217 Mon Sep 17 00:00:00 2001 From: Paul Otto Date: Thu, 5 Nov 2026 15:59:40 +0100 Subject: [PATCH 2/2] fix(tui): from the review on #23 - The cover's accent is tuned to the background when shown, not when the cover loads, so switching between a dark and a light palette retunes it. The loader hands over the cover's dominant color, or none, and show_accent tunes it. - The "theme" accent is the one of the colors in use: theme.toml's, else Omarchy's with Colors on Auto, and cloofy's green otherwise, also with Terminal. README, website and the doc comment say so. - ratatui-image: a color channel's length is checked before the shift, so an overlong answer is turned down instead of panicking in debug. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- crates/cloofy-tui/src/app/mod.rs | 11 +++---- crates/cloofy-tui/src/app/settings.rs | 8 +++-- crates/cloofy-tui/src/loader.rs | 5 +-- crates/cloofy-tui/src/preferences.rs | 3 +- crates/cloofy-tui/src/theme.rs | 37 ++++++++++++++++++++--- vendor/ratatui-image | 2 +- website/src/content/docs/docs/settings.md | 2 +- 8 files changed, 49 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a33d3ba..7bb6746 100644 --- a/README.md +++ b/README.md @@ -237,7 +237,7 @@ update_check = false # don't look for new releases at start; on when missi credentials = "keychain" # macOS: sign-ins in the Keychain instead of files (only here) ``` -The installer writes `services` with what you listen to. One config file serves every build, so a build leaves out the services it doesn't have. The `"theme"` accent is the Omarchy theme's or `theme.toml`'s, and cloofy's green elsewhere. +The installer writes `services` with what you listen to. One config file serves every build, so a build leaves out the services it doesn't have. The `"theme"` accent is the one of the colors in use: `theme.toml`'s when it sets one, else the Omarchy theme's with Colors on Auto, and cloofy's green otherwise, also with Terminal. cloofy draws with its own dark colors, or with Omarchy's theme. With Colors set to Terminal, it takes your terminal's background and text instead, with the shades in between. To pick colors yourself, put any of `bg`, `surface`, `player`, `raised`, `text`, `muted`, `faint`, `accent` and the [others](integrations/omarchy/cloofy.toml.tpl) in `~/.config/cloofy/theme.toml`, as `bg = "#2b0a3d"`; they go over whichever colors are set, and cloofy follows the file as you edit it. diff --git a/crates/cloofy-tui/src/app/mod.rs b/crates/cloofy-tui/src/app/mod.rs index 063dfe1..b0fe24e 100644 --- a/crates/cloofy-tui/src/app/mod.rs +++ b/crates/cloofy-tui/src/app/mod.rs @@ -487,7 +487,8 @@ pub struct App { /// Where the palette comes from; `None` in tests, which keep cloofy's own colors. colors: Option, pub accent: Accent, - /// The color of the playing cover, for when the accent follows it. + /// The dominant color of the playing cover, for when the accent follows it; tuned to the + /// background when shown. cover_accent: Option, pub me: Option, pub playlists: Vec, @@ -1486,13 +1487,9 @@ impl App { if let Some(old) = self.now.cover.replace(Cover { url, picture }) { self.terminal_out.extend(old.picture.terminal_delete()); } - self.cover_accent = Some(accent); + self.cover_accent = accent; if self.prefs.accent == AccentChoice::Cover { - if self.settings.low_power { - self.accent.jump(accent); - } else { - self.accent.set(accent); - } + self.show_accent(); } } Err(err) => tracing::warn!("cover {url}: {err}"), diff --git a/crates/cloofy-tui/src/app/settings.rs b/crates/cloofy-tui/src/app/settings.rs index 1c5d732..0c46d62 100644 --- a/crates/cloofy-tui/src/app/settings.rs +++ b/crates/cloofy-tui/src/app/settings.rs @@ -108,9 +108,11 @@ impl App { self.save_setting(setting); } - /// Moves the accent to the color the setting, the cover or the palette gives it now. - fn show_accent(&mut self) { - let color = self.prefs.accent.fixed().or(self.cover_accent).unwrap_or(theme::palette().accent); + /// Moves the accent to the color the setting, the cover or the palette gives it now. The + /// cover's is tuned to the background here, so it follows a change of colors. + pub(super) fn show_accent(&mut self) { + let cover = self.cover_accent.map(theme::cover_accent); + let color = self.prefs.accent.fixed().or(cover).unwrap_or(theme::palette().accent); if self.settings.low_power { self.accent.jump(color); } else { diff --git a/crates/cloofy-tui/src/loader.rs b/crates/cloofy-tui/src/loader.rs index ddc15fc..736991d 100644 --- a/crates/cloofy-tui/src/loader.rs +++ b/crates/cloofy-tui/src/loader.rs @@ -123,7 +123,8 @@ pub enum DataEvent { }, Cover { url: String, - result: Result<(Picture, Rgb), String>, + /// The cover and its dominant color. + result: Result<(Picture, Option), String>, }, LargeCover { url: String, @@ -632,7 +633,7 @@ impl Loader { /// Downloads cover art, picks the accent color and encodes it for the terminal. pub fn cover(&self, url: String, size: Size) { self.spawn(move |this| async move { - let result = this.load_image(&url, size, |image| theme::accent_from_image(image)).await; + let result = this.load_image(&url, size, |image| theme::dominant_color(image)).await; this.send(DataEvent::Cover { url, result: result.map_err(|e| format!("{e:#}")) }); }); } diff --git a/crates/cloofy-tui/src/preferences.rs b/crates/cloofy-tui/src/preferences.rs index f54d201..5b91db1 100644 --- a/crates/cloofy-tui/src/preferences.rs +++ b/crates/cloofy-tui/src/preferences.rs @@ -204,7 +204,8 @@ impl Setting { pub enum AccentChoice { /// The dominant color of the playing cover. Cover, - /// The palette's accent, which Omarchy's theme or `theme.toml` sets; cloofy's green elsewhere. + /// The accent of the colors in use: `theme.toml`'s or, with Colors on Auto, Omarchy's + /// theme's; cloofy's green otherwise. Theme, Fixed(NamedColor), } diff --git a/crates/cloofy-tui/src/theme.rs b/crates/cloofy-tui/src/theme.rs index 037a7e2..cccf4d1 100644 --- a/crates/cloofy-tui/src/theme.rs +++ b/crates/cloofy-tui/src/theme.rs @@ -164,8 +164,9 @@ impl Accent { } } -/// Finds the dominant vivid hue of a cover and tunes it to read well on the background. -pub fn accent_from_image(image: &DynamicImage) -> Rgb { +/// The dominant vivid color of a cover, if it has one. [`cover_accent`] tunes it to the +/// background when it is shown, so it follows a change of colors. +pub fn dominant_color(image: &DynamicImage) -> Option { const BUCKETS: usize = 24; let mut buckets = [[0f32; 4]; BUCKETS]; for pixel in image.thumbnail(40, 40).to_rgb8().pixels() { @@ -186,10 +187,21 @@ pub fn accent_from_image(image: &DynamicImage) -> Rgb { } let [r, g, b, weight] = buckets.into_iter().max_by(|a, b| a[3].total_cmp(&b[3])).unwrap_or_default(); if weight < 1.0 { - return palette().accent; + return None; } - let (h, s, l) = rgb_to_hsl(r / weight, g / weight, b / weight); - let lightness = if palette().is_light() { LIGHT_LIGHTNESS } else { DARK_LIGHTNESS }; + let channel = |c: f32| (c / weight * 255.0).round() as u8; + Some(Rgb(channel(r), channel(g), channel(b))) +} + +/// A cover's dominant color, tuned to read well on the background. +pub fn cover_accent(color: Rgb) -> Rgb { + tune(color, palette().is_light()) +} + +fn tune(color: Rgb, light_background: bool) -> Rgb { + let Rgb(r, g, b) = color; + let (h, s, l) = rgb_to_hsl(f32::from(r) / 255.0, f32::from(g) / 255.0, f32::from(b) / 255.0); + let lightness = if light_background { LIGHT_LIGHTNESS } else { DARK_LIGHTNESS }; let (r, g, b) = hsl_to_rgb(h, s.clamp(0.55, 0.85), l.clamp(lightness.0, lightness.1)); Rgb((r * 255.0) as u8, (g * 255.0) as u8, (b * 255.0) as u8) } @@ -259,6 +271,21 @@ mod tests { assert_eq!(accent.rgb(), target); } + #[test] + fn a_cover_s_color_is_tuned_to_the_background_it_is_shown_on() { + let red = DynamicImage::ImageRgb8(image::RgbImage::from_pixel(8, 8, image::Rgb([200, 20, 20]))); + let color = dominant_color(&red).expect("a vivid cover"); + let lightness = |Rgb(r, g, b): Rgb| (r.max(g).max(b) as f32 + r.min(g).min(b) as f32) / 2.0 / 255.0; + let on_dark = tune(color, false); + let on_light = tune(color, true); + assert!((DARK_LIGHTNESS.0 - 0.01..=DARK_LIGHTNESS.1).contains(&lightness(on_dark)), "{on_dark:?}"); + assert!((LIGHT_LIGHTNESS.0 - 0.01..=LIGHT_LIGHTNESS.1).contains(&lightness(on_light)), "{on_light:?}"); + assert!(on_dark.0 > on_dark.1 && on_light.0 > on_light.1, "still red"); + + let gray = DynamicImage::ImageRgb8(image::RgbImage::from_pixel(8, 8, image::Rgb([128, 128, 128]))); + assert_eq!(dominant_color(&gray), None, "the palette's accent then"); + } + #[test] fn tells_light_themes_from_dark_ones() { assert!(!Palette::DEFAULT.is_light()); diff --git a/vendor/ratatui-image b/vendor/ratatui-image index 9f2dbe0..6c66fc0 160000 --- a/vendor/ratatui-image +++ b/vendor/ratatui-image @@ -1 +1 @@ -Subproject commit 9f2dbe02801294f93cd851c4b00cd139c86f6623 +Subproject commit 6c66fc09285e2aa45c88e7a2158218c2858359b1 diff --git a/website/src/content/docs/docs/settings.md b/website/src/content/docs/docs/settings.md index 82165f2..6b26f97 100644 --- a/website/src/content/docs/docs/settings.md +++ b/website/src/content/docs/docs/settings.md @@ -24,7 +24,7 @@ update_check = false # don't look for new releases at start; on when missi credentials = "keychain" # macOS: sign-ins in the Keychain instead of files (only here) ``` -The installer writes `services` with what you listen to. One config file serves every build, so a build leaves out the services it doesn't have. The `"theme"` accent is the [Omarchy](/docs/omarchy/) theme's or `theme.toml`'s, and cloofy's green elsewhere. +The installer writes `services` with what you listen to. One config file serves every build, so a build leaves out the services it doesn't have. The `"theme"` accent is the one of the colors in use: `theme.toml`'s when it sets one, else the [Omarchy](/docs/omarchy/) theme's with Colors on Auto, and cloofy's green otherwise, also with Terminal. ## Colors