aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.RemoteDesktop/Quantization/PaletteTable.cs
blob: 5c24fe80f7e0652e7c5006c05eb3416afaeb259c (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
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET                                                                   //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors.     //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
// See src/Resources/Files/License.txt for full licensing and attribution      //
// details.                                                                    //
// .                                                                           //
/////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections;
using System.Drawing;

namespace Tango.RemoteDesktop.Quantization
{
    internal sealed class PaletteTable
    {
        private Color[] palette;

        public Color this[int index]
        {
            get
            {
                return this.palette[index];
            }

            set
            {
                this.palette[index] = value;
            }
        }

        private int GetDistanceSquared(Color a, Color b)
        {
            int dsq = 0; // delta squared
            int v; 

            v = a.B - b.B;
            dsq += v * v;
            v = a.G - b.G;
            dsq += v * v;
            v = a.R - b.R;
            dsq += v * v;

            return dsq;
        }

        public int FindClosestPaletteIndex(Color pixel)
        {
            int dsqBest = int.MaxValue;
            int ret = 0;

            for (int i = 0; i < this.palette.Length; ++i)
            {
                int dsq = GetDistanceSquared(this.palette[i], pixel);

                if (dsq < dsqBest)
                {
                    dsqBest = dsq;
                    ret = i;
                }
            }

            return ret;
        }

        public PaletteTable(Color[] palette)
        {
            this.palette = (Color[])palette.Clone();
        }
    }
}