fix: 灯条段长度为0时,崩溃问题。

This commit is contained in:
Ivan Li 2023-04-02 18:36:22 +08:00
parent 822d470605
commit a1e6c6e5fb
2 changed files with 11 additions and 6 deletions

View File

@ -82,6 +82,10 @@ impl Screenshot {
leds: usize, leds: usize,
single_axis_points: usize, single_axis_points: usize,
) -> Vec<LedSamplePoints> { ) -> Vec<LedSamplePoints> {
if leds == 0 {
return vec![];
}
let cell_size_x = length as f64 / single_axis_points as f64 / leds as f64; let cell_size_x = length as f64 / single_axis_points as f64 / leds as f64;
let cell_size_y = width / single_axis_points; let cell_size_y = width / single_axis_points;
@ -100,6 +104,7 @@ impl Screenshot {
.map(|&x| point_y_list.iter().map(move |&y| (x, y))) .map(|&x| point_y_list.iter().map(move |&y| (x, y)))
.flatten() .flatten()
.collect(); .collect();
points points
.chunks(single_axis_points * single_axis_points) .chunks(single_axis_points * single_axis_points)
.into_iter() .into_iter()
@ -159,6 +164,7 @@ impl Screenshot {
let mut b = 0.0; let mut b = 0.0;
let len = led_points.len() as f64; let len = led_points.len() as f64;
for (x, y) in led_points { for (x, y) in led_points {
// log::info!("x: {}, y: {}, bytes_per_row: {}", x, y, bytes_per_row);
let position = x * 4 + y * bytes_per_row; let position = x * 4 + y * bytes_per_row;
b += bitmap[position] as f64; b += bitmap[position] as f64;
g += bitmap[position + 1] as f64; g += bitmap[position + 1] as f64;

View File

@ -190,11 +190,12 @@ const SorterResult: Component = () => {
let c1 = `rgb(${Math.floor(colors[i * 3] * 0.8)}, ${Math.floor( let c1 = `rgb(${Math.floor(colors[i * 3] * 0.8)}, ${Math.floor(
colors[i * 3 + 1] * 0.8, colors[i * 3 + 1] * 0.8,
)}, ${Math.floor(colors[i * 3 + 2] * 0.8)})`; )}, ${Math.floor(colors[i * 3 + 2] * 0.8)})`;
let c2 = `rgb(${Math.floor(colors[i * 3] * 1.2)}, ${Math.floor( let c2 = `rgb(${Math.min(Math.floor(colors[i * 3] * 1.2), 255)}, ${Math.min(
colors[i * 3 + 1] * 1.2, Math.floor(colors[i * 3 + 1] * 1.2),
)}, ${Math.floor(colors[i * 3 + 2] * 1.2)})`; 255,
)}, ${Math.min(Math.floor(colors[i * 3 + 2] * 1.2), 255)})`;
return `linear-gradient(70deg, ${c1}, ${c2})`; return `linear-gradient(70deg, ${c1} 10%, ${c2})`;
}); });
console.log(fullLeds); console.log(fullLeds);
setFullLeds(fullLeds); setFullLeds(fullLeds);
@ -220,8 +221,6 @@ const SorterResult: Component = () => {
}; };
export const LedStripPartsSorter: Component = () => { export const LedStripPartsSorter: Component = () => {
const context = createContext();
return ( return (
<div <div
class="select-none overflow-hidden" class="select-none overflow-hidden"