Skip to content

Commit

Permalink
invert and brightness
Browse files Browse the repository at this point in the history
  • Loading branch information
Toyz committed Jan 3, 2025
1 parent 4eab8ce commit 46647a5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ returning a new one. The allowed operators are:
* `R{Num}` the red color component defined by `Num` (i.e. `R255` is the same as `R`)
* `G{Num}` the green color component defined by `Num` (i.e. `G255` is the same as `G`)
* `B{Num}` the blue color component defined by `Num` (i.e. `B255` is the same as `B`)
*
* `i` inverts the color component
* `b{Num}` the brightness of the color component defined by `Num` (i.e. `b255` is the same as `b`)

### Ported from go-glitch
* `+` plus
* `-` minus
Expand Down
33 changes: 32 additions & 1 deletion src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use image::{DynamicImage, GenericImageView, Rgba};
use image::{DynamicImage, GenericImageView, Pixel, Rgba};
use rand::{Rng, RngCore};
use std::collections::HashMap;

Expand Down Expand Up @@ -283,6 +283,37 @@ pub fn eval(
_ => return Err(format!("Unexpected token: {:?}", token)),
},

Token::Brightness(brightness) => {
let pixel = input.get_pixel(x, y);
let r = pixel[0];
let g = pixel[1];
let b = pixel[2];


if brightness != 0 {
let r = (f64::from(r) * brightness as f64).round() as u8;
let g = (f64::from(g) * brightness as f64).round() as u8;
let b = (f64::from(b) * brightness as f64).round() as u8;
stack.push(RgbSum::new(r, g, b));
continue;
} else {
// get default brightness
stack.push(RgbSum::new(r, g, b));
}
}

Token::Invert => {
let pixel = input.get_pixel(x, y);
let mut new_rgba = Rgba([pixel[0], pixel[1], pixel[2], pixel[3]]);
new_rgba.invert();

let r = new_rgba[0];
let g = new_rgba[1];
let b = new_rgba[2];

stack.push(RgbSum::new(r, g, b));
}

Token::Char(c) => match c {
'c' => stack.push(RgbSum::new(r, g, b)),
'Y' => {
Expand Down
34 changes: 34 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum Token {
Num(u8),
Random(u8),
RGBColor((char, u8)),
Brightness(u8),
Add,
Sub,
Mul,
Expand All @@ -26,6 +27,7 @@ pub enum Token {
LeftParen,
RightParen,
Char(char),
Invert,
}

pub trait DisplayStyle {
Expand Down Expand Up @@ -55,6 +57,8 @@ impl DisplayStyle for Token {
Self::Weight => Style::new().fg(Color::BrightYellow),
Self::Random(_) => Style::new().fg(Color::BrightBlue),
Self::RGBColor(_) => Style::new().fg(Color::BrightBlue),
Self::Brightness(_) => Style::new().fg(Color::BrightBlue),
Self::Invert => Style::new().fg(Color::BrightBlue),
}
}
}
Expand Down Expand Up @@ -163,6 +167,12 @@ impl std::fmt::Display for Token {
}
Self::RGBColor((part, val)) => {
content = Some(format!("RGB Color - {part}: {val}").leak());
},
Self::Brightness(val) => {
content = Some(format!("Brightness - {val}").leak());
},
Self::Invert => {
content = Some("Invert");
}
_ => {}
}
Expand Down Expand Up @@ -265,6 +275,30 @@ pub fn shunting_yard(input: &str) -> Result<Vec<Token>, String> {
};
output_queue.push_back(Token::RGBColor((part, value)));
}
'b' => {
push_number_buffer(&mut number_buffer, &mut output_queue, current_position)?;
let mut value_str = String::new();
while let Some(&next_char) = chars_iter.peek() {
if next_char.is_ascii_digit() {
value_str.push(chars_iter.next().unwrap());
current_position += 1;
} else {
break;
}
}
let value = if value_str.is_empty() {
255
} else {
value_str.parse::<u8>().map_err(|_| {
format!("Invalid value specified at position {}", current_position)
})?
};
output_queue.push_back(Token::Brightness(value));
}
'i' => {
// this is just invert
output_queue.push_back(Token::Invert);
}
c if char_to_token(c).is_some() => {
push_number_buffer(&mut number_buffer, &mut output_queue, current_position)?;
if let Some(token) = char_to_token(c) {
Expand Down

0 comments on commit 46647a5

Please sign in to comment.