diff options
author | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2022-09-28 16:48:54 +0300 |
---|---|---|
committer | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2022-09-28 16:48:54 +0300 |
commit | 6d1ecd9887ec1d9ae6675c652455a233de2a2e6f (patch) | |
tree | 17a398225d5c59769f556446455e2ca437a9328e /NesTiler/Palette.cs | |
parent | 0d50f7f46719ade1d1e06c0ab4f63ecf10fab224 (diff) |
Optimizations
Diffstat (limited to 'NesTiler/Palette.cs')
-rw-r--r-- | NesTiler/Palette.cs | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/NesTiler/Palette.cs b/NesTiler/Palette.cs index 335eac5..6b29f02 100644 --- a/NesTiler/Palette.cs +++ b/NesTiler/Palette.cs @@ -9,6 +9,7 @@ namespace com.clusterrr.Famicom.NesTiler class Palette : IEquatable<Palette>, IEnumerable<Color>
{
private Color?[] colors = new Color?[3];
+ private Dictionary<ColorPair, (Color color, double delta)> deltaCache = new();
public Color? this[int i]
{
@@ -53,10 +54,9 @@ namespace com.clusterrr.Famicom.NesTiler public void Add(Color color)
{
- if (Count < 3)
- this[Count + 1] = color;
- else
- throw new IndexOutOfRangeException();
+ if (Count >= 3) throw new IndexOutOfRangeException();
+ this[Count + 1] = color;
+ deltaCache.Clear();
}
public Palette(IEnumerable<Color> colors)
@@ -82,9 +82,17 @@ namespace com.clusterrr.Famicom.NesTiler private (Color color, double delta) GetMinDelta(Color color, Color bgColor)
{
+ var pair = new ColorPair()
+ {
+ Color1 = color,
+ Color2 = bgColor
+ };
+ if (deltaCache.ContainsKey(pair)) return deltaCache[pair];
var ac = Enumerable.Concat(colors.Where(c => c.HasValue).Select(c => c.Value), new Color[] { bgColor });
var result = ac.OrderBy(c => c.GetDelta(color)).First();
- return (result, result.GetDelta(color));
+ var r = (result, result.GetDelta(color));
+ deltaCache[pair] = r;
+ return r;
}
public bool Equals(Palette other)
|