From fc44a59a6f8fe3b6405ef2e7aa56f1b4774327f6 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 19 Aug 2022 22:50:39 +0200 Subject: [PATCH] Remove dependency on palette --- Cargo.lock | 60 ------------------------------------------------ Cargo.toml | 1 - src/euph/util.rs | 40 +++++++++++++++++++++++++++----- 3 files changed, 34 insertions(+), 67 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a200efa..7368637 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,15 +28,6 @@ version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "508b352bb5c066aac251f6daf6b36eccd03e8a88e8081cd44959ea277a3af9a8" -[[package]] -name = "approx" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" -dependencies = [ - "num-traits", -] - [[package]] name = "async-trait" version = "0.1.57" @@ -194,7 +185,6 @@ dependencies = [ "edit", "euphoxide", "log", - "palette", "parking_lot", "rusqlite", "serde_json", @@ -333,15 +323,6 @@ dependencies = [ "instant", ] -[[package]] -name = "find-crate" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59a98bbaacea1c0eb6a0876280051b892eb73594fd90cf3b20e9c817029c57d2" -dependencies = [ - "toml", -] - [[package]] name = "fnv" version = "1.0.7" @@ -629,15 +610,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "num-traits" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" -dependencies = [ - "autocfg", -] - [[package]] name = "num_cpus" version = "1.13.1" @@ -675,29 +647,6 @@ version = "6.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "648001efe5d5c0102d8cea768e348da85d90af8ba91f0bea908f157951493cd4" -[[package]] -name = "palette" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f9cd68f7112581033f157e56c77ac4a5538ec5836a2e39284e65bd7d7275e49" -dependencies = [ - "approx", - "num-traits", - "palette_derive", -] - -[[package]] -name = "palette_derive" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05eedf46a8e7c27f74af0c9cfcdb004ceca158cb1b918c6f68f8d7a549b3e427" -dependencies = [ - "find-crate", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "parking_lot" version = "0.12.1" @@ -1249,15 +1198,6 @@ dependencies = [ "webpki", ] -[[package]] -name = "toml" -version = "0.5.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" -dependencies = [ - "serde", -] - [[package]] name = "toss" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index d736dfc..a3ea11d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ crossterm = "0.25.0" directories = "4.0.1" edit = "0.1.4" log = { version = "0.4.17", features = ["std"] } -palette = { version = "0.6.1", default-features = false, features = ["std"] } parking_lot = "0.12.1" rusqlite = { version = "0.28.0", features = ["bundled", "time"] } serde_json = "1.0.83" diff --git a/src/euph/util.rs b/src/euph/util.rs index 1ec7bb3..05eb467 100644 --- a/src/euph/util.rs +++ b/src/euph/util.rs @@ -1,12 +1,40 @@ use crossterm::style::{Color, ContentStyle, Stylize}; -use palette::{FromColor, Hsl, RgbHue, Srgb}; + +/// Convert HSL to RGB following [this approach from wikipedia][1]. +/// +/// `h` must be in the range `[0, 360]`, `s` and `l` in the range `[0, 1]`. +/// +/// [1]: https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB +fn hsl_to_rgb(h: f32, s: f32, l: f32) -> (u8, u8, u8) { + assert!((0.0..=360.0).contains(&h), "h must be in range [0, 360]"); + assert!((0.0..=1.0).contains(&s), "s must be in range [0, 1]"); + assert!((0.0..=1.0).contains(&l), "l must be in range [0, 1]"); + + let c = (1.0 - (2.0 * l - 1.0).abs()) * s; + + let h_prime = h / 60.0; + let x = c * (1.0 - (h_prime.rem_euclid(2.0) - 1.0).abs()); + + let (r1, g1, b1) = match () { + _ if h_prime < 1.0 => (c, x, 0.0), + _ if h_prime < 2.0 => (x, c, 0.0), + _ if h_prime < 3.0 => (0.0, c, x), + _ if h_prime < 4.0 => (0.0, x, c), + _ if h_prime < 5.0 => (x, 0.0, c), + _ => (c, 0.0, x), + }; + + let m = l - c / 2.0; + let (r, g, b) = (r1 + m, g1 + m, b1 + m); + + // The rgb values in the range [0,1] are each split into 256 segments of the + // same length, which are then assigned to the 256 possible values of an u8. + ((r * 256.0) as u8, (g * 256.0) as u8, (b * 256.0) as u8) +} pub fn nick_color(nick: &str) -> (u8, u8, u8) { - let hue = RgbHue::from(euphoxide::nick_hue(nick) as f32); - let color = Hsl::new(hue, 1.0, 0.72); - Srgb::from_color(color) - .into_format::() - .into_components() + let hue = euphoxide::nick_hue(nick) as f32; + hsl_to_rgb(hue, 1.0, 0.72) } pub fn nick_style(nick: &str) -> ContentStyle {