Add pallet.rs

This commit is contained in:
Nicolas 2022-04-02 17:19:48 +02:00
parent 1a4cff1dbb
commit aed1f6d58e
3 changed files with 115 additions and 2 deletions

View File

@ -7,3 +7,6 @@ description = "Wipe your terminal with a random animation."
repository = "https://github.com/ricoriedel/wipe"
[dependencies]
clap = { version = "3.1", features = ["derive"]}
crossterm = "0.23"
rand = "0.8"

View File

@ -1,3 +1,20 @@
fn main() {
println!("Hello, world!");
use clap::Parser;
use rand::rngs::OsRng;
use crate::pallet::{choose_pallet, create_pallet, PalletEnum};
mod pallet;
#[derive(Parser)]
#[clap(author = "Rico Riedel", version = "0.1.0", about = "Wipe your terminal with a random animation.")]
struct Args {
#[clap(short, long, help = "Add color pallet", arg_enum)]
pallet: Vec<PalletEnum>,
}
fn main() {
let args = Args::parse();
let rng = &mut OsRng::default();
let pallet_key = choose_pallet(args.pallet, rng);
let pallet = create_pallet(pallet_key);
}

93
src/pallet.rs Normal file
View File

@ -0,0 +1,93 @@
use crossterm::style::Color;
use crossterm::style::Color::*;
use rand::prelude::IteratorRandom;
use rand::Rng;
use clap::ArgEnum;
pub struct Pallet {
values: Vec<Color>
}
impl Pallet {
pub fn new(values: Vec<Color>) -> Self {
Self { values }
}
pub fn red() -> Pallet {
Pallet::new(vec![Yellow, DarkYellow, Red])
}
pub fn red_light() -> Pallet {
Pallet::new(vec![White, Yellow, Red])
}
pub fn green() -> Pallet {
Pallet::new(vec![Cyan, DarkGreen, Green])
}
pub fn green_light() -> Pallet {
Pallet::new(vec![White, Cyan, Green])
}
pub fn blue() -> Pallet {
Pallet::new(vec![Magenta, DarkBlue, Blue])
}
pub fn blue_light() -> Pallet {
Pallet::new(vec![White, Magenta, Blue])
}
pub fn white() -> Pallet {
Pallet::new(vec![Black, Grey, White])
}
pub fn rainbow() -> Pallet {
Pallet::new(vec![Magenta, Blue, Green, Yellow, Red])
}
pub fn sample(&self, fill: f32) -> Color {
let pos = self.values.len() as f32 * fill;
let index = pos as usize;
self.values[index]
}
}
#[derive(Copy, Clone, ArgEnum)]
pub enum PalletEnum {
Red,
RedLight,
Green,
GreenLight,
Blue,
BlueLight,
White,
Rainbow
}
pub fn choose_pallet(mut options: Vec<PalletEnum>, rng: &mut impl Rng) -> PalletEnum {
if options.is_empty() {
options.push(PalletEnum::Red);
options.push(PalletEnum::RedLight);
options.push(PalletEnum::Green);
options.push(PalletEnum::GreenLight);
options.push(PalletEnum::Blue);
options.push(PalletEnum::BlueLight);
options.push(PalletEnum::White);
options.push(PalletEnum::Rainbow);
}
options.into_iter().choose(rng).unwrap()
}
pub fn create_pallet(pallet: PalletEnum) -> Pallet {
match pallet {
PalletEnum::Red => Pallet::red(),
PalletEnum::RedLight => Pallet::red_light(),
PalletEnum::Green => Pallet::green(),
PalletEnum::GreenLight => Pallet::green_light(),
PalletEnum::Blue => Pallet::blue(),
PalletEnum::BlueLight => Pallet::blue_light(),
PalletEnum::White => Pallet::white(),
PalletEnum::Rainbow => Pallet::rainbow()
}
}