|
| 1 | +using System.Collections.Concurrent; |
| 2 | + |
| 3 | +namespace Terminal.Gui; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Translates colors in an image into a Palette of up to <see cref="MaxColors"/> colors (typically 256). |
| 7 | +/// </summary> |
| 8 | +public class ColorQuantizer |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Gets the current colors in the palette based on the last call to |
| 12 | + /// <see cref="BuildPalette"/>. |
| 13 | + /// </summary> |
| 14 | + public IReadOnlyCollection<Color> Palette { get; private set; } = new List<Color> (); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Gets or sets the maximum number of colors to put into the <see cref="Palette"/>. |
| 18 | + /// Defaults to 256 (the maximum for sixel images). |
| 19 | + /// </summary> |
| 20 | + public int MaxColors { get; set; } = 256; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Gets or sets the algorithm used to map novel colors into existing |
| 24 | + /// palette colors (closest match). Defaults to <see cref="EuclideanColorDistance"/> |
| 25 | + /// </summary> |
| 26 | + public IColorDistance DistanceAlgorithm { get; set; } = new EuclideanColorDistance (); |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Gets or sets the algorithm used to build the <see cref="Palette"/>. |
| 30 | + /// </summary> |
| 31 | + public IPaletteBuilder PaletteBuildingAlgorithm { get; set; } = new PopularityPaletteWithThreshold (new EuclideanColorDistance (), 8); |
| 32 | + |
| 33 | + private readonly ConcurrentDictionary<Color, int> _nearestColorCache = new (); |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Builds a <see cref="Palette"/> of colors that most represent the colors used in <paramref name="pixels"/> image. |
| 37 | + /// This is based on the currently configured <see cref="PaletteBuildingAlgorithm"/>. |
| 38 | + /// </summary> |
| 39 | + /// <param name="pixels"></param> |
| 40 | + public void BuildPalette (Color [,] pixels) |
| 41 | + { |
| 42 | + List<Color> allColors = new (); |
| 43 | + int width = pixels.GetLength (0); |
| 44 | + int height = pixels.GetLength (1); |
| 45 | + |
| 46 | + for (var x = 0; x < width; x++) |
| 47 | + { |
| 48 | + for (var y = 0; y < height; y++) |
| 49 | + { |
| 50 | + allColors.Add (pixels [x, y]); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + _nearestColorCache.Clear (); |
| 55 | + Palette = PaletteBuildingAlgorithm.BuildPalette (allColors, MaxColors); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Returns the closest color in <see cref="Palette"/> that matches <paramref name="toTranslate"/> |
| 60 | + /// based on the color comparison algorithm defined by <see cref="DistanceAlgorithm"/> |
| 61 | + /// </summary> |
| 62 | + /// <param name="toTranslate"></param> |
| 63 | + /// <returns></returns> |
| 64 | + public int GetNearestColor (Color toTranslate) |
| 65 | + { |
| 66 | + if (_nearestColorCache.TryGetValue (toTranslate, out int cachedAnswer)) |
| 67 | + { |
| 68 | + return cachedAnswer; |
| 69 | + } |
| 70 | + |
| 71 | + // Simple nearest color matching based on DistanceAlgorithm |
| 72 | + var minDistance = double.MaxValue; |
| 73 | + var nearestIndex = 0; |
| 74 | + |
| 75 | + for (var index = 0; index < Palette.Count; index++) |
| 76 | + { |
| 77 | + Color color = Palette.ElementAt (index); |
| 78 | + double distance = DistanceAlgorithm.CalculateDistance (color, toTranslate); |
| 79 | + |
| 80 | + if (distance < minDistance) |
| 81 | + { |
| 82 | + minDistance = distance; |
| 83 | + nearestIndex = index; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + _nearestColorCache.TryAdd (toTranslate, nearestIndex); |
| 88 | + |
| 89 | + return nearestIndex; |
| 90 | + } |
| 91 | +} |
0 commit comments