From 8ab9cd7010afe1d7367e3612cc305f277917430d Mon Sep 17 00:00:00 2001 From: Xiretza Date: Fri, 17 Jun 2022 21:05:40 +0200 Subject: [PATCH] feat: add rainbow effect --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 8ac7c19..cb6bc68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index bee6e60..ceec220 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index f6daa5e..102c80c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 for Rgb { #[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(())