PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge PR #8091: Reject '+' signs in color picker hex field
aionggfa09db7d21c4
diff --cc crates/ui-egui/src/color_picker_ui.rsindex ac72e5b9e,805ada3e9..2a1cdb536--- a/crates/ui-egui/src/color_picker_ui.rs+++ b/crates/ui-egui/src/color_picker_ui.rs@@@ -1146,59 -1145,13 +1147,66 @@@ mod testsassert_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;+ let x = c * (1.0 - (h % 2.0 - 1.0).abs());+ let (r, g, b) = match h as u32 {+ 0 => (c, x, 0.0),+ 1 => (x, c, 0.0),+ 2 => (0.0, c, x),+ 3 => (0.0, x, c),+ 4 => (x, 0.0, c),+ _ => (c, 0.0, x),+ };+ 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;+ let h = if d <= 0.0 {+ 0.0+ } else if mx == c[0] {+ 60.0 * ((c[1] - c[2]) / d).rem_euclid(6.0)+ } else if mx == c[1] {+ 60.0 * ((c[2] - c[0]) / d + 2.0)+ } else {+ 60.0 * ((c[0] - c[1]) / d + 4.0)+ };+ [h, if mx <= 0.0 { 0.0 } else { d / mx }, mx]+ }+ let bits = |c: [f32; 3]| c.map(f32::to_bits);+ for h in (0..=720).map(|i| i as f32 * 0.5) {+ for s in (0..=20).map(|i| i as f32 / 20.0) {+ for v in (0..=20).map(|i| i as f32 / 20.0) {+ assert_eq!(bits(hsb_to_rgb([h, s, v])), bits(old_hsv_to_rgb(h, s, v)), "{h} {s} {v}");+ }+ }+ }+ for r in 0..=32 {+ for g in 0..=32 {+ for b in 0..=32 {+ let c = [r as f32 / 32.0, g as f32 / 32.0, b as f32 / 32.0];+ assert_eq!(bits(rgb_to_hsb(c)), bits(old_rgb_to_hsv(c)), "{c:?}");+ }+ }+ }+ }++ /// HSB and hex round-trip sRGB colours.+ #[test]+ fn parse_hex_rejects_signs() {+ assert_eq!(parse_hex("#+f+f+f"), None);+ assert_eq!(parse_hex("+1+2+3"), None);+ assert!(parse_hex("#0f0f0f").is_some());+ }+#[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]] {