feat: add rainbow effect

This commit is contained in:
Xiretza 2022-06-17 21:05:40 +02:00
parent 0d8f11c712
commit 8ab9cd7010
3 changed files with 42 additions and 0 deletions

7
Cargo.lock generated
View File

@ -49,6 +49,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bracket-color"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c1d1b160817fb74eebedccd678055cd688d1a73dc1a14519fa30ff4c9a5bdee"
[[package]]
name = "bumpalo"
version = "3.10.0"
@ -662,6 +668,7 @@ name = "splink_client"
version = "0.1.0"
dependencies = [
"anyhow",
"bracket-color",
"clap",
"image",
"rand",

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.57"
bracket-color = "0.8.2"
clap = { version = "3.1.18", features = ["derive", "deprecated"] }
image = "0.24.2"
rand = "0.8.5"

View File

@ -10,6 +10,7 @@ use std::{
time::Duration,
};
use bracket_color::prelude::{HSV, RGB};
use clap::{Parser, Subcommand};
use image::{Pixel, Rgb, RgbImage};
use rand::Rng;
@ -78,6 +79,7 @@ impl From<Color> for Rgb<u8> {
#[derive(Clone, Debug, PartialEq, Eq, Subcommand)]
enum Action {
Rainbow,
Solid { color: Color },
Clear,
}
@ -122,6 +124,24 @@ fn get_frame(layout: Layout, frame: u32) -> RgbImage {
})
}
fn rainbow(layout: Layout, frame: u32) -> RgbImage {
#![allow(
clippy::cast_precision_loss,
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
let w = layout.width_px();
let h = layout.height_px();
RgbImage::from_fn(w, h, |x, y| {
let RGB { r, g, b } =
HSV::from_f32((x + y + frame) as f32 / 100.0 % 1.0, 1.0, 0.1).to_rgb();
Rgb([(r * 255.0) as u8, (g * 255.0) as u8, (b * 255.0) as u8])
})
}
fn print_image(image: &RgbImage) {
let _hide = termion::cursor::HideCursor::from(stdout());
@ -170,6 +190,20 @@ fn main() -> anyhow::Result<()> {
let frame_num: u32 = rand::thread_rng().gen();
send_frame(&socket, layout, frame_num, &image)?;
}
Action::Rainbow => {
print!("{}", termion::clear::All);
for frame in 0.. {
let image = rainbow(layout, frame);
print_image(&image);
let frame_num: u32 = rand::thread_rng().gen();
send_frame(&socket, layout, frame_num, &image)?;
sleep(Duration::from_millis(16));
}
}
}
Ok(())