From ca742aa2048e7fdb85e8d4355d0c8d86a807b32d Mon Sep 17 00:00:00 2001 From: devdesk Date: Thu, 22 Feb 2024 23:08:30 +0200 Subject: [PATCH] add hsv to rgb, not better looking --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/stream.rs | 23 ++++++++++++++++++----- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4981f6f..6d80f8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2979,6 +2979,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "216080ab382b992234dda86873c18d4c48358f5cfcb70fd693d7f6f2131b628b" +[[package]] +name = "rgb_hsv" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fec0123e8720dcbc31a22f3ecb7dc5cb41f4b73e361eca37d4acf0b04f572589" + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -3366,6 +3372,7 @@ dependencies = [ "pcap-parser", "png", "pyo3", + "rgb_hsv", "serde", "tokio", "tracing-subscriber", diff --git a/Cargo.toml b/Cargo.toml index 8821ec0..280ab0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ pcap = { version = "1.2.0", features = ["capture-stream"] } pcap-parser = { version = "0.14.1", features = ["data"] } png = "0.17.10" pyo3 = { version = "0.20.0", "features" = ["extension-module"] } +rgb_hsv = "1.0.1" serde = { version = "1.0.193", features = ["derive", "serde_derive", "alloc"] } tokio = { version = "1.36.0", features = ["full"] } tracing-subscriber = "0.3.18" diff --git a/src/stream.rs b/src/stream.rs index 472de19..d617551 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -26,6 +26,18 @@ fn pixel_to_celcius(x: u16) -> u16 { (t * 256.0) as u16 } +/// https://en.wikipedia.org/wiki/HSL_and_HSV +/// convert to the expected dynamic range first. We insert values in [0..256) +/// h in [0, 360] degrees +/// s in [0, 1] +/// v in [0, 1] +fn once_upon_a_time_hsv2rgb(h: u8, s: u8, v: u8) -> (u8, u8, u8) { + let h = (h as f64) / 256.0 * 360.0; + let s = (s as f64) / 256.0; + let v = (v as f64) / 256.0; + (0, 0, 0) +} + fn main() -> anyhow::Result<()> { let args = Args::parse(); dotenv().ok(); @@ -121,12 +133,13 @@ fn main() -> anyhow::Result<()> { let cutoff = args.red_cutoff.unwrap(); let (r, g, b) = if pixel > (256.0 * cutoff) as u16 { let p = pixel - (256.0 * cutoff) as u16; - let p = (p / 256).max(127); - ((128 + p) as u8, 0, 0) + let (r, g, b) = rgb_hsv::hsv_to_rgb((0.0, p as f32, 0.0)); + (r as u8, g as u8, b as u8) } else { - let g = frame[i * 2]; - let b = frame[i * 2 + 1]; - (0, g, b) + let h = frame[i * 2] as f32; + let v = frame[i * 2 + 1] as f32; + let (r, g, b) = rgb_hsv::hsv_to_rgb((h, 0.0, v)); + (r as u8, g as u8, b as u8) }; let out_i = ((HEIGHT - 1 - y) + (WIDTH - 1 - x) * HEIGHT) * 4; swapped[out_i..out_i + 4].copy_from_slice(&[0, r, g, b]);