Magically import images and more into Gamecraft as blocks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

170 lines
6.4KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Text;
  4. using UnityEngine;
  5. using GamecraftModdingAPI.Blocks;
  6. using GamecraftModdingAPI.Utility;
  7. using Pixi.Common;
  8. namespace Pixi.Images
  9. {
  10. public static class PixelUtility
  11. {
  12. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  13. public static BlockInfo QuantizePixel(Color pixel)
  14. {
  15. BlockColors color = BlockColors.Default;
  16. int darkness = 0;
  17. bool force = false;
  18. #if DEBUG
  19. Logging.MetaLog($"Color (r:{pixel.r}, g:{pixel.g}, b:{pixel.b})");
  20. #endif
  21. if (Mathf.Abs(pixel.r - pixel.g) <= pixel.r * 0.1f && Mathf.Abs(pixel.r - pixel.b) <= pixel.r * 0.1f)
  22. {
  23. color = BlockColors.White;
  24. darkness = (int)(10 - ((pixel.r + pixel.g + pixel.b) * 3.5));
  25. //Logging.MetaDebugLog($"Color (r:{pixel.r}, g:{pixel.g}, b:{pixel.b})");
  26. }
  27. else if (pixel.r >= pixel.g && pixel.r >= pixel.b)
  28. {
  29. // Red is highest
  30. if ((pixel.r - pixel.g) > pixel.r * 0.65 && (pixel.r - pixel.b) > pixel.r * 0.55)
  31. {
  32. // Red is much higher than other pixels
  33. darkness = (int)(9 - (pixel.r * 8.01));
  34. color = BlockColors.Red;
  35. }
  36. else if ((pixel.g - pixel.b) > pixel.g * 0.25)
  37. {
  38. // Green is much higher than blue
  39. if ((pixel.r - pixel.g) < pixel.r * 0.8)
  40. {
  41. darkness = (int)(10 - ((pixel.r * 2.1 + pixel.g) * 2.1));
  42. color = BlockColors.Orange;
  43. }
  44. else
  45. {
  46. darkness = (int)(10 - ((pixel.r * 2.1 + pixel.g) * 2.2));
  47. color = BlockColors.Yellow;
  48. }
  49. }
  50. else if ((pixel.b - pixel.g) > pixel.b * 0.3)
  51. {
  52. // Blue is much higher than green
  53. darkness = (int)(10 - ((pixel.r + pixel.b) * 5.0));
  54. color = BlockColors.Purple;
  55. }
  56. else
  57. {
  58. // Green is close strength to blue
  59. darkness = (int)(10 - ((pixel.r * 2.1 + pixel.g + pixel.b) * 2.5));
  60. color = darkness < 6 ? BlockColors.Pink : BlockColors.Orange;
  61. force = true;
  62. }
  63. }
  64. else if (pixel.g >= pixel.r && pixel.g >= pixel.b)
  65. {
  66. // Green is highest
  67. if ((pixel.g - pixel.r) > pixel.g * 0.6 && (pixel.g - pixel.b) > pixel.g * 0.48)
  68. {
  69. // Green is much higher than other pixels
  70. darkness = (int)(10 - (pixel.g * 10.1));
  71. color = BlockColors.Green;
  72. }
  73. else if ((pixel.r - pixel.b) > pixel.r * 0.3)
  74. {
  75. // Red is much higher than blue
  76. darkness = (int)(10 - ((pixel.r + pixel.g) * 5.1));
  77. color = BlockColors.Yellow;
  78. }
  79. else if ((pixel.b - pixel.r) > pixel.b * 0.2)
  80. {
  81. // Blue is much higher than red
  82. darkness = (int)(9 - ((pixel.g + pixel.b) * 5.1));
  83. color = BlockColors.Aqua;
  84. }
  85. else
  86. {
  87. // Red is close strength to blue
  88. darkness = (int)(10 - ((pixel.r + pixel.g * 2.2 + pixel.b) * 2.9));
  89. color = BlockColors.Lime;
  90. }
  91. }
  92. else if (pixel.b >= pixel.g && pixel.b >= pixel.r)
  93. {
  94. // Blue is highest
  95. if ((pixel.b - pixel.g) > pixel.b * 0.6 && (pixel.b - pixel.r) > pixel.b * 0.6)
  96. {
  97. // Blue is much higher than other pixels
  98. darkness = (int)(10 - (pixel.b * 10.1));
  99. color = BlockColors.Blue;
  100. }
  101. else if ((pixel.g - pixel.r) > pixel.g * 0.3)
  102. {
  103. // Green is much higher than red
  104. darkness = (int)(10 - ((pixel.g + pixel.b) * 5.1));
  105. if (darkness == 4 || darkness == 5) darkness = 0;
  106. else if (darkness < 3) darkness = 4;
  107. color = BlockColors.Aqua;
  108. }
  109. else if ((pixel.r - pixel.g) > pixel.r * 0.3)
  110. {
  111. // Red is much higher than green
  112. darkness = (int)(10 - ((pixel.r + pixel.b) * 5.0));
  113. color = BlockColors.Purple;
  114. }
  115. else
  116. {
  117. // Green is close strength to red
  118. darkness = (int)(10 - ((pixel.r + pixel.g + pixel.b * 2.2) * 3.0));
  119. color = BlockColors.Aqua;
  120. }
  121. }
  122. // level 9 is not darker than lvl 8
  123. if (darkness > 8 && !force) darkness = 8;
  124. // darkness 0 is the most saturated (it's not just the lightest)
  125. if (darkness < 0) darkness = 0;
  126. BlockInfo result = new BlockInfo
  127. {
  128. block = pixel.a > 0.75 ? BlockIDs.AluminiumCube : BlockIDs.GlassCube,
  129. color = color,
  130. darkness = (byte)darkness,
  131. visible = pixel.a > 0.5f,
  132. };
  133. #if DEBUG
  134. Logging.MetaLog($"Quantized {color} (b:{result.block} d:{result.darkness} v:{result.visible})");
  135. #endif
  136. return result;
  137. }
  138. public static string HexPixel(Color pixel)
  139. {
  140. return "#"+ColorUtility.ToHtmlStringRGBA(pixel);
  141. }
  142. public static string TextureToString(Texture2D img)
  143. {
  144. StringBuilder imgString = new StringBuilder("<cspace=-0.1em><line-height=42%>");
  145. for (int y = img.height-1; y >= 0 ; y--) // text origin is top right, but img origin is bottom right
  146. {
  147. for (int x = 0; x < img.width; x++)
  148. {
  149. Color pixel = img.GetPixel(x, y);
  150. imgString.Append("<color=");
  151. imgString.Append(HexPixel(pixel));
  152. imgString.Append(">");
  153. imgString.Append("\u25a0");
  154. }
  155. imgString.Append("<br>");
  156. }
  157. imgString.Append("</color></cspace></line-height>");
  158. return imgString.ToString();
  159. }
  160. }
  161. }