Skip to content

Commit

Permalink
Calculate HSV
Browse files Browse the repository at this point in the history
  • Loading branch information
yds12 committed Aug 26, 2024
1 parent 29011ec commit c57d7ea
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
35 changes: 33 additions & 2 deletions lapix/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,34 @@ impl ColorF32 {

4.0 + (self.r - self.g) / (max - min)
};
dbg!(partial, partial * 60.0, (partial * 60.0) as i16 + 360,
((partial * 60.0) as i16 + 360) % 360 );

(((partial * 60.0).round() as i16 + 360) % 360) as u16
}

pub fn saturation(&self) -> f32 {
if self.r == self.g && self.r == self.b {
return 0.0;
}

let max = self.value();
let min = [self.r, self.g, self.b]
.into_iter()
.map(|c| (c * 1000.) as i32)
.min()
.unwrap() as f32
/ 1000.;

return (max - min) / max;

Check failure on line 141 in lapix/src/color.rs

View workflow job for this annotation

GitHub Actions / Clippy

unneeded `return` statement
}

pub fn value(&self) -> f32 {
[self.r, self.g, self.b]
.into_iter()
.map(|c| (c * 1000.) as i32)
.max()
.unwrap() as f32
/ 1000.
}
}

impl Color {
Expand Down Expand Up @@ -171,6 +194,14 @@ impl Color {
pub fn hue(&self) -> u16 {
ColorF32::from(*self).hue()
}

pub fn saturation(&self) -> f32 {
ColorF32::from(*self).saturation()
}

pub fn value(&self) -> f32 {
ColorF32::from(*self).value()
}
}

#[cfg(test)]
Expand Down
16 changes: 13 additions & 3 deletions lapix/src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ impl Palette {
break;
}
}
palette.sort_by(|a, b| a.hue().cmp(&b.hue()));
let mut palette = Self(palette);
palette.sort();

Self(palette)
palette
}

pub fn add_color(&mut self, color: Color) {
if !self.0.contains(&color) {
self.0.push(color)
}
self.0.sort_by(|a, b| a.hue().cmp(&b.hue()));
self.sort();
}

pub fn remove_color(&mut self, color: Color) {
Expand All @@ -67,6 +68,15 @@ impl Palette {
pub fn colors(&self) -> &[Color] {
&self.0
}

pub fn sort(&mut self) {
fn sort_val(color: &Color) -> i32 {
(color.hue() as i32) * 1_000_000 +
(color.saturation() * 10_000.) as i32 +
(color.value() * 10_000.) as i32
}
self.0.sort_by(|a, b| sort_val(a).cmp(&sort_val(b)));

Check failure on line 78 in lapix/src/palette.rs

View workflow job for this annotation

GitHub Actions / Clippy

consider using `sort_by_key`
}
}

#[cfg(test)]
Expand Down
8 changes: 5 additions & 3 deletions tarsila/src/gui/palette.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::wrapped_image::WrappedImage;
use crate::Effect;
use lapix::{Bitmap, Event, Color};
use lapix::{Bitmap, Color, Event};
use macroquad::prelude::Image as MqImage;

const BTN_SIZE: i32 = 20;
Expand Down Expand Up @@ -69,9 +69,11 @@ impl Palette {
ui.ctx().load_texture("", image.clone(), Default::default())
});
let tooltip = format!(
"Select color {:?} (hue: {}) (right click to remove from palette)",
"Select color {:?} (HSV: {}, {:.3}, {:.3}) (right click to remove from palette)",
self.colors[i],
Color::from(self.colors[i]).hue()
Color::from(self.colors[i]).hue(),
Color::from(self.colors[i]).saturation(),
Color::from(self.colors[i]).value()
);

let btn = egui::ImageButton::new(tex, tex.size_vec2());
Expand Down

0 comments on commit c57d7ea

Please sign in to comment.