Skip to content

Commit

Permalink
Sort palette by hue
Browse files Browse the repository at this point in the history
  • Loading branch information
yds12 committed Aug 26, 2024
1 parent b89744a commit 29011ec
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
43 changes: 43 additions & 0 deletions lapix/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,33 @@ impl ColorF32 {
+ (self.a - other.a).powf(2.))
.sqrt()
}

pub fn hue(&self) -> u16 {
if self.r == self.g && self.r == self.b {
return 0;
}

let partial = if self.r >= self.g && self.r >= self.b {
let max = self.r;
let min = if self.g < self.b { self.g } else { self.b };

(self.g - self.b) / (max - min)
} else if self.g >= self.r && self.g >= self.b {
let max = self.g;
let min = if self.r < self.b { self.r } else { self.b };

2.0 + (self.b - self.r) / (max - min)
} else {
let max = self.b;
let min = if self.r < self.g { self.r } else { self.g };

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
}
}

impl Color {
Expand Down Expand Up @@ -140,6 +167,10 @@ impl Color {
pub fn hex(&self) -> String {
format!("#{:02X}{:02X}{:02X}{:02X}", self.r, self.g, self.b, self.a)
}

pub fn hue(&self) -> u16 {
ColorF32::from(*self).hue()
}
}

#[cfg(test)]
Expand Down Expand Up @@ -198,4 +229,16 @@ mod tests {
fn hex_val(color: impl Into<Color>, hex: &str) {
assert_eq!(color.into().hex(), hex);
}

#[test_case((255, 0, 0, 255), 0)]
#[test_case((255, 255, 0, 255), 60)]
#[test_case((0, 255, 0, 255), 120)]
#[test_case((0, 255, 255, 255), 180)]
#[test_case((0, 0, 255, 255), 240)]
#[test_case((255, 0, 255, 255), 300)]
#[test_case((45, 100, 200, 255), 219)]
#[test_case((128, 210, 77, 255), 97)]
fn hue(color: impl Into<Color>, hue: u16) {
assert_eq!(color.into().hue(), hue);
}
}
2 changes: 2 additions & 0 deletions lapix/src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Palette {
break;
}
}
palette.sort_by(|a, b| a.hue().cmp(&b.hue()));

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

View workflow job for this annotation

GitHub Actions / Clippy

consider using `sort_by_key`

Self(palette)
}
Expand All @@ -56,6 +57,7 @@ impl Palette {
if !self.0.contains(&color) {
self.0.push(color)
}
self.0.sort_by(|a, b| a.hue().cmp(&b.hue()));

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

View workflow job for this annotation

GitHub Actions / Clippy

consider using `sort_by_key`
}

pub fn remove_color(&mut self, color: Color) {
Expand Down
7 changes: 4 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};
use lapix::{Bitmap, Event, Color};
use macroquad::prelude::Image as MqImage;

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

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

0 comments on commit 29011ec

Please sign in to comment.