diff --git a/OpenNest.Core/Drawing.cs b/OpenNest.Core/Drawing.cs index 5280a8d3..d8379ab8 100644 --- a/OpenNest.Core/Drawing.cs +++ b/OpenNest.Core/Drawing.cs @@ -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 - }; + /// The active palette's color for a drawing at . + 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()) { } diff --git a/OpenNest.Core/PartColorPalette.cs b/OpenNest.Core/PartColorPalette.cs index f3b6df73..ed9cf0ef 100644 --- a/OpenNest.Core/PartColorPalette.cs +++ b/OpenNest.Core/PartColorPalette.cs @@ -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; + + /// + /// The palette drawings take their colors from, process-wide. Replaced as a whole by + /// , so a reader never sees one palette's colors with another's flag. + /// + 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); diff --git a/OpenNest.Core/PartPalette.cs b/OpenNest.Core/PartPalette.cs new file mode 100644 index 00000000..62feef7a --- /dev/null +++ b/OpenNest.Core/PartPalette.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; + +namespace OpenNest +{ + /// + /// 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. + /// + public sealed class PartPalette + { + private readonly Color[] colors; + + public PartPalette(IEnumerable colors, bool useGoldenAngle = false) + { + ArgumentNullException.ThrowIfNull(colors); + this.colors = colors.ToArray(); + Colors = Array.AsReadOnly(this.colors); + UseGoldenAngle = useGoldenAngle; + } + + /// The default palette, also the fallback before a color scheme is applied. + 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 + }); + + /// The explicit colors, cycled by unless . + public IReadOnlyList Colors { get; } + + /// When set, colors are generated by instead of cycled. + public bool UseGoldenAngle { get; } + + public Color ColorAt(int index) + { + ArgumentOutOfRangeException.ThrowIfNegative(index); + return UseGoldenAngle + ? PartColorPalette.GoldenAngle(index) + : colors[index % colors.Length]; + } + } +} diff --git a/OpenNest.Desktop.Tests/ColorSchemeRegistryTests.cs b/OpenNest.Desktop.Tests/ColorSchemeRegistryTests.cs index 21a84c75..5977bc27 100644 --- a/OpenNest.Desktop.Tests/ColorSchemeRegistryTests.cs +++ b/OpenNest.Desktop.Tests/ColorSchemeRegistryTests.cs @@ -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; } } diff --git a/OpenNest.Desktop/ColorScheme.cs b/OpenNest.Desktop/ColorScheme.cs index 71887685..5a9fe5e7 100644 --- a/OpenNest.Desktop/ColorScheme.cs +++ b/OpenNest.Desktop/ColorScheme.cs @@ -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; diff --git a/OpenNest.Desktop/ColorSchemeRegistry.cs b/OpenNest.Desktop/ColorSchemeRegistry.cs index efc3d1a0..2d47be36 100644 --- a/OpenNest.Desktop/ColorSchemeRegistry.cs +++ b/OpenNest.Desktop/ColorSchemeRegistry.cs @@ -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); } diff --git a/OpenNest.Training/Program.cs b/OpenNest.Training/Program.cs index 2e93dae8..c3d73a37 100644 --- a/OpenNest.Training/Program.cs +++ b/OpenNest.Training/Program.cs @@ -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); diff --git a/OpenNest.WinForms.Tests/Forms/DrawingMetadataColorTests.cs b/OpenNest.WinForms.Tests/Forms/DrawingMetadataColorTests.cs index 7adcf672..e7fc01c9 100644 --- a/OpenNest.WinForms.Tests/Forms/DrawingMetadataColorTests.cs +++ b/OpenNest.WinForms.Tests/Forms/DrawingMetadataColorTests.cs @@ -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."); } diff --git a/docs/nesting-view-colors.md b/docs/nesting-view-colors.md index bd3fe607..bbbbb342 100644 --- a/docs/nesting-view-colors.md +++ b/docs/nesting-view-colors.md @@ -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