Files
OpenNest/OpenNest.Core/PartPalette.cs
T

61 lines
2.4 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"/>.
/// When empty, <see cref="ColorAt"/> uses <see cref="Classic"/>.
/// </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);
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];
}
}
}