Add invert transformation

This commit is contained in:
Rico Riedel 2022-08-04 16:08:19 +02:00
parent 74e83cd543
commit 91e80cc7c7
No known key found for this signature in database
GPG Key ID: 75AC868575DE7B18
3 changed files with 40 additions and 3 deletions

View File

@ -41,12 +41,16 @@ struct Args {
chars: String,
#[clap(long, value_enum)]
char_pattern: Vec<PatternEnum>,
#[clap(long)]
char_invert: Option<bool>,
#[clap(long, value_enum)]
colors: Vec<PalletEnum>,
#[clap(long, value_enum)]
color_pattern: Vec<PatternEnum>,
#[clap(long)]
color_shift: Option<bool>,
#[clap(long)]
color_invert: Option<bool>,
}
#[derive(ValueEnum, Clone)]
@ -89,15 +93,16 @@ enum PatternEnum {
struct PatternConfig<'a> {
patterns: &'a Vec<PatternEnum>,
shift: Option<bool>,
invert: Option<bool>,
}
impl Args {
fn char_config(&self) -> PatternConfig {
PatternConfig::new(&self.char_pattern, Some(true))
PatternConfig::new(&self.char_pattern, Some(true), self.char_invert)
}
fn color_config(&self) -> PatternConfig {
PatternConfig::new(&self.color_pattern, self.color_shift)
PatternConfig::new(&self.color_pattern, self.color_shift, self.color_invert)
}
fn pallet(&self, rand: &mut impl Rng) -> Vec<Color> {
@ -153,6 +158,9 @@ impl<'a> PatternConfig<'a> {
if self.shift.unwrap_or(rand.gen()) {
pattern = Box::new(ShiftFactory::new(pattern))
}
if self.invert.unwrap_or(rand.gen()) {
pattern = Box::new(InvertFactory::new(pattern))
}
pattern
}
}

27
src/transform/invert.rs Normal file
View File

@ -0,0 +1,27 @@
use crate::pattern::*;
use crate::Vector;
#[derive(derive_more::Constructor)]
pub struct InvertFactory {
child: Box<dyn PatternFactory>,
}
#[derive(derive_more::Constructor)]
pub struct Invert {
child: Box<dyn Pattern>,
}
impl PatternFactory for InvertFactory {
fn create(&self, config: &Config) -> Box<dyn Pattern> {
let mut copy = config.clone();
copy.step = 1.0 - config.step;
Box::new(Invert::new(self.child.create(&copy)))
}
}
impl Pattern for Invert {
fn sample(&self, pos: Vector) -> f32 {
1.0 - self.child.sample(pos)
}
}

View File

@ -1,3 +1,5 @@
mod invert;
mod shift;
pub use shift::*;
pub use invert::*;
pub use shift::*;