From 32fc3cad9790103853e80f8176e7ea780ce26220 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Thu, 12 Nov 2026 19:54:48 -0500 Subject: [PATCH] fix(core): color drawings from the classic palette when a scheme has no part colors Co-Authored-By: Claude Fable 5.2 --- OpenNest.Core/PartPalette.cs | 13 +++++++++---- OpenNest.Tests/PartPaletteTests.cs | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 OpenNest.Tests/PartPaletteTests.cs diff --git a/OpenNest.Core/PartPalette.cs b/OpenNest.Core/PartPalette.cs index 62feef7a..96fe55c1 100644 --- a/OpenNest.Core/PartPalette.cs +++ b/OpenNest.Core/PartPalette.cs @@ -38,7 +38,10 @@ namespace OpenNest Color.FromArgb(215, 130, 130), // Light Coral }); - /// The explicit colors, cycled by unless . + /// + /// The explicit colors, cycled by unless . + /// When empty, uses . + /// public IReadOnlyList Colors { get; } /// When set, colors are generated by instead of cycled. @@ -47,9 +50,11 @@ namespace OpenNest public Color ColorAt(int index) { ArgumentOutOfRangeException.ThrowIfNegative(index); - return UseGoldenAngle - ? PartColorPalette.GoldenAngle(index) - : colors[index % colors.Length]; + if (UseGoldenAngle) + return PartColorPalette.GoldenAngle(index); + + // A scheme saved without part colors still has to color drawings. + return colors.Length == 0 ? Classic.ColorAt(index) : colors[index % colors.Length]; } } } diff --git a/OpenNest.Tests/PartPaletteTests.cs b/OpenNest.Tests/PartPaletteTests.cs new file mode 100644 index 00000000..add673c5 --- /dev/null +++ b/OpenNest.Tests/PartPaletteTests.cs @@ -0,0 +1,25 @@ +using System.Drawing; + +namespace OpenNest.Tests; + +public class PartPaletteTests +{ + [Fact] + public void ColorAt_CyclesTheGivenColors() + { + var palette = new PartPalette(new[] { Color.Red, Color.Blue }); + + Assert.Equal(Color.Red, palette.ColorAt(0)); + Assert.Equal(Color.Blue, palette.ColorAt(1)); + Assert.Equal(Color.Red, palette.ColorAt(2)); + } + + [Fact] + public void ColorAt_WithNoColors_UsesTheClassicPalette() + { + var palette = new PartPalette(System.Array.Empty()); + + for (var i = 0; i < 14; i++) + Assert.Equal(PartPalette.Classic.ColorAt(i), palette.ColorAt(i)); + } +}