From ed2ff677f1977d2c163322ce9e7a55f0740d1f1a Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 5 Jun 2022 17:19:53 +0200 Subject: thing --- .gitignore | 1 + src/main.rs | 56 ++++++++++++++++++++++++++++++++++++-------------------- src/pattern.rs | 7 +++---- src/transform.rs | 9 ++++++++- 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..94067ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/test diff --git a/src/main.rs b/src/main.rs index d210aaf..482e20a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use anyhow::Result; use blubcat::{ color::Color, pattern::{Gradient, Rainbow, Sequence, Solid}, - transform::{Matrix, Polar, Translate}, + transform::{Matrix, Polar, Translate, Transpose}, Sample, }; use std::{ @@ -16,30 +16,36 @@ use std::{ }; static HELP: &str = " -Usage: blubcat [PATTERN] [FILE...] -Concatenate FILE(s) to standard output, colorizing the output according to PATTERN. +Usage: blubcat [OPTION...] [FILE...] +Concatenate FILE(s) to standard output, colorizing the output according to OPTION. With no FILE, or when FILE is -, the standard input is read. -If PATTERN is repeated only the last one will be used. +OPTION can be PATTERN, TRANSFORM or PRESET. +Patterns are stored on a stack. +, indicates the number of patters popped from the stack and how many are pushed back on. PATTERN: - -R --rainbow Sinebow - -S --sequence Sequences colors on the X-axis - -G --gradient Interpolates the colors on the X-axis - Push mulple patterns to a stack, then process them with transform - +0,1 -R --rainbow Sinebow +0,1 -K --const Solid color +0,1 -S --sequence Sequences colors on the X-axis +0,1 -G --gradient Interpolates the colors on the X-axis TRANSFORM: - -r --rotate - -s --scale - -m --matrix - -t --translate - --polar +1,1 -r --rotate +1,1 -s --scale +1,1 -m --matrix +1,1 -t --translate +1,1 -T --transpose +1,1 -p --polar +2,1 --mix +2,1 --add +2,1 --subtract +2,1 --multiply PRESET: - --zebra - --pride --trans - --ukraine ofc the this needed :) +0,1 --zebra +0,1 --pride --trans +0,1 --ukraine ofc this is needed COLOR: @@ -49,6 +55,7 @@ COLOR: fn main() -> Result<()> { let mut args = env::args().skip(1); + let mut arg_extra = None; let mut pat: Vec> = vec![]; @@ -61,9 +68,9 @@ fn main() -> Result<()> { .parse::() .expect("not a number") }; - if let Some(a) = a { if !a.starts_with("-") || a == "--" || a == "-" { + arg_extra = Some(a); break; } let mut push_queue: Vec> = vec![]; @@ -99,6 +106,10 @@ fn main() -> Result<()> { .map(|e| Color::parse(e).expect("color invalid")) .collect::>(), )), + "-K" | "--const" => pat.push(box Solid( + Color::parse(arg_next().expect("color expected").as_str()) + .expect("invalid color"), + )), /* TRANSFORMS */ "-s" | "--scale" => { @@ -119,7 +130,7 @@ fn main() -> Result<()> { inner: pat.pop().unwrap(), matrix: ((arg_num(), arg_num()), (arg_num(), arg_num())), }), - "--polar" => push_queue.push(box Polar(pat.pop().unwrap())), + "-p" | "--polar" => push_queue.push(box Polar(pat.pop().unwrap())), "-t" | "--translate" => { let (x, y) = (arg_num(), arg_num()); push_queue.push(box Translate { @@ -127,6 +138,7 @@ fn main() -> Result<()> { inner: pat.pop().unwrap(), }) } + "-T" | "--transpose" => push_queue.push(box Transpose(pat.pop().unwrap())), _ => panic!("unknown option {}", &a), } pat.extend(push_queue.drain(..)) @@ -135,9 +147,13 @@ fn main() -> Result<()> { } } - let inputs = args.collect::>(); + let mut inputs = args.collect::>(); let mut pat = pat.pop().unwrap_or_else(|| box Solid(Color::WHITE)); + if let Some(a) = arg_extra { + inputs.insert(0, a) + } + if inputs.len() == 0 { colorize(stdin(), &mut pat)? } else { diff --git a/src/pattern.rs b/src/pattern.rs index ba9899e..042d0be 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -16,16 +16,15 @@ impl Sample for Rainbow { pub struct Sequence(pub Vec); impl Sample for Sequence { fn sample(&mut self, x: f64, _y: f64) -> Color { - self.0[(x * self.0.len() as f64).floor() as usize % self.0.len()] + self.0[x.rem_euclid(self.0.len() as f64) as usize] } } pub struct Gradient(pub Vec); impl Sample for Gradient { fn sample(&mut self, x: f64, _y: f64) -> Color { - let index = x * self.0.len() as f64; - let index_int = index.floor() as usize; - let index_error = index % 1.0; + let index_int = x.floor() as usize; + let index_error = x % 1.0; let a = self.0[index_int % self.0.len()]; let b = self.0[(index_int + 1) % self.0.len()]; Color::mix(a, b, index_error) diff --git a/src/transform.rs b/src/transform.rs index 3994f1d..0b65728 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -23,12 +23,19 @@ impl Sample for Polar { } } +pub struct Transpose(pub Box); +impl Sample for Transpose { + fn sample(&mut self, x: f64, y: f64) -> Color { + self.0.sample(y, x) + } +} + pub struct Translate { pub inner: Box, pub offset: (f64, f64), } impl Sample for Translate { fn sample(&mut self, x: f64, y: f64) -> Color { - self.inner.sample(x + self.offset.0, y + self.offset.1) + self.inner.sample(x - self.offset.0, y - self.offset.1) } } -- cgit v1.2.3-70-g09d2