Merge pull request #10009 from TaylorBurke/fix/color-wheel-antialias-9996

Color wheel: feather the hue ring and triangle edges (#9996)
This commit is contained in:
Tanish Manku authored and GitHub committed 2026-11-11 00:12:08 -07:00
commit a44ac93dce
2 files changed
+130 -13

No files matched your search

+61
View File
@@ -339,6 +339,67 @@ fn color_wheel_ring_sets_hue_and_triangle_sets_saturation_and_brightness() {
assert!((hsv[0] - 90.0).abs() < 4.0 && hsv[1] < 0.9 && hsv[2] < 0.95, "{after:?} {hsv:?}");
}
/// The wheel's ring and triangle are meshes, which egui doesn't antialias: each edge fades to
/// transparent over the feather, centred on it, so neither looks jagged (#9996).
#[test]
fn color_wheel_edges_are_feathered() {
use crate::color_panel_ui::{ring_mesh, triangle_mesh};
let (center, inner, outer, feather) = (pos2(100.0, 100.0), 80.0, 96.0, 1.0);
let ring = ring_mesh(center, inner, outer, feather);
for v in &ring.vertices {
let r = (v.pos - center).length();
if v.color == egui::Color32::TRANSPARENT {
let fringe = (r - (inner - 0.5)).abs().min((r - (outer + 0.5)).abs());
assert!(fringe < 1e-3, "transparent vertex at radius {r}");
} else {
assert!(r > inner + 0.5 - 1e-3 && r < outer - 0.5 + 1e-3, "solid vertex at radius {r}");
assert_eq!(v.color.a(), 255);
}
}
assert!(ring.vertices.iter().any(|v| v.color == egui::Color32::TRANSPARENT));
// Hard edges: no fringe at all.
assert!(ring_mesh(center, inner, outer, 0.0).vertices.iter().all(|v| v.color.a() == 255));
// An equilateral triangle in the ring (inradius 39.5): solid vertices half a feather inside
// its edges, transparent ones half a feather outside.
let tri = [0.0f32, 1.0 / 3.0, 2.0 / 3.0].map(|t| {
let a = t * std::f32::consts::TAU;
center + egui::vec2(a.cos(), -a.sin()) * 79.0
});
// Signed distance from `p` to the nearest edge, positive inside.
let inside = |p: egui::Pos2| {
(0..3)
.map(|k| {
let (a, b) = (tri[k], tri[(k + 1) % 3]);
let n = egui::vec2(-(b - a).y, (b - a).x).normalized();
let d = (p - a).dot(n);
if (center - a).dot(n) < 0.0 { -d } else { d }
})
.fold(f32::INFINITY, f32::min)
};
let m = triangle_mesh(tri, 0.25, feather);
let (mut solid_edge, mut fringe) = (0, 0);
for v in &m.vertices {
let d = inside(v.pos);
if v.color == egui::Color32::TRANSPARENT {
assert!((d + 0.5).abs() < 1e-2, "transparent vertex {d} from the edge");
fringe += 1;
} else {
assert!(d > 0.5 - 1e-2, "solid vertex {d} from the edge");
solid_edge += usize::from((d - 0.5).abs() < 1e-2);
// It shows the colour picking reads where it is drawn.
let (s, b) = crate::color_panel_ui::triangle_sv(tri, v.pos).unwrap();
let picked = crate::color_panel_ui::rgb32(photocraft_color::convert::hsb_to_rgb([90.0, s, b]));
assert_eq!(v.color, picked, "vertex at {:?}", v.pos);
}
}
assert!(fringe >= 48 && solid_edge >= 48, "{fringe} fringe, {solid_edge} edge vertices");
// Every index points at a vertex.
assert!(m.indices.iter().all(|&i| (i as usize) < m.vertices.len()));
assert!(ring.indices.iter().all(|&i| (i as usize) < ring.vertices.len()));
assert!(triangle_mesh(tri, 0.25, 0.0).vertices.iter().all(|v| v.color.a() == 255));
}
/// Picking a mode from the panel menu changes the panel; the theme never does.
#[test]
fn panel_menu_picks_the_mode_and_ramp() {
+69 -13
View File
@@ -568,9 +568,14 @@ fn wheel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let (center, inner, outer) = wheel_geometry(area);
let ring_w = outer - inner;
let (h, s, b) = (v[0] / 360.0, v[1] / 100.0, v[2] / 100.0);
ui.painter().add(ring_mesh(center, inner, outer));
// Meshes aren't antialiased by egui: the ring and the triangle feather their own edges as
// egui's shapes do (#9996).
// Read outside the options' lock: asking the context for its scale inside it deadlocks.
let ppp = ui.ctx().pixels_per_point();
let feather = ui.ctx().tessellation_options(|o| if o.feathering { o.feathering_size_in_pixels / ppp } else { 0.0 });
ui.painter().add(ring_mesh(center, inner, outer, feather));
let tri = triangle(center, inner - 1.0, h);
ui.painter().add(triangle_mesh(tri, h));
ui.painter().add(triangle_mesh(tri, h, feather));
// Which part a drag began on: the ring sets the hue, the triangle saturation and brightness.
// The ring or the triangle, from where the press began (kept for frames without a press
// origin), so a drag that leaves the ring keeps turning the hue.
@@ -640,22 +645,29 @@ pub(crate) fn wheel_geometry(area: Rect) -> (Pos2, f32, f32) {
(area.center(), outer - ring_w, outer)
}
/// The wheel's hue ring between radii `inner` and `outer`.
fn ring_mesh(center: Pos2, inner: f32, outer: f32) -> Mesh {
/// The wheel's hue ring between radii `inner` and `outer`, its edges fading out over `feather`
/// points centred on them (0 for hard edges).
pub(crate) fn ring_mesh(center: Pos2, inner: f32, outer: f32, feather: f32) -> Mesh {
let mut m = Mesh::default();
let steps = 96u32;
let half = feather / 2.0;
// Out from the centre: transparent, the hue, the hue, transparent (all the hue for hard edges).
let radii = [(inner - half).max(0.0), inner + half, outer - half, outer + half];
for k in 0..=steps {
let f = k as f32 / steps as f32;
let a = f * TAU;
let c = rgb32(hsb_to_rgb([f * 360.0, 1.0, 1.0]));
let dir = vec2(a.cos(), -a.sin());
m.colored_vertex(center + dir * inner, c);
m.colored_vertex(center + dir * outer, c);
for (i, r) in radii.into_iter().enumerate() {
m.colored_vertex(center + dir * r, if feather > 0.0 && (i == 0 || i == 3) { Color32::TRANSPARENT } else { c });
}
}
for k in 0..steps {
let a = k * 2;
m.add_triangle(a, a + 1, a + 3);
m.add_triangle(a, a + 3, a + 2);
for band in 0..3 {
let a = k * 4 + band;
m.add_triangle(a, a + 1, a + 5);
m.add_triangle(a, a + 5, a + 4);
}
}
m
}
@@ -694,14 +706,39 @@ pub(crate) fn triangle_sv([hue, white, black]: [Pos2; 3], p: Pos2) -> Option<(f3
Some((if b > 0.0 { bs / b } else { 0.0 }, b))
}
/// The triangle shaded through saturation and brightness at hue `h`.
fn triangle_mesh(tri: [Pos2; 3], h: f32) -> Mesh {
/// `tri` with every edge moved out by `d` (in by a negative `d`): scaled about its incentre, so
/// the point of a saturation and brightness moves with it. A degenerate triangle stays as it is.
fn offset_triangle(tri: [Pos2; 3], d: f32) -> [Pos2; 3] {
let [p, q, r] = tri;
// Each corner weighs by the length of the side opposite it.
let (a, b, c) = ((q - r).length(), (r - p).length(), (p - q).length());
let perimeter = a + b + c;
let area2 = ((q - p).x * (r - p).y - (q - p).y * (r - p).x).abs();
if perimeter <= 0.0 || area2 <= 1e-6 {
return tri;
}
let incentre = ((p.to_vec2() * a + q.to_vec2() * b + r.to_vec2() * c) / perimeter).to_pos2();
let inradius = area2 / perimeter;
let k = ((inradius + d) / inradius).max(0.0);
tri.map(|v| incentre + (v - incentre) * k)
}
/// The triangle shaded through saturation and brightness at hue `h`, its edges fading out over
/// `feather` points centred on them (0 for hard edges).
pub(crate) fn triangle_mesh(tri: [Pos2; 3], h: f32, feather: f32) -> Mesh {
let n = 16usize;
let mut m = Mesh::default();
let (fill, fringe) = (offset_triangle(tri, -feather / 2.0), offset_triangle(tri, feather / 2.0));
// A vertex of the smaller triangle shows the colour picking reads at its position, which is
// on `tri`, so the drawn colour and the picked one agree.
let solid = |m: &mut Mesh, s: f32, b: f32| {
let p = triangle_point(fill, s, b);
let (s, b) = triangle_sv(tri, p).unwrap_or((s, b));
m.colored_vertex(p, rgb32(hsb_to_rgb([h * 360.0, s, b])));
};
for j in 0..=n {
for i in 0..=n {
let (s, b) = (i as f32 / n as f32, j as f32 / n as f32);
m.colored_vertex(triangle_point(tri, s, b), rgb32(hsb_to_rgb([h * 360.0, s, b])));
solid(&mut m, i as f32 / n as f32, j as f32 / n as f32);
}
}
let row = (n + 1) as u32;
@@ -712,6 +749,25 @@ fn triangle_mesh(tri: [Pos2; 3], h: f32) -> Mesh {
m.add_triangle(a, a + row + 1, a + row);
}
}
if feather <= 0.0 {
return m;
}
// The edge, once round: black up to white (s = 0), white to the hue (b = 1), the hue down to
// black (s = 1). Each point gets a transparent twin on the larger triangle.
let t = |k: usize| k as f32 / n as f32;
let edge = (0..n).map(|k| (0.0, t(k))).chain((0..n).map(|k| (t(k), 1.0))).chain((0..n).map(|k| (1.0, t(n - k))));
let start = m.vertices.len() as u32;
let mut count = 0u32;
for (s, b) in edge {
solid(&mut m, s, b);
m.colored_vertex(triangle_point(fringe, s, b), Color32::TRANSPARENT);
count += 1;
}
for k in 0..count {
let (a, next) = (start + k * 2, start + (k + 1) % count * 2);
m.add_triangle(a, a + 1, next + 1);
m.add_triangle(a, next + 1, next);
}
m
}