GifMotion by Warren Galyen

<PackageReference Include="GifMotion" Version="1.0.4" />

 PaletteQuantizer

public class PaletteQuantizer : Quantizer
using System.Collections; using System.Drawing; using System.Drawing.Imaging; namespace GifMotion { public class PaletteQuantizer : Quantizer { private readonly Hashtable _colorMap; protected Color[] Colors; public PaletteQuantizer(ArrayList palette) : base(true) { _colorMap = new Hashtable(); Colors = new Color[palette.Count]; palette.CopyTo(Colors); } protected override byte QuantizePixel(Color32 pixel) { byte b = 0; int aRGB = pixel.ARGB; if (_colorMap.ContainsKey(aRGB)) b = (byte)_colorMap[aRGB]; else { if (pixel.Alpha == 0) { for (int i = 0; i < Colors.Length; i++) { if (Colors[i].A == 0) { b = (byte)i; break; } } } else { int num = 2147483647; int red = pixel.Red; int green = pixel.Green; int blue = pixel.Blue; for (int j = 0; j < Colors.Length; j++) { Color color = Colors[j]; int num2 = color.R - red; int num3 = color.G - green; int num4 = color.B - blue; int num5 = num2 * num2 + num3 * num3 + num4 * num4; if (num5 < num) { b = (byte)j; num = num5; if (num5 == 0) break; } } } _colorMap.Add(aRGB, b); } return b; } protected override ColorPalette GetPalette(ColorPalette palette) { for (int i = 0; i < Colors.Length; i++) { palette.Entries[i] = Colors[i]; } return palette; } } }