Skip to content

Commit

Permalink
Add apply palette transform
Browse files Browse the repository at this point in the history
  • Loading branch information
yds12 committed Aug 12, 2024
1 parent 6092e73 commit 27dfcd7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lapix/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ impl ColorF32 {
pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}

pub fn dist(&self, other: &Self) -> f32 {
((self.r - other.r).powf(2.)
+ (self.g - other.g).powf(2.)
+ (self.b - other.b).powf(2.)
+ (self.a - other.a).powf(2.))
.sqrt()
}
}

impl Color {
Expand Down
2 changes: 1 addition & 1 deletion lapix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod util;
use action::{Action, AtomicAction};
pub use bitmap::Bitmap;
pub use canvas::{Canvas, CanvasEffect};
pub use color::Color;
pub use color::{Color, ColorF32};
pub use error::{Error, Result};
pub use event::Event;
pub use floating::FreeImage;
Expand Down
4 changes: 3 additions & 1 deletion lapix/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,10 @@ impl<IMG: Bitmap + Serialize + for<'de> Deserialize<'de>> State<IMG> {
if let Some(Selection::Canvas(_)) = self.selection {
self.free_image_from_selection(None);
}

let palette = self.palette().to_vec();
if let Some(free_img) = self.free_image.as_mut() {
t.apply(&mut free_img.texture);
t.apply(&mut free_img.texture, palette);
}
}
Event::NewLayerAbove => {
Expand Down
30 changes: 28 additions & 2 deletions lapix/src/transform.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
//! Functions that can be applied to an image, modifying it
use crate::{color, Bitmap};
use crate::Color;
use crate::{color, Bitmap, ColorF32};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Transform {
Identity,
Silhouete,
ApplyPalette,
}

impl Transform {
pub fn apply<IMG: Bitmap>(&self, image: &mut IMG) {
pub fn apply<IMG: Bitmap>(&self, image: &mut IMG, palette: Vec<Color>) {
match self {
Self::Identity => (),
Self::Silhouete => Self::silhouette(image),
Self::ApplyPalette => Self::apply_palette(image, &palette),
}
}

Expand All @@ -28,4 +31,27 @@ impl Transform {
}
}
}

fn apply_palette<IMG: Bitmap>(image: &mut IMG, palette: &[Color]) {
for i in 0..image.width() {
for j in 0..image.height() {
let p = (i, j).into();
let color = image.pixel(p);

let mut min_dist = f32::MAX;
let mut min_index = 0;
for (i, palette_color) in palette.iter().enumerate() {
let colorf: ColorF32 = (*palette_color).into();
let dist = colorf.dist(&color.into());

if dist < min_dist {
min_dist = dist;
min_index = i;
}
}

image.set_pixel(p, palette[min_index]);
}
}
}
}
4 changes: 4 additions & 0 deletions tarsila/src/gui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ impl MenuBar {
ui.close_menu();
events.push(Event::ApplyTransform(Transform::Silhouete).into());
}
if ui.button("Apply palette").clicked() {
ui.close_menu();
events.push(Event::ApplyTransform(Transform::ApplyPalette).into());
}
});
});
});
Expand Down

0 comments on commit 27dfcd7

Please sign in to comment.