Add units transformation

This commit is contained in:
Rico Riedel 2022-08-04 17:41:56 +02:00
parent c80dac4c4e
commit 711ba4850c
No known key found for this signature in database
GPG Key ID: 75AC868575DE7B18
3 changed files with 43 additions and 0 deletions

View File

@ -46,6 +46,8 @@ struct Args {
#[clap(long)]
char_swap: Option<bool>,
#[clap(long)]
char_units: Option<u8>,
#[clap(long)]
char_slices: Option<u8>,
#[clap(long, value_enum)]
colors: Vec<PalletEnum>,
@ -103,6 +105,7 @@ struct PatternConfig<'a> {
shift: Option<bool>,
invert: Option<bool>,
swap: Option<bool>,
units: Option<u8>,
slices: Option<u8>,
}
@ -113,6 +116,7 @@ impl Args {
Some(true),
self.char_invert,
self.char_swap,
self.char_units,
self.char_slices,
)
}
@ -123,6 +127,7 @@ impl Args {
self.color_shift,
self.color_invert,
self.color_swap,
Some(1),
self.color_slices,
)
}
@ -176,6 +181,7 @@ impl<'a> PatternConfig<'a> {
fn create(&self, rand: &mut impl Rng) -> Box<dyn PatternFactory> {
let mut pattern = self.create_base(rand);
let units = self.units.unwrap_or(rand.gen_range(1..=4));
let slices = self.slices.unwrap_or(rand.gen_range(1..=4));
if self.shift.unwrap_or(rand.gen()) {
@ -187,6 +193,9 @@ impl<'a> PatternConfig<'a> {
if self.swap.unwrap_or(rand.gen()) {
pattern = Box::new(SwapFactory::new(pattern))
}
if units != 1 {
pattern = Box::new(UnitsFactory::new(pattern, units));
}
if slices != 1 {
pattern = Box::new(SliceFactory::new(pattern, slices));
}

View File

@ -2,8 +2,10 @@ mod invert;
mod shift;
mod slice;
mod swap;
mod units;
pub use invert::*;
pub use shift::*;
pub use slice::*;
pub use swap::*;
pub use units::*;

32
src/transform/units.rs Normal file
View File

@ -0,0 +1,32 @@
use crate::pattern::*;
use crate::Vector;
#[derive(derive_more::Constructor)]
pub struct UnitsFactory {
child: Box<dyn PatternFactory>,
units: u8,
}
#[derive(derive_more::Constructor)]
pub struct Units {
child: Box<dyn Pattern>,
units: u8,
}
impl PatternFactory for UnitsFactory {
fn create(&self, config: &Config) -> Box<dyn Pattern> {
Box::new(Units::new(self.child.create(config), self.units))
}
}
impl Pattern for Units {
fn sample(&self, pos: Vector) -> f32 {
let sample = self.child.sample(pos);
if 0.0 <= sample && sample < 1.0 {
(sample * self.units as f32) % 1.0
} else {
sample
}
}
}