Files
Chronos/src/Titano.Ui/Visuals/PixelGrid.cs
T
Claude 661be6c94b feat(ui-visuals): add layout, scale, decimation, viewport and curve math kernels
WPF-free geometry behind the visual controls: wrapped-grid layout with O(1)
index/offset mapping and keyboard targets, nice axis ticks, min/max
decimation, zoom/pan transforms, monotone curve editing rules, filmstrip and
heatmap cell layout, an LRU cache and device-pixel snapping, with tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 10:03:20 +00:00

33 lines
1.4 KiB
C#

namespace Titano.Ui.Visuals;
/// <summary>Device-pixel alignment for drawn lines and edges (<c>scale</c> = device pixels per DIP).</summary>
public static class PixelGrid
{
/// <summary>Rounds <paramref name="value"/> to the nearest device-pixel boundary.</summary>
public static double Snap(double value, double scale)
{
if (!(scale > 0) || !double.IsFinite(value)) return value;
return Math.Round(value * scale) / scale;
}
/// <summary>
/// Position of the centre of a line <paramref name="thickness"/> DIPs wide drawn at <paramref name="value"/> so it
/// covers whole device pixels: odd pixel widths sit on pixel centres, even ones on pixel boundaries.
/// </summary>
public static double SnapLine(double value, double thickness, double scale)
{
if (!(scale > 0) || !double.IsFinite(value)) return value;
var pixels = Math.Max(1, Math.Round(thickness * scale));
var device = value * scale;
var snapped = pixels % 2 == 1 ? Math.Floor(device) + 0.5 : Math.Round(device);
return snapped / scale;
}
/// <summary>Line thickness in DIPs that renders as whole device pixels (at least one).</summary>
public static double SnapThickness(double thickness, double scale)
{
if (!(scale > 0)) return thickness;
return Math.Max(1, Math.Round(thickness * scale)) / scale;
}
}