Files
OpenNest/OpenNest.Core/PartPalette.cs
T
ajandClaude Fable 5.2 b77b3b7a33 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>
2026-11-12 19:20:22 -05:00

56 lines
2.1 KiB
C#

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];
}
}
}