diff --git a/Cargo.lock b/Cargo.lock index 5271e2e..e5149ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc" +[[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-broadcast" version = "0.4.0" @@ -233,6 +242,15 @@ 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 = "fuchsia-cprng" version = "0.1.1" @@ -476,6 +494,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.10.0" @@ -492,6 +519,30 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "palette" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9735f7e1e51a3f740bacd5dc2724b61a7806f23597a8736e679f38ee3435d18" +dependencies = [ + "approx", + "num-traits", + "palette_derive", + "phf", +] + +[[package]] +name = "palette_derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7799c3053ea8a6d8a1193c7ba42f534e7863cf52e378a7f90406f4a645d33bad" +dependencies = [ + "find-crate", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "parking" version = "2.0.0" @@ -523,6 +574,50 @@ dependencies = [ "winapi", ] +[[package]] +name = "phf" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ac8b67553a7ca9457ce0e526948cad581819238f4a9d1ea74545851fa24f37" +dependencies = [ + "phf_macros", + "phf_shared", + "proc-macro-hack", +] + +[[package]] +name = "phf_generator" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d43f3220d96e0080cc9ea234978ccd80d904eafb17be31bb0f76daaea6493082" +dependencies = [ + "phf_shared", + "rand 0.8.5", +] + +[[package]] +name = "phf_macros" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b706f5936eb50ed880ae3009395b43ed19db5bff2ebd459c95e7bf013a89ab86" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "phf_shared" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a68318426de33640f02be62b4ae8eb1261be2efbc337b60c54d845bf4484e0d9" +dependencies = [ + "siphasher", +] + [[package]] name = "pin-project-lite" version = "0.2.9" @@ -570,6 +665,12 @@ dependencies = [ "toml", ] +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + [[package]] name = "proc-macro2" version = "1.0.37" @@ -742,6 +843,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" +[[package]] +name = "siphasher" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" + [[package]] name = "slab" version = "0.4.6" @@ -886,6 +993,7 @@ dependencies = [ "anyhow", "calloop", "futures", + "palette", "smithay-client-toolkit", "upower_dbus", "wayland-client", diff --git a/Cargo.toml b/Cargo.toml index 4c012fe..4ba9538 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,6 @@ calloop = "0.9.3" zbus = "2.2.0" upower_dbus = { version = "0.2.0" } -futures = { version = "0.3.21", features = ["executor"] } \ No newline at end of file +futures = { version = "0.3.21", features = ["executor"] } + +palette = "0.6.0" \ No newline at end of file diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs index 56762fa..e705b9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,14 @@ +extern crate core; + pub mod upower; use std::cell::Cell; use std::sync::RwLock; use std::{cell::RefCell, rc::Rc, sync::Arc}; +use std::cmp::min; +use std::os::linux::raw::stat; +use palette::convert::FromColorUnclamped; +use palette::{Blend, FromColor, IntoColor, LinSrgba, Mix, Oklab, Oklaba, Packed, Pixel, Shade, Srgb, Srgba}; use wayland_client::{ protocol::{wl_output::WlOutput, wl_shm, wl_surface::WlSurface}, Attached, Main, @@ -74,9 +80,9 @@ impl Surface { "WattBar".to_owned(), ); - layer_surface.set_size(1900, 3); + layer_surface.set_size(1900, 2); layer_surface.set_anchor(zwlr_layer_surface_v1::Anchor::Bottom); - layer_surface.set_exclusive_zone(3); + layer_surface.set_exclusive_zone(2); let next_render_event = Rc::new(Cell::new(None)); let nre_handle = Rc::clone(&next_render_event); @@ -141,12 +147,45 @@ impl Surface { .pool .buffer(width, height, stride, wl_shm::Format::Argb8888) .unwrap(); - for dst_pixel in canvas.chunks_exact_mut(4) { - let pixel = 0x01_00_8f_00u32.to_ne_bytes(); - dst_pixel[0] = pixel[0]; - dst_pixel[1] = pixel[1]; - dst_pixel[2] = pixel[2]; - dst_pixel[3] = pixel[3]; + + let state = self.display_status.read().map_or(None, |lock| lock.clone()); + + let (base_color, pct) = if let Some(state) = state { + let mix_color = if !state.charging { + let min_color = Oklaba::from_color_unclamped(palette::LinSrgba::new(1., 0., 0., 1.)); + let max_color = Oklaba::from_color_unclamped(palette::LinSrgba::new(0., 1., 0., 1.)); + min_color.mix(&max_color, state.level) + } else { + Oklaba::from_color_unclamped(Srgba::new(0., 0.5, 1., 1.0f32)) + }; + + (mix_color, state.level) + } else { + + + let color = Oklaba::from_color_unclamped(Srgba::new(0., 0.5, 1., 1.0f32)); + let pct = 0.5; + (color, pct) + }; + + let bg_color = base_color.darken(0.5); + + let to_u32 = |color| { + LinSrgba::from_color(color).into_encoding::().into_format::().into_u32::().to_le_bytes() + } ; + + + let fg_color = to_u32(base_color); + let bg_color = to_u32(bg_color); + eprintln!("Colors: {:?}/{:?}", fg_color, bg_color); + + // let pct = pct * 0.75 + 0.125; + // blit the buffer + let fill_width = (width as f32 * pct) as usize * 4; + for row in canvas.chunks_exact_mut(stride as usize) { + println!("Filling ..{}", fill_width); + row[..fill_width].chunks_exact_mut(4).for_each(|chunk| chunk.copy_from_slice(fg_color.as_slice())); + row[fill_width..].chunks_exact_mut(4).for_each(|chunk| chunk.copy_from_slice(bg_color.as_slice())); } self.surface.attach(Some(&buffer), 0, 0);