Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Hue/Saturation: hue tracks and spectrum bars use full-strength sRGB hues, one shared hue_color (#6540)

MarekSpodBiedry10fc2a3839d5
diff --git a/crates/ui-egui/src/adjust_editors.rs b/crates/ui-egui/src/adjust_editors.rs
index 87a81b8c3..d6f93726f 100644
--- a/crates/ui-egui/src/adjust_editors.rs
+++ b/crates/ui-egui/src/adjust_editors.rs
@@ -21,7 +21,7 @@ use serde_json::{Value, json};
use crate::PhotocraftApp;
use crate::theme::Tokens;
use crate::tone::{self, HistSource, Histograms};
-use crate::widgets;
+use crate::widgets::{self, hue_color};
/// Kinds edited here (Selective Color and Color Lookup have their own editors; Invert has none).
pub const KINDS: [&str; 13] = [
@@ -848,11 +848,6 @@ fn threshold(ui: &mut egui::Ui, v: &mut Value, cx: &EditorCx) -> Edit {
const RANGE_LABELS: [&str; 6] = ["Reds", "Yellows", "Greens", "Cyans", "Blues", "Magentas"];
-fn hue_color(deg: f32) -> Color32 {
- let rgb = egui::ecolor::Hsva::new(deg.rem_euclid(360.0) / 360.0, 1.0, 1.0, 1.0).to_srgb();
- Color32::from_rgb(rgb[0], rgb[1], rgb[2])
-}
-
/// The Saturation slider's track: grey to the hue at full saturation (UI-217-16).
fn saturation_track(hue_deg: f32) -> [Color32; 2] {
[Color32::from_gray(128), hue_color(hue_deg)]
@@ -885,7 +880,7 @@ fn adjusted_spectrum(v: &Value, n: usize) -> Vec<Color32> {
let rect = photocraft_geom::Rect::new(0, 0, n as i32, 1);
let px = (0..n)
.map(|i| {
- let c = egui::ecolor::Hsva::new(i as f32 / (n - 1).max(1) as f32, 1.0, 1.0, 1.0).to_rgb();
+ let c = crate::color_picker_ui::hsv_to_rgb(i as f32 / (n - 1).max(1) as f32 * 360.0, 1.0, 1.0);
[c[0], c[1], c[2], 1.0]
})
.collect();
diff --git a/crates/ui-egui/src/camera_raw_ui.rs b/crates/ui-egui/src/camera_raw_ui.rs
index d2f78a35c..562d3d5dd 100644
--- a/crates/ui-egui/src/camera_raw_ui.rs
+++ b/crates/ui-egui/src/camera_raw_ui.rs
@@ -1215,7 +1215,7 @@ fn draw(app: &mut PhotocraftApp, ctx: &egui::Context, own_window: bool) {
};
for (k, name) in BANDS.iter().enumerate() {
let c = photocraft_algo::camera_raw::HSL_BANDS[k];
- let base = hue_color(c);
+ let base = widgets::hue_color(c);
let grad = [Color32::from_gray(128), base];
row(ui, &mut dirty, name, &mut arr[k], -100.0..=100.0, Some(&grad));
}
@@ -1308,20 +1308,6 @@ fn draw(app: &mut PhotocraftApp, ctx: &egui::Context, own_window: bool) {
}
}
-fn hue_color(h: f32) -> Color32 {
- let h6 = (h.rem_euclid(360.0)) / 60.0;
- let x = 1.0 - ((h6 % 2.0) - 1.0).abs();
- let (r, g, b) = match h6 as u32 {
- 0 => (1.0, x, 0.0),
- 1 => (x, 1.0, 0.0),
- 2 => (0.0, 1.0, x),
- 3 => (0.0, x, 1.0),
- 4 => (x, 0.0, 1.0),
- _ => (1.0, 0.0, x),
- };
- Color32::from_rgb((r * 220.0) as u8, (g * 220.0) as u8, (b * 220.0) as u8)
-}
-
#[cfg(test)]
mod tests {
use super::*;
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index 2acbdf3c8..051fa7601 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -1332,15 +1332,17 @@ pub fn vline(ui: &mut Ui, height: f32) {
ui.painter().line_segment([r.center_top(), r.center_bottom()], Stroke::new(1.0, t.separator));
}
-/// Hue spectrum stops for colour sliders.
+/// The fully saturated colour at hue `deg` (degrees), built on sRGB values as Photoshop's hue
+/// tracks are: 0 is (255,0,0), 60 is (255,255,0), 120 is (0,255,0) and so on (#6540).
+pub fn hue_color(deg: f32) -> Color32 {
+ let [r, g, b] = crate::color_picker_ui::hsv_to_rgb(deg, 1.0, 1.0);
+ let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
+ Color32::from_rgb(q(r), q(g), q(b))
+}
+
+/// Hue spectrum stops for colour sliders: red, yellow, green, cyan, blue, magenta, red.
pub fn hue_stops() -> Vec<Color32> {
- (0..=12)
- .map(|i| {
- let h = i as f32 / 12.0;
- let rgb = egui::ecolor::Hsva::new(h, 0.85, 1.0, 1.0).to_srgb();
- Color32::from_rgb(rgb[0], rgb[1], rgb[2])
- })
- .collect()
+ (0..=12).map(|i| hue_color(i as f32 * 30.0)).collect()
}
/// A compact labelled dropdown in the studio style.
@@ -1775,7 +1777,20 @@ fn clean(text: &str) -> String {
#[cfg(test)]
mod tests {
- use egui::{Key, Modifiers};
+ use super::{hue_color, hue_stops};
+ use egui::{Color32, Key, Modifiers};
+
+ #[test]
+ fn hue_stops_are_pure_and_evenly_spaced() {
+ let c = |r, g, b| Color32::from_rgb(r, g, b);
+ let want = [c(255, 0, 0), c(255, 255, 0), c(0, 255, 0), c(0, 255, 255), c(0, 0, 255), c(255, 0, 255), c(255, 0, 0)];
+ let stops = hue_stops();
+ assert_eq!(stops.len(), 13);
+ for (k, w) in want.iter().enumerate() {
+ assert_eq!(stops[k * 2], *w, "stop at {} degrees", k * 60);
+ assert_eq!(hue_color(k as f32 * 60.0), *w);
+ }
+ }
#[test]
fn checkbox_label_is_a_button_target_not_selectable_text() {