palettequantizer.cs

来自「破解的飞信源代码」· CS 代码 · 共 79 行

CS
79
字号
namespace Imps.Utils
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;

    public class PaletteQuantizer : ImageQuantizer
    {
        private Hashtable _colorMap;
        private Color[] _colors;

        public PaletteQuantizer(List<Color> palette) : base(true)
        {
            this._colorMap = new Hashtable();
            this._colors = new Color[palette.get_Count()];
            palette.CopyTo(this._colors);
        }

        protected override ColorPalette GetPalette(ColorPalette palette)
        {
            for (int i = 0; i < this._colors.Length; i++)
            {
                palette.Entries[i] = this._colors[i];
            }
            return palette;
        }

        protected override unsafe byte QuantizePixel(ImageQuantizer.Color32* pixel)
        {
            byte num = 0;
            int key = pixel.ARGB;
            if (this._colorMap.ContainsKey(key))
            {
                return (byte) this._colorMap[key];
            }
            if (pixel.Alpha == 0)
            {
                for (int i = 0; i < this._colors.Length; i++)
                {
                    if (this._colors[i].A == 0)
                    {
                        num = (byte) i;
                        break;
                    }
                }
            }
            else
            {
                int num4 = 0x7fffffff;
                int red = pixel.Red;
                int green = pixel.Green;
                int blue = pixel.Blue;
                for (int j = 0; j < this._colors.Length; j++)
                {
                    Color color = this._colors[j];
                    int num9 = color.R - red;
                    int num10 = color.G - green;
                    int num11 = color.B - blue;
                    int num12 = ((num9 * num9) + (num10 * num10)) + (num11 * num11);
                    if (num12 < num4)
                    {
                        num = (byte) j;
                        num4 = num12;
                        if (num12 == 0)
                        {
                            break;
                        }
                    }
                }
            }
            this._colorMap.Add(key, num);
            return num;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?