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

ColorExtensions.cs « NesTiler - github.com/ClusterM/NesTiler.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 239497de73b13bbbaf813079f249abedfa75a352 (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
using ColorMine.ColorSpaces;
using ColorMine.ColorSpaces.Comparisons;
using SkiaSharp;
using System.Collections.Generic;
using System.Drawing;

namespace com.clusterrr.Famicom.NesTiler
{
    public record ColorPair
    {
        public SKColor Color1;
        public SKColor Color2;
    }

    static class ColorExtensions
    {
        private static Dictionary<ColorPair, double> cache = new();
        private static CieDe2000Comparison comparer = new();

        public static void ClearCache() => cache.Clear();

        public static Color ToColor(this SKColor color)
            => Color.FromArgb((int)color.ToArgb());

        public static SKColor ToSKColor(this Color color)
            => new SKColor((uint)color.ToArgb());

        public static uint ToArgb(this SKColor color)
            => (uint)((color.Alpha << 24) | (color.Red << 16) | (color.Green << 8) | color.Blue);

        public static double GetDelta(this SKColor color1, SKColor color2)
        {
            var pair = new ColorPair()
            {
                Color1 = color1,
                Color2 = color2
            };
            if (cache.ContainsKey(pair))
                return cache[pair];
            var a = new Rgb { R = color1.Red, G = color1.Green, B = color1.Blue };
            var b = new Rgb { R = color2.Red, G = color2.Green, B = color2.Blue };
            var delta = a.Compare(b, comparer);
            cache[pair] = delta;
            return delta;
        }
    }
}