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: 8fad30cce2def4440b51f60842c8b29d060281da (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
using SkiaSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using Color = System.Drawing.Color;

namespace com.clusterrr.Famicom.NesTiler
{
    class Palette : IEquatable<Palette>, IEnumerable<SKColor>
    {
        public struct LossyInfo
        {
            public int ImageNum { get; init; }
            public int TileX { get; init; }
            public int TileY { get; init; }
            public int TileWidth { get; init; }
            public int TileHeight { get; init; }
            public SKColor[] Colors { get; init; }
        }

        private SKColor[] colors;
        private Dictionary<ColorPair, (SKColor color, double delta)> deltaCache = new();
        public LossyInfo? ColorLossy { get; init; } = null;

        public SKColor? this[int i]
        {
            get
            {
                if (i > colors.Length) return null;
                if (i <= 0) throw new ArgumentOutOfRangeException("Invalid color index");
                return colors[i - 1];
            }
        }
        public int Count => colors.Length;

        public Palette(int imageNum, FastBitmap image, int tileX, int tileY, int tileWidth, int tileHeight, SKColor bgColor)
        {
            Dictionary<SKColor, int> colorCounter = new();
            for (int y = tileY; y < tileY + tileHeight; y++)
            {
                if (y < 0) continue;
                for (int x = tileX; x < tileX + tileWidth; x++)
                {
                    var color = image.GetPixelColor(x, y);
                    if (color == bgColor) continue;
                    if (!colorCounter.ContainsKey(color)) colorCounter[color] = 0;
                    colorCounter[color]++;
                }
            }

            var colorsCandidates = colorCounter.OrderByDescending(kv => kv.Value);
            if (colorsCandidates.Count() > 3) ColorLossy = new()
            {
                ImageNum = imageNum,
                TileX = tileX,
                TileY = tileY,
                TileWidth = tileWidth,
                TileHeight = tileHeight,
                Colors = Enumerable.Concat(new[] { bgColor }, colorsCandidates.Select(kv => kv.Key)).ToArray()
            };
            colors = colorsCandidates.Take(3).OrderBy(kv => kv.Key.ToArgb()).Select(kv => kv.Key).ToArray();
        }

        public Palette(IEnumerable<SKColor> colors, bool nosort = false)
        {
            if (!nosort)
                this.colors = colors.OrderBy(c => c.ToArgb()).Take(3).ToArray();
            else
                this.colors = colors.Take(3).ToArray();
        }

        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, new[] { 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 r = Enumerable.SequenceEqual(this, other);
            return r;
        }

        public bool Contains(Palette other)
        {
            foreach (var color in other)
            {
                if (!this.Contains(color))
                    return false;
            }
            return true;
        }

        public IEnumerator<SKColor> GetEnumerator()
        {
            return colors.Select(c => c).GetEnumerator(); // wtf?
        }

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

        public override string ToString() => string.Join(", ", colors.Select(c => ColorTranslator.ToHtml(c.ToColor())));

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