IconColorPicker.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using SixLabors.ImageSharp;
  2. using SixLabors.ImageSharp.PixelFormats;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Ava.Ui.Windows
  6. {
  7. static class IconColorPicker
  8. {
  9. private const int ColorsPerLine = 64;
  10. private const int TotalColors = ColorsPerLine * ColorsPerLine;
  11. private const int UvQuantBits = 3;
  12. private const int UvQuantShift = BitsPerComponent - UvQuantBits;
  13. private const int SatQuantBits = 5;
  14. private const int SatQuantShift = BitsPerComponent - SatQuantBits;
  15. private const int BitsPerComponent = 8;
  16. private const int CutOffLuminosity = 64;
  17. private struct PaletteColor
  18. {
  19. public int Qck { get; }
  20. public byte R { get; }
  21. public byte G { get; }
  22. public byte B { get; }
  23. public PaletteColor(int qck, byte r, byte g, byte b)
  24. {
  25. Qck = qck;
  26. R = r;
  27. G = g;
  28. B = b;
  29. }
  30. }
  31. public static Color GetFilteredColor(Image<Bgra32> image)
  32. {
  33. var color = GetColor(image).ToPixel<Bgra32>();
  34. // We don't want colors that are too dark.
  35. // If the color is too dark, make it brighter by reducing the range
  36. // and adding a constant color.
  37. int luminosity = GetColorApproximateLuminosity(color.R, color.G, color.B);
  38. if (luminosity < CutOffLuminosity)
  39. {
  40. color = Color.FromRgb(
  41. (byte)Math.Min(CutOffLuminosity + color.R, byte.MaxValue),
  42. (byte)Math.Min(CutOffLuminosity + color.G, byte.MaxValue),
  43. (byte)Math.Min(CutOffLuminosity + color.B, byte.MaxValue));
  44. }
  45. return color;
  46. }
  47. public static Color GetColor(Image<Bgra32> image)
  48. {
  49. var colors = new PaletteColor[TotalColors];
  50. var dominantColorBin = new Dictionary<int, int>();
  51. var buffer = GetBuffer(image);
  52. int w = image.Width;
  53. int w8 = w << 8;
  54. int h8 = image.Height << 8;
  55. int xStep = w8 / ColorsPerLine;
  56. int yStep = h8 / ColorsPerLine;
  57. int i = 0;
  58. int maxHitCount = 0;
  59. for (int y = 0; y < image.Height; y++)
  60. {
  61. int yOffset = y * image.Width;
  62. for (int x = 0; x < image.Width && i < TotalColors; x++)
  63. {
  64. int offset = x + yOffset;
  65. byte cb = buffer[offset].B;
  66. byte cg = buffer[offset].G;
  67. byte cr = buffer[offset].R;
  68. var qck = GetQuantizedColorKey(cr, cg, cb);
  69. if (dominantColorBin.TryGetValue(qck, out int hitCount))
  70. {
  71. dominantColorBin[qck] = hitCount + 1;
  72. if (maxHitCount < hitCount)
  73. {
  74. maxHitCount = hitCount;
  75. }
  76. }
  77. else
  78. {
  79. dominantColorBin.Add(qck, 1);
  80. }
  81. colors[i++] = new PaletteColor(qck, cr, cg, cb);
  82. }
  83. }
  84. int highScore = -1;
  85. PaletteColor bestCandidate = default;
  86. for (i = 0; i < TotalColors; i++)
  87. {
  88. var score = GetColorScore(dominantColorBin, maxHitCount, colors[i]);
  89. if (highScore < score)
  90. {
  91. highScore = score;
  92. bestCandidate = colors[i];
  93. }
  94. }
  95. return Color.FromRgb(bestCandidate.R, bestCandidate.G, bestCandidate.B);
  96. }
  97. public static Bgra32[] GetBuffer(Image<Bgra32> image)
  98. {
  99. return image.TryGetSinglePixelSpan(out var data) ? data.ToArray() : new Bgra32[0];
  100. }
  101. private static int GetColorScore(Dictionary<int, int> dominantColorBin, int maxHitCount, PaletteColor color)
  102. {
  103. var hitCount = dominantColorBin[color.Qck];
  104. var balancedHitCount = BalanceHitCount(hitCount, maxHitCount);
  105. var quantSat = (GetColorSaturation(color) >> SatQuantShift) << SatQuantShift;
  106. var value = GetColorValue(color);
  107. // If the color is rarely used on the image,
  108. // then chances are that theres a better candidate, even if the saturation value
  109. // is high. By multiplying the saturation value with a weight, we can lower
  110. // it if the color is almost never used (hit count is low).
  111. var satWeighted = quantSat;
  112. var satWeight = balancedHitCount << 5;
  113. if (satWeight < 0x100)
  114. {
  115. satWeighted = (satWeighted * satWeight) >> 8;
  116. }
  117. // Compute score from saturation and dominance of the color.
  118. // We prefer more vivid colors over dominant ones, so give more weight to the saturation.
  119. var score = ((satWeighted << 1) + balancedHitCount) * value;
  120. return score;
  121. }
  122. private static int BalanceHitCount(int hitCount, int maxHitCount)
  123. {
  124. return (hitCount << 8) / maxHitCount;
  125. }
  126. private static int GetColorApproximateLuminosity(byte r, byte g, byte b)
  127. {
  128. return (r + g + b) / 3;
  129. }
  130. private static int GetColorSaturation(PaletteColor color)
  131. {
  132. int cMax = Math.Max(Math.Max(color.R, color.G), color.B);
  133. if (cMax == 0)
  134. {
  135. return 0;
  136. }
  137. int cMin = Math.Min(Math.Min(color.R, color.G), color.B);
  138. int delta = cMax - cMin;
  139. return (delta << 8) / cMax;
  140. }
  141. private static int GetColorValue(PaletteColor color)
  142. {
  143. return Math.Max(Math.Max(color.R, color.G), color.B);
  144. }
  145. private static int GetQuantizedColorKey(byte r, byte g, byte b)
  146. {
  147. int u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
  148. int v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
  149. return (v >> UvQuantShift) | ((u >> UvQuantShift) << UvQuantBits);
  150. }
  151. }
  152. }