Compare commits

...

2 commits

Author SHA1 Message Date
Alex Janka f49bf7b949 switch to oklab 2024-09-05 09:36:49 +10:00
Alex Janka 77095d6653 update deps 2024-09-05 09:02:34 +10:00
7 changed files with 155 additions and 58 deletions

149
Cargo.lock generated
View file

@ -38,6 +38,15 @@ version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]]
name = "approx"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6"
dependencies = [
"num-traits",
]
[[package]]
name = "arc-swap"
version = "1.7.1"
@ -123,6 +132,12 @@ version = "3.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
[[package]]
name = "by_address"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64fa3c856b712db6612c019f14756e64e4bcea13337a6b33b696333a9eaa2d06"
[[package]]
name = "byteorder"
version = "1.5.0"
@ -137,9 +152,9 @@ checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50"
[[package]]
name = "cc"
version = "1.1.15"
version = "1.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6"
checksum = "e9d013ecb737093c0e86b151a7b837993cf9ec6c502946cfb44bedc392421e0b"
dependencies = [
"shlex",
]
@ -267,6 +282,12 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "fast-srgb8"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd2e7510819d6fbf51a5545c8f922716ecfb14df168a3242f7d33e0239efe6a1"
[[package]]
name = "fastrand"
version = "2.1.1"
@ -275,9 +296,9 @@ checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6"
[[package]]
name = "filetime"
version = "0.2.24"
version = "0.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf401df4a4e3872c4fe8151134cf483738e74b67fc934d6532c882b3d24a4550"
checksum = "35c0522e981e68cbfa8c3f978441a5f34b30b96e146b33cd3359176b50fe8586"
dependencies = [
"cfg-if",
"libc",
@ -333,7 +354,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
@ -422,9 +443,9 @@ dependencies = [
[[package]]
name = "indexmap"
version = "2.4.0"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93ead53efc7ea8ed3cfb0c79fc8023fbb782a5432b52830b6518941cebe6505c"
checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5"
dependencies = [
"equivalent",
"hashbrown",
@ -485,12 +506,6 @@ dependencies = [
"libc",
]
[[package]]
name = "lab"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf36173d4167ed999940f804952e6b08197cae5ad5d572eb4db150ce8ad5d58f"
[[package]]
name = "libc"
version = "0.2.158"
@ -647,9 +662,9 @@ dependencies = [
[[package]]
name = "object"
version = "0.36.3"
version = "0.36.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27b64972346851a39438c60b341ebc01bba47464ae329e55cf343eb93964efd9"
checksum = "084f1a5821ac4c651660a94a7153d27ac9d8a53736203f58b31945ded098070a"
dependencies = [
"memchr",
]
@ -675,6 +690,30 @@ dependencies = [
"num-traits",
]
[[package]]
name = "palette"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cbf71184cc5ecc2e4e1baccdb21026c20e5fc3dcf63028a086131b3ab00b6e6"
dependencies = [
"approx",
"fast-srgb8",
"palette_derive",
"phf",
]
[[package]]
name = "palette_derive"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5030daf005bface118c096f510ffb781fc28f9ab6a32ab224d8631be6851d30"
dependencies = [
"by_address",
"proc-macro2",
"quote",
"syn 2.0.77",
]
[[package]]
name = "parking"
version = "2.2.0"
@ -704,6 +743,48 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "phf"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc"
dependencies = [
"phf_macros",
"phf_shared",
]
[[package]]
name = "phf_generator"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0"
dependencies = [
"phf_shared",
"rand",
]
[[package]]
name = "phf_macros"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro2",
"quote",
"syn 2.0.77",
]
[[package]]
name = "phf_shared"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b"
dependencies = [
"siphasher",
]
[[package]]
name = "pin-project-lite"
version = "0.2.14"
@ -816,9 +897,9 @@ checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]]
name = "rustix"
version = "0.38.34"
version = "0.38.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f"
checksum = "a85d50532239da68e9addb745ba38ff4612a242c1c7ceea689c4bc7c2f43c36f"
dependencies = [
"bitflags 2.6.0",
"errno",
@ -875,14 +956,14 @@ checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
name = "serde_json"
version = "1.0.127"
version = "1.0.128"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad"
checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8"
dependencies = [
"itoa",
"memchr",
@ -927,6 +1008,12 @@ dependencies = [
"libc",
]
[[package]]
name = "siphasher"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d"
[[package]]
name = "slab"
version = "0.4.9"
@ -954,15 +1041,15 @@ dependencies = [
[[package]]
name = "sway-flash-indicator"
version = "0.4.3"
version = "0.5.0"
dependencies = [
"directories",
"futures-util",
"lab",
"log",
"log4rs",
"notify",
"notify-debouncer-mini",
"palette",
"serde",
"swayipc-async",
"thiserror",
@ -1008,9 +1095,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.76"
version = "2.0.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525"
checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed"
dependencies = [
"proc-macro2",
"quote",
@ -1034,7 +1121,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
@ -1049,9 +1136,9 @@ dependencies = [
[[package]]
name = "tokio"
version = "1.39.3"
version = "1.40.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9babc99b9923bfa4804bd74722ff02c0381021eafa4db9949217e3be8e84fff5"
checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998"
dependencies = [
"backtrace",
"bytes",
@ -1073,7 +1160,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
@ -1194,7 +1281,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
"wasm-bindgen-shared",
]
@ -1216,7 +1303,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -1442,5 +1529,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
]

View file

@ -1,18 +1,18 @@
[package]
name = "sway-flash-indicator"
version = "0.4.3"
version = "0.5.0"
edition = "2021"
[dependencies]
log = "0.4.22"
log4rs = "1.3.0"
swayipc-async = "2.0.3"
tokio = { version = "1.39.3", features = ["full"] }
tokio = { version = "1.40.0", features = ["full"] }
futures-util = "0.3.30"
lab = "0.11.0"
directories = "5.0.1"
toml = "0.8.19"
serde = { version = "1.0.209", features = ["derive"] }
thiserror = "1.0.63"
notify-debouncer-mini = "0.4.1"
notify = "6.1.1"
palette = "0.7.6"

View file

@ -1,7 +1,7 @@
# Maintainer: Alex Janka <alex@alexjanka.com>
pkgname=sway-flash-indicator
pkgver=0.4.3
pkgver=0.5.0
pkgrel=1
pkgdesc="flashes sway indicator border rather than always showing it"
arch=('x86_64' 'aarch64')

View file

@ -1,11 +1,23 @@
use lab::Lab;
use palette::FromColor;
use crate::prelude::*;
pub fn parse_hex(hex: &str) -> Res<Lab> {
pub type Format = palette::Oklab;
pub type RgbFormat = palette::Srgb<u8>;
pub fn parse_hex(hex: &str) -> Res<Format> {
let hex = hex.strip_prefix('#').unwrap_or(hex);
let r = u8::from_str_radix(&hex[..2], 16).map_err(|_| Error::HexParse)?;
let g = u8::from_str_radix(&hex[2..4], 16).map_err(|_| Error::HexParse)?;
let b = u8::from_str_radix(&hex[4..], 16).map_err(|_| Error::HexParse)?;
Ok(Lab::from_rgb(&[r, g, b]))
let rgb_u8 = palette::rgb::Srgb::new(r, g, b);
let rgb_f32 = palette::rgb::Srgb::<f32>::from(rgb_u8);
let oklab = palette::Oklab::from_color(rgb_f32);
Ok(oklab)
}
pub fn oklab_to_hex(oklab: &Format) -> String {
let (r, g, b) = RgbFormat::from(palette::Srgb::from_color(*oklab)).into_components();
format!("#{r:02x}{g:02x}{b:02x}")
}

View file

@ -1,4 +1,3 @@
use lab::Lab;
use serde::{Deserialize, Serialize};
use crate::prelude::*;
@ -10,7 +9,7 @@ pub struct Config {
pub frames_anim: u32,
pub refresh_rate: u64,
#[serde(serialize_with = "lab_ser", deserialize_with = "lab_de")]
pub flash_colour: Lab,
pub flash_colour: colour::Format,
pub autosplit_enabled: bool,
pub autosplit_ratio: f64,
pub output_blocklist: Vec<String>,
@ -128,11 +127,7 @@ impl Default for Config {
frames_delay: 10,
frames_anim: 20,
refresh_rate: 60,
flash_colour: Lab {
l: 53.2,
a: 80.1,
b: 67.2,
},
flash_colour: colour::parse_hex("#ff0000").unwrap(),
autosplit_enabled: true,
autosplit_ratio: 1.0,
output_blocklist: Vec::new(),
@ -141,14 +136,16 @@ impl Default for Config {
}
}
fn lab_ser<S: serde::Serializer>(colour: &Lab, serializer: S) -> Result<S::Ok, S::Error> {
let [r, g, b] = colour.to_rgb();
format!("#{r:02x}{g:02x}{b:02x}").serialize(serializer)
fn lab_ser<S: serde::Serializer>(
colour: &colour::Format,
serializer: S,
) -> Result<S::Ok, S::Error> {
colour::oklab_to_hex(colour).serialize(serializer)
}
fn lab_de<'a, D: serde::Deserializer<'a>>(deserializer: D) -> Result<Lab, D::Error> {
fn lab_de<'a, D: serde::Deserializer<'a>>(deserializer: D) -> Result<colour::Format, D::Error> {
use serde::de::Error;
let hex = String::deserialize(deserializer)?;
crate::colour::parse_hex(&hex).map_err(|_| D::Error::custom("couldn't parse hex"))
colour::parse_hex(&hex).map_err(|_| D::Error::custom("couldn't parse hex"))
}

View file

@ -6,7 +6,7 @@ pub async fn interpolate_task() -> Res<()> {
let to = crate::DEFAULT_BORDER.get().ok_or(Error::NoMatchingConfig)?;
let config = CONFIG.get().await;
let per_frame = 1.0 / config.frames_anim as f32;
let (d_l, d_a, d_b) = (
let (d_l, d_chroma, d_hue) = (
(to.l - config.flash_colour.l) * per_frame,
(to.a - config.flash_colour.a) * per_frame,
(to.b - config.flash_colour.b) * per_frame,
@ -20,8 +20,8 @@ pub async fn interpolate_task() -> Res<()> {
tokio::time::sleep(dur_per_frame * config.frames_delay).await;
for _ in 0..config.frames_anim {
c.l += d_l;
c.a += d_a;
c.b += d_b;
c.a += d_chroma;
c.b += d_hue;
connection.run_command(set_command(&c)?).await?;
@ -37,10 +37,11 @@ pub async fn interpolate_task() -> Res<()> {
Ok(())
}
fn set_command(colour: &lab::Lab) -> Res<String> {
let [r, g, b] = colour.to_rgb();
fn set_command(colour: &colour::Format) -> Res<String> {
let hex = colour::oklab_to_hex(colour);
Ok(format!(
"{} #{r:02x}{g:02x}{b:02x}",
"{} {hex}",
crate::DEFAULT_COLOURS_NO_INDICATOR
.get()
.ok_or(Error::NoMatchingConfig)?

View file

@ -1,5 +1,4 @@
use futures_util::StreamExt;
use lab::Lab;
mod colour;
mod config;
@ -8,6 +7,7 @@ mod flash;
mod logger;
pub mod prelude {
pub(crate) use crate::colour;
pub use crate::error::*;
pub use crate::CONFIG;
}
@ -18,7 +18,7 @@ pub static CONFIG: config::ConfigHandle = config::ConfigHandle::new();
static DEFAULT_COLOURS: tokio::sync::OnceCell<String> = tokio::sync::OnceCell::const_new();
static DEFAULT_COLOURS_NO_INDICATOR: tokio::sync::OnceCell<String> =
tokio::sync::OnceCell::const_new();
static DEFAULT_BORDER: tokio::sync::OnceCell<Lab> = tokio::sync::OnceCell::const_new();
static DEFAULT_BORDER: tokio::sync::OnceCell<colour::Format> = tokio::sync::OnceCell::const_new();
#[tokio::main]
async fn main() -> Res<()> {