diff options
author | metamuffin <yvchraiqi@protonmail.com> | 2022-06-05 17:19:53 +0200 |
---|---|---|
committer | metamuffin <yvchraiqi@protonmail.com> | 2022-06-05 17:19:53 +0200 |
commit | ed2ff677f1977d2c163322ce9e7a55f0740d1f1a (patch) | |
tree | bad9701c11004f2faa05ce21d31a97cd4d874ef3 | |
parent | 280cc84e641872564e35d801fbdc7990e013a0e7 (diff) | |
download | blubcat-ed2ff677f1977d2c163322ce9e7a55f0740d1f1a.tar blubcat-ed2ff677f1977d2c163322ce9e7a55f0740d1f1a.tar.bz2 blubcat-ed2ff677f1977d2c163322ce9e7a55f0740d1f1a.tar.zst |
thing
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | src/main.rs | 56 | ||||
-rw-r--r-- | src/pattern.rs | 7 | ||||
-rw-r--r-- | src/transform.rs | 9 |
4 files changed, 48 insertions, 25 deletions
@@ -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. +<pop>,<push> indicates the number of patters popped from the stack and how many are pushed back on. PATTERN: - -R --rainbow Sinebow - -S --sequence <c1,c2,c3,...> Sequences colors on the X-axis - -G --gradient <c1,c2,c3,...> Interpolates the colors on the X-axis - <PATTERN...> <TRANSFORM> Push mulple patterns to a stack, then process them with transform - <PRESET> +0,1 -R --rainbow Sinebow +0,1 -K --const <color> Solid color +0,1 -S --sequence <c1,c2,c3,...> Sequences colors on the X-axis +0,1 -G --gradient <c1,c2,c3,...> Interpolates the colors on the X-axis TRANSFORM: - -r --rotate <angle> - -s --scale <factor> - -m --matrix <x> <y> <z> <w> - -t --translate <x> <y> - --polar +1,1 -r --rotate <angle> +1,1 -s --scale <factor> +1,1 -m --matrix <x> <y> <z> <w> +1,1 -t --translate <x> <y> +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: <rrggbb> @@ -49,6 +55,7 @@ COLOR: fn main() -> Result<()> { let mut args = env::args().skip(1); + let mut arg_extra = None; let mut pat: Vec<Box<dyn Sample>> = vec![]; @@ -61,9 +68,9 @@ fn main() -> Result<()> { .parse::<f64>() .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<Box<dyn Sample>> = vec![]; @@ -99,6 +106,10 @@ fn main() -> Result<()> { .map(|e| Color::parse(e).expect("color invalid")) .collect::<Vec<_>>(), )), + "-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::<Vec<String>>(); + let mut inputs = args.collect::<Vec<String>>(); 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<Color>); 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<Color>); 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<dyn Sample>); +impl Sample for Transpose { + fn sample(&mut self, x: f64, y: f64) -> Color { + self.0.sample(y, x) + } +} + pub struct Translate { pub inner: Box<dyn Sample>, 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) } } |