Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Palette.cs « NesTiler - github.com/ClusterM/NesTiler.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2f6ddf623ae41ede3e35a5e515de45f7036ed9f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using SkiaSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Color = System.Drawing.Color;

namespace com.clusterrr.Famicom.NesTiler
{
    class Palette : IEquatable<Palette>, IEnumerable<SKColor>
    {
        private SKColor?[] colors = new SKColor?[3];
        private Dictionary<ColorPair, (SKColor color, double delta)> deltaCache = new();

        public SKColor? this[int i]
        {
            get
            {
                if (i < 1 || i > 3) throw new IndexOutOfRangeException("Color index must be between 1 and 3");
                return colors[i - 1];
            }
            set
            {
                if (i < 1 || i > 3) throw new IndexOutOfRangeException("Color index must be between 1 and 3");
                colors[i - 1] = value;
            }
        }
        public int Count { get => colors.Where(c => c.HasValue).Count(); }

        public Palette()
        {
            // Empty palette
        }

        public Palette(FastBitmap image, int leftX, int topY, int width, int height, SKColor bgColor)
        {
            Dictionary<SKColor, int> colorCounter = new();
            for (int y = topY; y < topY + height; y++)
            {
                if (y < 0) continue;
                for (int x = leftX; x < leftX + width; x++)
                {
                    var color = image.GetPixelColor(x, y);
                    if (color == bgColor) continue;
                    if (!colorCounter.ContainsKey(color)) colorCounter[color] = 0;
                    colorCounter[color]++;
                }
            }

            var sortedColors = colorCounter
                .OrderByDescending(kv => kv.Value).ToArray();
            for (int i = 0; i < 3; i++)
                if (sortedColors.Length > i)
                    this[i + 1] = sortedColors[i].Key;
        }

        public void Add(SKColor color)
        {
            if (Count >= 3) throw new IndexOutOfRangeException();
            this[Count + 1] = color;
            deltaCache.Clear();
        }

        public Palette(IEnumerable<SKColor> colors)
        {
            var colorsList = colors.ToList();
            for (int i = 0; i < 3; i++)
                if (colorsList.Count > i) this[i + 1] = colorsList[i];
        }

        public double GetTileDelta(FastBitmap image, int leftX, int topY, int width, int height, SKColor bgColor)
        {
            double delta = 0;
            for (int y = topY; y < topY + height; y++)
            {
                if (y < 0) continue;
                for (int x = leftX; x < leftX + width; x++)
                {
                    var color = image.GetPixelColor(x, y);
                    delta += GetMinDelta(color, bgColor).delta;
                }
            }
            return delta;
        }

        private (SKColor color, double delta) GetMinDelta(SKColor color, SKColor 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 SKColor[] { bgColor });
            var result = ac.OrderBy(c => c.GetDelta(color)).First();
            var r = (result, result.GetDelta(color));
            deltaCache[pair] = r;
            return r;
        }

        public bool Equals(Palette? other)
        {
            if (other == null) return false;
            var colors1 = colors.Where(c => c.HasValue)
                .OrderBy(c => c!.Value.ToArgb())
                .Select(c => c!.Value)
                .ToArray();
            var colors2 = new SKColor?[] { other[1], other[2], other[3] }
                .Where(c => c.HasValue)
                .OrderBy(c => c!.Value.ToArgb())
                .Select(c => c!.Value)
                .ToArray();
            return Enumerable.SequenceEqual(colors1, colors2);
            /*
            if (other == null) return false;
            var colors1 = colors.Where(c => c.HasValue)
                .Select(c => c!.Value.ToArgb())
                .OrderBy(c => c)
                .ToArray();
            var colors2 = new SKColor?[] { other[1], other[2], other[3] }
                .Where(c => c.HasValue)
                .Select(c => c!.Value.ToArgb())
                .OrderBy(c => c)
                .ToArray();
            var r = Enumerable.SequenceEqual(colors1, colors2);
            return r;
            */
        }

        public bool Contains(Palette other)
        {
            var thisColors = colors.Where(c => c.HasValue);
            var otherColors = new SKColor?[] { other[1], other[2], other[3] }.Where(c => c.HasValue).Select(c => c!.Value);

            foreach (var color in otherColors)
            {
                if (!thisColors.Contains(color))
                    return false;
            }
            return true;
        }

        public IEnumerator<SKColor> GetEnumerator()
        {
            return colors.Where(c => c.HasValue).Select(c => c!.Value).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public override string ToString() => string.Join(", ", colors.Where(c => c.HasValue).Select(c => ColorTranslator.ToHtml(c!.Value.ToColor())).OrderBy(c => c));

        public override int GetHashCode() => (int)((this[1]?.ToArgb() ?? 0) + ((this[2]?.ToArgb() ?? 0) << 4) + ((this[3]?.ToArgb() ?? 0) << 8));
    }
}