From a6e91dfae12c57c5a4e1fb74733bd01245789cce Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Thu, 1 Dec 2022 20:15:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=88=AA=E5=B1=8F=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=8A=BD=E6=A0=B7=EF=BC=8C=E6=8F=90=E5=8D=87=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/Cargo.lock | 7 ++++++ src-tauri/Cargo.toml | 1 + src-tauri/src/core/core.rs | 7 ++++-- src-tauri/src/picker/manager.rs | 8 +++---- src-tauri/src/picker/screenshot.rs | 37 ++++++++++++++++++++++-------- src/main.tsx | 6 ++--- 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index b80ffff..8b811dc 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -573,6 +573,7 @@ dependencies = [ "base64", "bmp", "color_space", + "either", "futures", "hex", "once_cell", @@ -611,6 +612,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bd4b30a6560bbd9b4620f4de34c3f14f60848e58a9b7216801afcb4c7b31c3c" +[[package]] +name = "either" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" + [[package]] name = "embed_plist" version = "1.2.2" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 9803e70..69306a8 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -32,6 +32,7 @@ rumqttc = "0.17.0" time = { version = "0.3.17", features = ["formatting"] } color_space = "0.5.3" futures = "0.3.25" +either = "1.8.0" [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/core/core.rs b/src-tauri/src/core/core.rs index 0565426..b89074d 100644 --- a/src-tauri/src/core/core.rs +++ b/src-tauri/src/core/core.rs @@ -120,13 +120,15 @@ impl CoreManager { let start_at = Instant::now(); match screenshot.get_top_colors().await { Ok(colors) => { - let start = screenshot.get_top_of_led_strip_range().min().unwrap_or(0); + let start = screenshot.get_top_of_led_start_at().min(screenshot.get_top_of_led_end_at()); let colors_len = colors.len(); for (index, color) in colors.into_iter().enumerate() { global_colors.insert(index + start, color); } + info!("led count: {}, spend: {:?}", global_colors.len(), start_at.elapsed()); + if global_colors.len() == 60 { let mut colors = vec![]; for index in 0..global_colors.len() { @@ -165,6 +167,7 @@ impl CoreManager { tx: mpsc::Sender, ) -> anyhow::Result<()> { let mut picker = DisplayPicker::from_config(config)?; + info!("width: {}", picker.config.display_width); loop { let start = Instant::now(); @@ -173,7 +176,7 @@ impl CoreManager { if let AmbientLightMode::Follow = *lock { drop(lock); let screenshot = picker.take_screenshot()?; - // info!("Take Screenshot Spend: {:?}", start.elapsed()); + info!("Take Screenshot Spend: {:?}", start.elapsed()); tx.send(screenshot).await; } else { break; diff --git a/src-tauri/src/picker/manager.rs b/src-tauri/src/picker/manager.rs index 44f1dfa..328d461 100644 --- a/src-tauri/src/picker/manager.rs +++ b/src-tauri/src/picker/manager.rs @@ -35,8 +35,8 @@ impl Picker { display_height: 1200, top_led_strip: LedStripConfig { index: 1, - global_start_position: 32, - global_end_position: 60, + global_start_position: 59, + global_end_position: 32, }, bottom_led_strip: LedStripConfig { index: 0, @@ -60,8 +60,8 @@ impl Picker { display_height: 1692, top_led_strip: LedStripConfig { index: 0, - global_start_position: 0, - global_end_position: 32, + global_start_position: 31, + global_end_position: 0, }, bottom_led_strip: LedStripConfig { index: 0, diff --git a/src-tauri/src/picker/screenshot.rs b/src-tauri/src/picker/screenshot.rs index 4dcca16..31a713f 100644 --- a/src-tauri/src/picker/screenshot.rs +++ b/src-tauri/src/picker/screenshot.rs @@ -1,7 +1,7 @@ - use std::ops::Range; use color_space::{Hsv, Rgb}; +use either::Either; use super::{ config::{DisplayConfig, LedStripConfig}, @@ -28,9 +28,11 @@ impl Screenshot { .await } - pub fn get_top_of_led_strip_range(&self) -> Range { + pub fn get_top_of_led_start_at(&self) -> usize { self.config.top_led_strip.global_start_position - ..self.config.top_led_strip.global_end_position + } + pub fn get_top_of_led_end_at(&self) -> usize { + self.config.top_led_strip.global_end_position } async fn get_x_colors( @@ -41,7 +43,11 @@ impl Screenshot { let bitmap = &self.bitmap; let number_of_leds = strip_config .global_start_position - .abs_diff(strip_config.global_end_position); + .abs_diff(strip_config.global_end_position) + + 1; + if number_of_leds == 0 { + return Ok(vec![]); + } let cell_size_x = self.config.display_width / number_of_leds; let cell_size_y = self.config.display_height / 8; let cell_size = cell_size_x * cell_size_y; @@ -50,32 +56,43 @@ impl Screenshot { XPosition::Bottom => { self.config.display_height - 20 - cell_size_y..self.config.display_height - 20 } + } + .step_by(5); + + let x_range = if strip_config.global_start_position < strip_config.global_end_position { + Either::Left(strip_config.global_start_position..=strip_config.global_end_position) + } else { + Either::Right( + (strip_config.global_end_position..=strip_config.global_start_position).rev(), + ) }; let mut colors = Vec::new(); let stride = bitmap.len() / self.config.display_height; - for pos in strip_config.global_start_position..strip_config.global_end_position { + for pos in x_range { 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 { + let mut count = 0; + for x in (pos * cell_size_x..(pos + 1) * cell_size_x).step_by(5) { for y in y_range.to_owned() { let i = stride * y + 4 * x; r += bitmap[i + 2] as f64; g += bitmap[i + 1] as f64; b += bitmap[i] as f64; + count+=1; } } let rgb = Rgb::new( - r / cell_size as f64, - g / cell_size as f64, - b / cell_size as f64, + r / count as f64, + g / count as f64, + b / count as f64, ); let hsv = Hsv::from(rgb); // info!("HSV: {:?}", [hsv.h, hsv.s, hsv.v]); let color = LedColor::from_hsv(hsv.h, hsv.s, hsv.v); - // info!("color: {:?}", color.get_rgb()); + // paris::info!("color: {:?}", color.get_rgb()); colors.push(color); } return Ok(colors); diff --git a/src/main.tsx b/src/main.tsx index 7d6d78e..1085049 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -3,8 +3,8 @@ import ReactDOM from "react-dom/client"; import App from "./App"; import "./style.css"; -ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( +ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( - -); + , +); \ No newline at end of file