From 753074d0f4091b2b25c22bd6c49566f7b5f5603f Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Wed, 23 Nov 2022 21:42:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=95=B0=E6=8D=AE=E4=BC=A0=E8=BE=93?= =?UTF-8?q?=E5=8D=A1=E9=A1=BF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/main.rs | 2 +- src-tauri/src/picker/screenshot.rs | 34 ++++++++++++++++++------------ src-tauri/src/rpc/manager.rs | 2 +- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 05fcefb..8d67752 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -51,7 +51,7 @@ async fn get_led_strip_colors() -> Result, String> { match colors { Ok(colors) => { rpc::manager::Manager::global() - .publish_led_colors(&colors) + .publish_led_colors(&colors.to_vec()) .await; Ok(colors) } diff --git a/src-tauri/src/picker/screenshot.rs b/src-tauri/src/picker/screenshot.rs index c60ce1c..905fb81 100644 --- a/src-tauri/src/picker/screenshot.rs +++ b/src-tauri/src/picker/screenshot.rs @@ -1,3 +1,5 @@ +use color_space::{Hsv, Rgb}; +use paris::info; use std::sync::Arc; use tokio::sync::Mutex; @@ -58,27 +60,33 @@ impl Screenshot { XPosition::Bottom => self.height - cell_size_y..self.height, }; - let mut colors = vec![LedColor::default(); self.led_number_of_x]; + let mut colors = Vec::new(); let stride = bitmap.len() / self.height; for pos in 0..self.led_number_of_x { - let mut r = 0u32; - let mut g = 0u32; - let mut b = 0u32; + let mut r = 0.0; + let mut g = 0.0; + let mut b = 0.0; for x in pos * cell_size_x..(pos + 1) * cell_size_x { for y in y_range.to_owned() { let i = stride * y + 4 * x; - r += bitmap[i + 2] as u32; - g += bitmap[i + 1] as u32; - b += bitmap[i] as u32; + r += bitmap[i + 2] as f64; + g += bitmap[i + 1] as f64; + b += bitmap[i] as f64; } } - colors[pos] = LedColor::new( - (r / cell_size as u32) as u8, - (g / cell_size as u32) as u8, - (b / cell_size as u32) as u8, + let rgb = Rgb::new( + r / cell_size as f64, + g / cell_size as f64, + b / cell_size as f64, ); + let hsv = Hsv::from(rgb); + info!("HSV: {:?}", [hsv.h, hsv.s, hsv.v]); + let color = LedColor::from_hsv(hsv.h, hsv.s * 10.0, hsv.v / 2.0); + info!("color: {:?}", color.get_rgb()); + colors.push(color); } + colors.reverse(); return Ok(colors); } None => Ok(vec![]), @@ -111,10 +119,8 @@ impl Screenshot { ) .encode(100.0); return base64::encode(&*webp_memory); - }, - None => { - "".to_owned() } + None => "".to_owned(), } } } diff --git a/src-tauri/src/rpc/manager.rs b/src-tauri/src/rpc/manager.rs index b6a3af6..ef09249 100644 --- a/src-tauri/src/rpc/manager.rs +++ b/src-tauri/src/rpc/manager.rs @@ -32,7 +32,7 @@ impl Manager { .client .publish( "screen-bg-light/desktop/colors", - rumqttc::QoS::AtMostOnce, + rumqttc::QoS::AtLeastOnce, false, payload, )