Skip to content

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

document the shared hsb conversions

Omar Saidahmedab16e0b4b7f9
diff --git a/crates/color/src/convert.rs b/crates/color/src/convert.rs
index 6b864a9b6..6092dc397 100644
--- a/crates/color/src/convert.rs
+++ b/crates/color/src/convert.rs
@@ -213,6 +213,7 @@ pub fn rgb_to_gray(rgb: [f32; 3]) -> f32 {
0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]
}
+/// HSB of an sRGB colour with the hue in sextants (0..6) instead of degrees, and hue 0 for greys.
pub fn rgb_to_hsb_sextant(rgb: [f32; 3]) -> [f32; 3] {
let (mx, mn) = (rgb[0].max(rgb[1]).max(rgb[2]), rgb[0].min(rgb[1]).min(rgb[2]));
let d = mx - mn;
@@ -228,6 +229,7 @@ pub fn rgb_to_hsb_sextant(rgb: [f32; 3]) -> [f32; 3] {
[h, if mx > 0.0 { d / mx } else { 0.0 }, mx]
}
+/// The sRGB colour at a hue sextant (0..6) with the given chroma, raised by offset on every channel.
pub fn rgb_from_hue_sextant(sextant: f32, chroma: f32, offset: f32) -> [f32; 3] {
let x = chroma * (1.0 - (sextant.rem_euclid(2.0) - 1.0).abs());
let (r, g, b) = match sextant as u32 {
@@ -241,11 +243,13 @@ pub fn rgb_from_hue_sextant(sextant: f32, chroma: f32, offset: f32) -> [f32; 3]
[r + offset, g + offset, b + offset]
}
+/// HSB to sRGB, the hue in degrees (wrapping) and saturation and brightness in 0..1, as Photoshop's HSB.
pub fn hsb_to_rgb([h, s, v]: [f32; 3]) -> [f32; 3] {
let chroma = v * s;
rgb_from_hue_sextant(h.rem_euclid(360.0) / 60.0, chroma, v - chroma)
}
+/// sRGB to HSB, the hue in degrees 0..360 and saturation and brightness in 0..1.
pub fn rgb_to_hsb(rgb: [f32; 3]) -> [f32; 3] {
let [h, s, v] = rgb_to_hsb_sextant(rgb);
[60.0 * h, s, v]
@@ -332,6 +336,7 @@ mod tests {
});
}
+ /// Lab round-trips sRGB and puts white at L 100 and black at L 0.
#[test]
fn lab_roundtrip_and_anchors() {
let white = srgb_to_lab([1.0, 1.0, 1.0]);
@@ -346,6 +351,7 @@ mod tests {
}
}
+ /// HSB hits the primary and grey anchors and round-trips sRGB.
#[test]
fn hsb_conversions_match_known_colours_and_round_trip() {
assert_eq!(hsb_to_rgb([120.0, 1.0, 1.0]), [0.0, 1.0, 0.0]);
diff --git a/crates/ui-egui/src/color_picker_ui.rs b/crates/ui-egui/src/color_picker_ui.rs
index 4f63c2935..fcbc61ee0 100644
--- a/crates/ui-egui/src/color_picker_ui.rs
+++ b/crates/ui-egui/src/color_picker_ui.rs
@@ -462,6 +462,7 @@ pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) -> Option<bool> {
outcome
}
+/// The Color Picker's colour field and component slider, editing the colour in f.
fn color_field(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
let t = Tokens::get(ui.ctx());
let mode = f.get("__mode").and_then(Value::as_str).unwrap_or("h").to_string();
@@ -1023,6 +1024,7 @@ mod tests {
assert_eq!(color(&h), "#123456");
}
+ /// Only Web Colors snaps hex, field and component edits to web-safe colours.
#[test]
fn web_only_snaps_hex_field_and_component_edits() {
let mut f = Map::new();
@@ -1107,6 +1109,7 @@ mod tests {
}
}
+ /// Hex parsing refuses text whose bytes split a character instead of panicking.
#[test]
fn parse_hex_rejects_multibyte_text() {
// Six bytes, three characters: the byte range 0..2 splits "é", which used to panic.
@@ -1114,8 +1117,10 @@ mod tests {
assert_eq!(parse_hex("#ff880"), None);
}
+ /// The shared HSB conversions give bit-identical output to the picker's old copies.
#[test]
fn shared_hsb_output_matches_the_old_picker_conversions() {
+ /// The picker's HSB to sRGB before it used photocraft-color.
fn old_hsv_to_rgb(h: f32, s: f32, v: f32) -> [f32; 3] {
let h = (h.rem_euclid(360.0)) / 60.0;
let c = v * s;
@@ -1131,6 +1136,7 @@ mod tests {
let m = v - c;
[r + m, g + m, b + m]
}
+ /// The picker's sRGB to HSB before it used photocraft-color.
fn old_rgb_to_hsv(c: [f32; 3]) -> [f32; 3] {
let (mx, mn) = (c[0].max(c[1]).max(c[2]), c[0].min(c[1]).min(c[2]));
let d = mx - mn;
@@ -1163,6 +1169,7 @@ mod tests {
}
}
+ /// HSB and hex round-trip sRGB colours.
#[test]
fn hsv_round_trips() {
for c in [[1.0, 0.0, 0.0], [0.2, 0.6, 0.4], [0.5, 0.5, 0.5], [0.0, 0.0, 1.0], [0.9, 0.8, 0.1]] {
@@ -1176,6 +1183,7 @@ mod tests {
assert_eq!(parse_hex("#FF8000").map(hex).as_deref(), Some("#ff8000"));
}
+ /// Locating a colour on the field and reading the field back agree in every mode.
#[test]
fn field_and_locate_are_inverse_in_every_mode() {
let rgb = [0.8, 0.3, 0.2];
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 2f102f96a..a596adc2d 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -3955,6 +3955,7 @@ fn srgb_hsva(c: [f32; 4]) -> egui::ecolor::Hsva {
egui::ecolor::Hsva::new(h / 360.0, s, v, 1.0)
}
+/// The inverse of srgb_hsva: the Color panel's HSB back to sRGB-encoded values.
fn hsva_srgb(h: egui::ecolor::Hsva) -> [f32; 4] {
let [r, g, b] = photocraft_color::convert::hsb_to_rgb([h.h * 360.0, h.s.clamp(0.0, 1.0), h.v.clamp(0.0, 1.0)]);
[r, g, b, 1.0]