Skip to content

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

Brush cursor: Normal Brush Tip ring on the real 50% contour (#2744) (#2747)

Valentin Klein737d1ca7531a
diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs
index 04ac59f74..31be8aecf 100644
--- a/crates/paint/src/brush_tests.rs
+++ b/crates/paint/src/brush_tests.rs
@@ -1098,3 +1098,15 @@ fn the_tip_falls_off_from_the_hard_core_to_zero_at_the_edge() {
assert!(v.windows(2).all(|w| w[1] <= w[0]), "{v:?}");
assert!((tip_falloff(5.0, 10.0, 0.0) - 0.75f32.powi(4)).abs() < 1e-6);
}
+
+#[test]
+fn half_coverage_radius_is_the_tips_50_percent_contour() {
+ use crate::{half_coverage_radius, tip_falloff};
+ // The Normal Brush Tip cursor ring (#2744): where the soft falloff crosses 50 %.
+ for h in [0.0, 0.25, 0.5, 0.75, 0.99] {
+ let d = half_coverage_radius(10.5, h);
+ assert!((tip_falloff(d, 10.5, h) - 0.5).abs() < 1e-3, "hardness {h}: ring at {d}");
+ }
+ assert_eq!(half_coverage_radius(10.5, 1.0), 10.5);
+ assert!(half_coverage_radius(10.5, f32::NAN).is_finite());
+}
diff --git a/crates/paint/src/lib.rs b/crates/paint/src/lib.rs
index 51707201d..b81a2ae85 100644
--- a/crates/paint/src/lib.rs
+++ b/crates/paint/src/lib.rs
@@ -155,6 +155,16 @@ pub fn tip_falloff(d: f32, radius: f32, hardness: f32) -> f32 {
s2 * s2
}
+/// Distance from the centre where [`tip_falloff`] drops to 50 %: the contour Preferences ›
+/// Cursors › Normal Brush Tip outlines. It inverts `(1 - t²)⁴ = ½`, so `t = √(1 - ½^¼) ≈ 0.399`
+/// of the way from the hard core (`hardness · radius`) to the edge.
+#[inline]
+pub fn half_coverage_radius(radius: f32, hardness: f32) -> f32 {
+ let h = if hardness.is_nan() { 0.0 } else { hardness.clamp(0.0, 1.0) };
+ let t = (1.0 - 0.5f32.sqrt().sqrt()).sqrt();
+ radius * (h + t * (1.0 - h))
+}
+
/// Coverage of a round dab at distance `d` from its centre (anti-aliased edge, hardness falloff).
#[inline]
pub fn dab_coverage(d: f32, radius: f32, hardness: f32) -> f32 {
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 783d56466..c834c4fbe 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -2999,7 +2999,11 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
let painting = app.drag.is_some();
let brush = &app.session.tools.brush;
let full = (brush.size / 2.0 * xf.zoom).max(1.0);
- let r = if cur.painting == PaintingCursor::NormalTip { (full * (0.5 + 0.5 * brush.hardness.clamp(0.0, 1.0))).max(1.0) } else { full };
+ let r = if cur.painting == PaintingCursor::NormalTip {
+ photocraft_paint::half_coverage_radius(full, brush.hardness).max(1.0)
+ } else {
+ full
+ };
match cur.painting {
PaintingCursor::Standard => egui::CursorIcon::Default,
PaintingCursor::Precise => crate::tool_cursor::crosshair(&painter, p, 6.0, 0.0),