refactor(core): swap an immutable part palette instead of mutating Drawing statics

Drawing.PartColors (a public array aliasing the active scheme's), Drawing.UseGoldenAngleColors and a non-atomic colour counter were process-global display state. PartPalette now holds a copy of the colours and the golden-angle flag; PartColorPalette.Current/Use swap it as a whole, Drawing.GetPartColor/GetNextColor read it, and the counter increments atomically. Colours shown are unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Fable 5.2 committed 2026-11-12 19:20:22 -05:00
1 parent 986167a411
commit b77b3b7a33
9 files changed
+120 -41

No files matched your search

+4 -26
View File
@@ -16,33 +16,11 @@ namespace OpenNest
private static int nextColorIndex;
private Program program;
public static Color[] PartColors = new Color[]
{
Color.FromArgb(205, 92, 92), // Indian Red
Color.FromArgb(148, 103, 189), // Medium Purple
Color.FromArgb(75, 180, 175), // Teal
Color.FromArgb(210, 190, 75), // Goldenrod
Color.FromArgb(190, 85, 175), // Orchid
Color.FromArgb(185, 115, 85), // Sienna
Color.FromArgb(120, 100, 190), // Slate Blue
Color.FromArgb(200, 100, 140), // Rose
Color.FromArgb(80, 175, 155), // Sea Green
Color.FromArgb(195, 160, 85), // Dark Khaki
Color.FromArgb(175, 95, 160), // Plum
Color.FromArgb(215, 130, 130), // Light Coral
};
/// <summary>The active palette's color for a drawing at <paramref name="index"/>.</summary>
public static Color GetPartColor(int index) => PartColorPalette.Current.ColorAt(index);
public static bool UseGoldenAngleColors { get; set; }
public static Color GetPartColor(int index)
{
ArgumentOutOfRangeException.ThrowIfNegative(index);
return UseGoldenAngleColors
? PartColorPalette.GoldenAngle(index)
: PartColors[index % PartColors.Length];
}
public static Color GetNextColor() => GetPartColor(unchecked(nextColorIndex++) & int.MaxValue);
public static Color GetNextColor() =>
GetPartColor(unchecked(Interlocked.Increment(ref nextColorIndex) - 1) & int.MaxValue);
public Drawing()
: this(string.Empty, new Program()) { }
+15
View File
@@ -1,5 +1,6 @@
using System;
using System.Drawing;
using System.Threading;
namespace OpenNest
{
@@ -26,6 +27,20 @@ namespace OpenNest
private const double BandStart = 95.0;
private const double BandWidth = 75.0;
private static PartPalette current = PartPalette.Classic;
/// <summary>
/// The palette drawings take their colors from, process-wide. Replaced as a whole by
/// <see cref="Use"/>, so a reader never sees one palette's colors with another's flag.
/// </summary>
public static PartPalette Current => Volatile.Read(ref current);
public static void Use(PartPalette palette)
{
ArgumentNullException.ThrowIfNull(palette);
Volatile.Write(ref current, palette);
}
public static Color GoldenAngle(int index)
{
ArgumentOutOfRangeException.ThrowIfNegative(index);
+55
View File
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace OpenNest
{
/// <summary>
/// The colors drawings are given by position. Immutable: it copies the colors it is given,
/// so a caller's later edits to its own array never change what drawings receive.
/// </summary>
public sealed class PartPalette
{
private readonly Color[] colors;
public PartPalette(IEnumerable<Color> colors, bool useGoldenAngle = false)
{
ArgumentNullException.ThrowIfNull(colors);
this.colors = colors.ToArray();
Colors = Array.AsReadOnly(this.colors);
UseGoldenAngle = useGoldenAngle;
}
/// <summary>The default palette, also the fallback before a color scheme is applied.</summary>
public static PartPalette Classic { get; } = new PartPalette(new[]
{
Color.FromArgb(205, 92, 92), // Indian Red
Color.FromArgb(148, 103, 189), // Medium Purple
Color.FromArgb(75, 180, 175), // Teal
Color.FromArgb(210, 190, 75), // Goldenrod
Color.FromArgb(190, 85, 175), // Orchid
Color.FromArgb(185, 115, 85), // Sienna
Color.FromArgb(120, 100, 190), // Slate Blue
Color.FromArgb(200, 100, 140), // Rose
Color.FromArgb(80, 175, 155), // Sea Green
Color.FromArgb(195, 160, 85), // Dark Khaki
Color.FromArgb(175, 95, 160), // Plum
Color.FromArgb(215, 130, 130), // Light Coral
});
/// <summary>The explicit colors, cycled by <see cref="ColorAt"/> unless <see cref="UseGoldenAngle"/>.</summary>
public IReadOnlyList<Color> Colors { get; }
/// <summary>When set, colors are generated by <see cref="PartColorPalette.GoldenAngle"/> instead of cycled.</summary>
public bool UseGoldenAngle { get; }
public Color ColorAt(int index)
{
ArgumentOutOfRangeException.ThrowIfNegative(index);
return UseGoldenAngle
? PartColorPalette.GoldenAngle(index)
: colors[index % colors.Length];
}
}
}
@@ -124,7 +124,38 @@ public class ColorSchemeRegistryTests : IDisposable
Assert.Equal("Halloween", applied.Name);
Assert.Equal("Halloween", ColorScheme.Default.Name);
Assert.Equal(applied.BackgroundColor, ColorScheme.Default.BackgroundColor);
Assert.Equal(applied.PartColors, Drawing.PartColors);
Assert.Equal(applied.PartColors, PartColorPalette.Current.Colors);
}
[Fact]
public void EditingASchemesColorsAfterApplyingDoesNotRecolorDrawings()
{
var scheme = new ColorScheme
{
Name = "Mutable",
PartColors = [Color.FromArgb(10, 20, 30), Color.FromArgb(40, 50, 60)],
};
ColorSchemeRegistry.Apply(scheme);
var palette = PartColorPalette.Current;
scheme.PartColors[0] = Color.Black;
scheme.UseGoldenAngleColors = true;
Assert.Same(palette, PartColorPalette.Current);
Assert.Equal(Color.FromArgb(10, 20, 30), Drawing.GetPartColor(0));
Assert.Equal(Color.FromArgb(40, 50, 60), Drawing.GetPartColor(3));
}
[Fact]
public void ANewSchemeOwnsItsOwnCopyOfTheClassicColors()
{
var first = new ColorScheme();
var second = new ColorScheme();
first.PartColors[0] = Color.Black;
Assert.Equal(PartPalette.Classic.Colors, second.PartColors);
Assert.NotEqual(Color.Black, PartPalette.Classic.ColorAt(0));
}
[Theory]
@@ -163,7 +194,7 @@ public class ColorSchemeRegistryTests : IDisposable
Assert.Same(workshop, raised);
Assert.Equal(Snapshot(workshop), Snapshot(ColorScheme.Default));
Assert.NotEqual(defaultVersion, ColorScheme.Default.Version);
Assert.True(Drawing.UseGoldenAngleColors);
Assert.True(PartColorPalette.Current.UseGoldenAngle);
Assert.Equal(PartColorPalette.GoldenAngle(30), Drawing.GetPartColor(30));
}
@@ -230,15 +261,13 @@ public sealed class ColorSchemeState
private readonly ColorScheme previous =
ColorSchemeSerializer.Deserialize(ColorSchemeSerializer.Serialize(ColorScheme.Default));
private readonly Color[] previousPartColors = Drawing.PartColors;
private readonly bool previousGoldenAngle = Drawing.UseGoldenAngleColors;
private readonly PartPalette previousPalette = PartColorPalette.Current;
private readonly string previousDirectory = ColorSchemeRegistry.SchemesDirectory;
public void Restore()
{
ColorSchemeRegistry.Apply(previous);
Drawing.PartColors = previousPartColors;
Drawing.UseGoldenAngleColors = previousGoldenAngle;
PartColorPalette.Use(previousPalette);
ColorSchemeRegistry.SchemesDirectory = previousDirectory;
}
}
+2 -1
View File
@@ -1,4 +1,5 @@
using System.Drawing;
using System.Linq;
using System.Threading;
namespace OpenNest
@@ -19,7 +20,7 @@ namespace OpenNest
private Color originColor;
private Color edgeSpacingColor;
private Color previewPartColor;
private Color[] partColors = Drawing.PartColors;
private Color[] partColors = PartPalette.Classic.Colors.ToArray();
private bool useGoldenAngleColors;
private Color partOutlineColor = Color.Empty;
private Color selectedPartColor = Color.Empty;
+2 -2
View File
@@ -114,8 +114,8 @@ namespace OpenNest
d.LibraryEtchColor = scheme.LibraryEtchColor;
d.LibraryBendColor = scheme.LibraryBendColor;
Drawing.PartColors = scheme.PartColors;
Drawing.UseGoldenAngleColors = scheme.UseGoldenAngleColors;
// A copy: editing the scheme's array afterwards must not recolor drawings.
PartColorPalette.Use(new PartPalette(scheme.PartColors ?? [], scheme.UseGoldenAngleColors));
Applied?.Invoke(null, scheme);
}
+1 -3
View File
@@ -79,8 +79,6 @@ int RunDataCollection(string dir, string dbPath, string saveDir, double s, strin
Console.WriteLine($"Using template: {template}");
}
var PartColors = Drawing.PartColors;
var sheetSuite = new[]
{
new Size(96, 48),
@@ -167,7 +165,7 @@ int RunDataCollection(string dir, string dbPath, string saveDir, double s, strin
continue;
}
drawing.Color = PartColors[colorIndex % PartColors.Length];
drawing.Color = PartPalette.Classic.ColorAt(colorIndex);
colorIndex++;
var features = FeatureExtractor.Extract(drawing);
@@ -17,7 +17,7 @@ public class DrawingMetadataColorTests
bool updateGeometry, bool changeName) => StaTestThread.Run(() =>
{
var previousScheme = ColorSchemeSerializer.Deserialize(ColorSchemeSerializer.Serialize(ColorScheme.Default));
var previousPartColors = Drawing.PartColors;
var previousPalette = PartColorPalette.Current;
try
{
var program = new CNC.Program();
@@ -71,7 +71,7 @@ public class DrawingMetadataColorTests
finally
{
ColorSchemeRegistry.Apply(previousScheme);
Drawing.PartColors = previousPartColors;
PartColorPalette.Use(previousPalette);
}
}, TimeSpan.FromMinutes(1), "Metadata color test timed out.");
}
+4 -1
View File
@@ -69,7 +69,10 @@ The schemes, their JSON format, the `Schemes/` folder and the recolor rule live
`OpenNest.Desktop` (`ColorScheme`, `ColorSchemeRegistry`, `ColorSchemeSerializer`,
and `ColorShade`, a port of `ControlPaint.Dark` for the legacy part outline), so
both desktop apps share them; `Schemes/*.json` in that project copies beside both
apps. WinForms draws with per-thread GDI+ pens from `GdiSchemeTools`; the Avalonia
apps. Applying a scheme hands Core an immutable copy of its drawing colors through
`PartColorPalette.Use(new PartPalette(...))`; `Drawing.GetPartColor` and `GetNextColor`
read `PartColorPalette.Current`, so editing a scheme's array later changes nothing until
it is applied again. WinForms draws with per-thread GDI+ pens from `GdiSchemeTools`; the Avalonia
plate canvas draws with Skia colours from `SkiaSchemePalette`. The Avalonia app
applies the scheme saved as `ActiveColorScheme` in the shared user-settings file
when it starts, and recolors a nest's drawings when it opens one; it has no