Add swap transformation

This commit is contained in:
Rico Riedel 2022-08-04 16:24:31 +02:00
parent 91e80cc7c7
commit 852d3e5bf5
No known key found for this signature in database
GPG Key ID: 75AC868575DE7B18
4 changed files with 58 additions and 2 deletions

View File

@ -43,6 +43,8 @@ struct Args {
char_pattern: Vec<PatternEnum>, char_pattern: Vec<PatternEnum>,
#[clap(long)] #[clap(long)]
char_invert: Option<bool>, char_invert: Option<bool>,
#[clap(long)]
char_swap: Option<bool>,
#[clap(long, value_enum)] #[clap(long, value_enum)]
colors: Vec<PalletEnum>, colors: Vec<PalletEnum>,
#[clap(long, value_enum)] #[clap(long, value_enum)]
@ -51,6 +53,8 @@ struct Args {
color_shift: Option<bool>, color_shift: Option<bool>,
#[clap(long)] #[clap(long)]
color_invert: Option<bool>, color_invert: Option<bool>,
#[clap(long)]
color_swap: Option<bool>,
} }
#[derive(ValueEnum, Clone)] #[derive(ValueEnum, Clone)]
@ -94,15 +98,26 @@ struct PatternConfig<'a> {
patterns: &'a Vec<PatternEnum>, patterns: &'a Vec<PatternEnum>,
shift: Option<bool>, shift: Option<bool>,
invert: Option<bool>, invert: Option<bool>,
swap: Option<bool>,
} }
impl Args { impl Args {
fn char_config(&self) -> PatternConfig { fn char_config(&self) -> PatternConfig {
PatternConfig::new(&self.char_pattern, Some(true), self.char_invert) PatternConfig::new(
&self.char_pattern,
Some(true),
self.char_invert,
self.char_swap,
)
} }
fn color_config(&self) -> PatternConfig { fn color_config(&self) -> PatternConfig {
PatternConfig::new(&self.color_pattern, self.color_shift, self.color_invert) PatternConfig::new(
&self.color_pattern,
self.color_shift,
self.color_invert,
self.color_swap,
)
} }
fn pallet(&self, rand: &mut impl Rng) -> Vec<Color> { fn pallet(&self, rand: &mut impl Rng) -> Vec<Color> {
@ -161,6 +176,9 @@ impl<'a> PatternConfig<'a> {
if self.invert.unwrap_or(rand.gen()) { if self.invert.unwrap_or(rand.gen()) {
pattern = Box::new(InvertFactory::new(pattern)) pattern = Box::new(InvertFactory::new(pattern))
} }
if self.swap.unwrap_or(rand.gen()) {
pattern = Box::new(SwapFactory::new(pattern))
}
pattern pattern
} }
} }

View File

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

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

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

View File

@ -32,6 +32,10 @@ impl Vector {
pub fn angle(&self) -> f32 { pub fn angle(&self) -> f32 {
self.y.atan2(self.x) self.y.atan2(self.x)
} }
pub fn swap(&self) -> Vector {
Self::new(self.y, self.x)
}
} }
#[cfg(test)] #[cfg(test)]
@ -70,4 +74,9 @@ mod test {
fn angle() { fn angle() {
assert_abs_diff_eq!(-1.5, Vector::new(2.0, -20.0).angle(), epsilon = 0.1); assert_abs_diff_eq!(-1.5, Vector::new(2.0, -20.0).angle(), epsilon = 0.1);
} }
#[test]
fn swap() {
assert_eq!(Vector::new(7.0, 2.0), Vector::new(2.0, 7.0).swap());
}
} }