aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormetamuffin <yvchraiqi@protonmail.com>2022-06-05 17:33:11 +0200
committermetamuffin <yvchraiqi@protonmail.com>2022-06-05 17:33:11 +0200
commita5f64a98782cc15bb3cfbc353e271c612e1ff14a (patch)
tree87f33b12f97527430871ccf85f69b58859f1dab9 /src
parented2ff677f1977d2c163322ce9e7a55f0740d1f1a (diff)
downloadblubcat-a5f64a98782cc15bb3cfbc353e271c612e1ff14a.tar
blubcat-a5f64a98782cc15bb3cfbc353e271c612e1ff14a.tar.bz2
blubcat-a5f64a98782cc15bb3cfbc353e271c612e1ff14a.tar.zst
composite
Diffstat (limited to 'src')
-rw-r--r--src/main.rs26
-rw-r--r--src/transform.rs40
2 files changed, 64 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 482e20a..d7b1349 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, Transpose},
+ transform::{Composite, CompositeOperation, Matrix, Polar, Translate, Transpose},
Sample,
};
use std::{
@@ -139,6 +139,28 @@ fn main() -> Result<()> {
})
}
"-T" | "--transpose" => push_queue.push(box Transpose(pat.pop().unwrap())),
+
+ "--add" => push_queue.push(box Composite {
+ a: pat.pop().unwrap(),
+ b: pat.pop().unwrap(),
+ mode: CompositeOperation::Add,
+ }),
+ "--subtract" => push_queue.push(box Composite {
+ a: pat.pop().unwrap(),
+ b: pat.pop().unwrap(),
+ mode: CompositeOperation::Subtract,
+ }),
+ "--multiply" => push_queue.push(box Composite {
+ a: pat.pop().unwrap(),
+ b: pat.pop().unwrap(),
+ mode: CompositeOperation::Multiply,
+ }),
+ "--mix" => push_queue.push(box Composite {
+ a: pat.pop().unwrap(),
+ b: pat.pop().unwrap(),
+ mode: CompositeOperation::Mix(arg_num()),
+ }),
+
_ => panic!("unknown option {}", &a),
}
pat.extend(push_queue.drain(..))
@@ -167,7 +189,7 @@ fn main() -> Result<()> {
fn flag_helper(colors: &[&str]) -> Box<dyn Sample> {
box Matrix {
- matrix: ((1.0 / 12.0, 1.0 / 12.0), (0.0, 0.0)),
+ matrix: ((1.0 / 6.0, 1.0 / 6.0), (0.0, 0.0)),
inner: box Sequence(
colors
.into_iter()
diff --git a/src/transform.rs b/src/transform.rs
index 0b65728..ccb1850 100644
--- a/src/transform.rs
+++ b/src/transform.rs
@@ -39,3 +39,43 @@ impl Sample for Translate {
self.inner.sample(x - self.offset.0, y - self.offset.1)
}
}
+
+pub enum CompositeOperation {
+ Add,
+ Subtract,
+ Multiply,
+ Mix(f64),
+}
+pub struct Composite {
+ pub a: Box<dyn Sample>,
+ pub b: Box<dyn Sample>,
+ pub mode: CompositeOperation,
+}
+impl Sample for Composite {
+ fn sample(&mut self, x: f64, y: f64) -> Color {
+ let a = self.a.sample(x, y);
+ let b = self.b.sample(x, y);
+ match self.mode {
+ CompositeOperation::Add => Color {
+ r: a.r + b.r,
+ g: a.g + b.g,
+ b: a.b + b.b,
+ },
+ CompositeOperation::Subtract => Color {
+ r: a.r - b.r,
+ g: a.g - b.g,
+ b: a.b - b.b,
+ },
+ CompositeOperation::Multiply => Color {
+ r: a.r * b.r,
+ g: a.g * b.g,
+ b: a.b * b.b,
+ },
+ CompositeOperation::Mix(f) => Color {
+ r: a.r * (1.0 - f) + b.r * f,
+ g: a.g * (1.0 - f) + b.g * f,
+ b: a.b * (1.0 - f) + b.b * f,
+ },
+ }
+ }
+}