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.

286 lines
19KB

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using UnityEngine;
  6. using GamecraftModdingAPI.Blocks;
  7. using GamecraftModdingAPI.Utility;
  8. namespace Pixi.Common
  9. {
  10. public static class ColorSpaceUtility
  11. {
  12. private const float optimal_delta = 0.2f;
  13. private static Dictionary<BlockColor, float[]> colorMap = null;
  14. private static Dictionary<byte, BlockColor> botColorMap = null;
  15. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  16. public static BlockColor QuantizeToBlockColor(Color pixel)
  17. {
  18. if (colorMap == null) BuildColorMap();
  19. float[] closest = new float[3] { 1, 1, 1 };
  20. BlockColor c = new BlockColor
  21. {
  22. Color = BlockColors.Default,
  23. Darkness = 0,
  24. };
  25. BlockColor[] keys = colorMap.Keys.ToArray();
  26. float geometricClosest = float.MaxValue;
  27. for (int k = 0; k < keys.Length; k++)
  28. {
  29. float[] color = colorMap[keys[k]];
  30. float[] distance = new float[3] { Math.Abs(pixel.r - color[0]), Math.Abs(pixel.g - color[1]), Math.Abs(pixel.b - color[2]) };
  31. float dist = Mathf.Sqrt(Mathf.Pow(distance[0], 2) + Mathf.Pow(distance[1], 2) + Mathf.Pow(distance[2], 2));
  32. if (dist < geometricClosest)
  33. {
  34. c = keys[k];
  35. closest = distance;
  36. geometricClosest = Mathf.Sqrt(Mathf.Pow(closest[0], 2) + Mathf.Pow(closest[1], 2) + Mathf.Pow(closest[2], 2));
  37. if (geometricClosest < optimal_delta)
  38. {
  39. #if DEBUG
  40. Logging.MetaLog($"Final delta ({closest[0]},{closest[1]},{closest[2]}) t:{geometricClosest}");
  41. #endif
  42. return c;
  43. }
  44. }
  45. }
  46. #if DEBUG
  47. Logging.MetaLog($"Final delta ({closest[0]},{closest[1]},{closest[2]}) t:{geometricClosest}");
  48. #endif
  49. return c;
  50. }
  51. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  52. public static BlockColor QuantizeToBlockColor(byte cubeColorEnum)
  53. {
  54. if (botColorMap == null) BuildBotColorMap();
  55. return botColorMap[cubeColorEnum];
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public static BlockColor QuantizeToBlockColor(float[] pixel)
  59. {
  60. if (pixel.Length < 3 || pixel[0] < 0 || pixel[1] < 0 || pixel[2] < 0)
  61. {
  62. return new BlockColor
  63. {
  64. Color = BlockColors.Default,
  65. Darkness = 0,
  66. };
  67. }
  68. return QuantizeToBlockColor(new Color(pixel[0], pixel[1], pixel[2]));
  69. }
  70. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  71. public static float[] UnquantizeToArray(BlockColor c)
  72. {
  73. if (colorMap == null) BuildColorMap();
  74. return colorMap[c];
  75. }
  76. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  77. public static float[] UnquantizeToArray(BlockColors color, byte darkness = 0)
  78. {
  79. return UnquantizeToArray(new BlockColor
  80. {
  81. Color = color,
  82. Darkness = darkness,
  83. });
  84. }
  85. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  86. public static Color UnquantizeToColor(BlockColor c)
  87. {
  88. float[] t = UnquantizeToArray(c);
  89. return new Color(t[0], t[1], t[2]);
  90. }
  91. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  92. public static Color UnquantizeToColor(BlockColors color, byte darkness = 0)
  93. {
  94. return UnquantizeToColor(new BlockColor
  95. {
  96. Color = color,
  97. Darkness = darkness,
  98. });
  99. }
  100. private static void BuildColorMap()
  101. {
  102. colorMap = new Dictionary<BlockColor, float[]>();
  103. // TODO create actual color map
  104. foreach (BlockColors c in Enum.GetValues(typeof(BlockColors)))
  105. {
  106. for (byte d = 0; d < 10; d++)
  107. {
  108. BlockColor colorStruct = new BlockColor
  109. {
  110. Color = c,
  111. Darkness = d,
  112. };
  113. colorMap[colorStruct] = new float[3] { 1f, 0f, 1f };
  114. }
  115. }
  116. // this was done manually -- never again
  117. // White
  118. colorMap[new BlockColor { Color = BlockColors.White, Darkness = 0 }] = new float[3] { 1f, 1f, 1f};
  119. colorMap[new BlockColor { Color = BlockColors.White, Darkness = 1 }] = new float[3] { 0.88f, 0.98f, 0.99f };
  120. colorMap[new BlockColor { Color = BlockColors.White, Darkness = 2 }] = new float[3] { 0.80f, 0.89f, 0.99f };
  121. colorMap[new BlockColor { Color = BlockColors.White, Darkness = 3 }] = new float[3] { 0.746f, 0.827f, 0.946f };
  122. colorMap[new BlockColor { Color = BlockColors.White, Darkness = 4 }] = new float[3] { 0.71f, 0.789f, 0.888f };
  123. colorMap[new BlockColor { Color = BlockColors.White, Darkness = 5 }] = new float[3] { 0.597f, 0.664f, 0.742f };
  124. colorMap[new BlockColor { Color = BlockColors.White, Darkness = 6 }] = new float[3] { 0.484f, 0.535f, 0.61f };
  125. colorMap[new BlockColor { Color = BlockColors.White, Darkness = 7 }] = new float[3] { 0.355f, 0.39f, 0.449f };
  126. colorMap[new BlockColor { Color = BlockColors.White, Darkness = 8 }] = new float[3] { 0f, 0f, 0f };
  127. colorMap[new BlockColor { Color = BlockColors.White, Darkness = 9 }] = new float[3] { 0.581f, 0.643f, 0.745f };
  128. // Pink
  129. colorMap[new BlockColor { Color = BlockColors.Pink, Darkness = 0 }] = new float[3] { 1f, 0.657f, 1f };
  130. colorMap[new BlockColor { Color = BlockColors.Pink, Darkness = 1 }] = new float[3] { 0.912f, 0.98f, 0.993f };
  131. colorMap[new BlockColor { Color = BlockColors.Pink, Darkness = 2 }] = new float[3] { 0.897f, 0.905f, 0.991f };
  132. colorMap[new BlockColor { Color = BlockColors.Pink, Darkness = 3 }] = new float[3] { 0.892f, 0.776f, 0.988f };
  133. colorMap[new BlockColor { Color = BlockColors.Pink, Darkness = 4 }] = new float[3] { 0.898f, 0.698f, 0.992f };
  134. colorMap[new BlockColor { Color = BlockColors.Pink, Darkness = 5 }] = new float[3] { 0.875f, 0.267f, 0.882f };
  135. colorMap[new BlockColor { Color = BlockColors.Pink, Darkness = 6 }] = new float[3] { 0.768f, 0.199f, 0.767f };
  136. colorMap[new BlockColor { Color = BlockColors.Pink, Darkness = 7 }] = new float[3] { 0.628f, 0.15f, 0.637f };
  137. colorMap[new BlockColor { Color = BlockColors.Pink, Darkness = 8 }] = new float[3] { 0.435f, 0.133f, 0.439f };
  138. colorMap[new BlockColor { Color = BlockColors.Pink, Darkness = 9 }] = new float[3] { 0.726f, 0.659f, 0.871f };
  139. // Purple
  140. colorMap[new BlockColor { Color = BlockColors.Purple, Darkness = 0 }] = new float[3] { 0.764f, 0.587f, 1f };
  141. colorMap[new BlockColor { Color = BlockColors.Purple, Darkness = 1 }] = new float[3] { 0.893f, 0.966f, 0.992f };
  142. colorMap[new BlockColor { Color = BlockColors.Purple, Darkness = 2 }] = new float[3] { 0.842f, 0.877f, 0.991f };
  143. colorMap[new BlockColor { Color = BlockColors.Purple, Darkness = 3 }] = new float[3] { 0.794f, 0.747f, 0.99f };
  144. colorMap[new BlockColor { Color = BlockColors.Purple, Darkness = 4 }] = new float[3] { 0.783f, 0.669f, 0.992f };
  145. colorMap[new BlockColor { Color = BlockColors.Purple, Darkness = 5 }] = new float[3] { 0.636f, 0.249f, 0.991f };
  146. colorMap[new BlockColor { Color = BlockColors.Purple, Darkness = 6 }] = new float[3] { 0.548f, 0.18f, 0.896f };
  147. colorMap[new BlockColor { Color = BlockColors.Purple, Darkness = 7 }] = new float[3] { 0.441f, 0.152f, 0.726f };
  148. colorMap[new BlockColor { Color = BlockColors.Purple, Darkness = 8 }] = new float[3] { 0.308f, 0.135f, 0.498f };
  149. colorMap[new BlockColor { Color = BlockColors.Purple, Darkness = 9 }] = new float[3] { 0.659f, 0.646f, 0.909f };
  150. // Blue
  151. colorMap[new BlockColor { Color = BlockColors.Blue, Darkness = 0 }] = new float[3] { 0.449f, 0.762f, 1f };
  152. colorMap[new BlockColor { Color = BlockColors.Blue, Darkness = 1 }] = new float[3] { 0.856f, 0.971f, 0.992f };
  153. colorMap[new BlockColor { Color = BlockColors.Blue, Darkness = 2 }] = new float[3] { 0.767f, 0.907f, 0.989f };
  154. colorMap[new BlockColor { Color = BlockColors.Blue, Darkness = 3 }] = new float[3] { 0.642f, 0.836f, 0.992f };
  155. colorMap[new BlockColor { Color = BlockColors.Blue, Darkness = 4 }] = new float[3] { 0.564f, 0.812f, 0.989f };
  156. colorMap[new BlockColor { Color = BlockColors.Blue, Darkness = 5 }] = new float[3] { 0.211f, 0.621f, 0.989f };
  157. colorMap[new BlockColor { Color = BlockColors.Blue, Darkness = 6 }] = new float[3] { 0.143f, 0.525f, 0.882f };
  158. colorMap[new BlockColor { Color = BlockColors.Blue, Darkness = 7 }] = new float[3] { 0.114f, 0.410f, 0.705f };
  159. colorMap[new BlockColor { Color = BlockColors.Blue, Darkness = 8 }] = new float[3] { 0.116f, 0.289f, 0.481f };
  160. colorMap[new BlockColor { Color = BlockColors.Blue, Darkness = 9 }] = new float[3] { 0.571f, 0.701f, 0.901f };
  161. // Aqua
  162. colorMap[new BlockColor { Color = BlockColors.Aqua, Darkness = 0 }] = new float[3] { 0.408f, 0.963f, 1f };
  163. colorMap[new BlockColor { Color = BlockColors.Aqua, Darkness = 1 }] = new float[3] { 0.838f, 0.976f, 0.990f };
  164. colorMap[new BlockColor { Color = BlockColors.Aqua, Darkness = 2 }] = new float[3] { 0.747f, 0.961f, 0.994f };
  165. colorMap[new BlockColor { Color = BlockColors.Aqua, Darkness = 3 }] = new float[3] { 0.605f, 0.948f, 0.990f };
  166. colorMap[new BlockColor { Color = BlockColors.Aqua, Darkness = 4 }] = new float[3] { 0.534f, 0.954f, 0.993f };
  167. colorMap[new BlockColor { Color = BlockColors.Aqua, Darkness = 5 }] = new float[3] { 0.179f, 0.841f, 0.991f };
  168. colorMap[new BlockColor { Color = BlockColors.Aqua, Darkness = 6 }] = new float[3] { 0.121f, 0.719f, 0.868f };
  169. colorMap[new BlockColor { Color = BlockColors.Aqua, Darkness = 7 }] = new float[3] { 0.117f, 0.574f, 0.687f };
  170. colorMap[new BlockColor { Color = BlockColors.Aqua, Darkness = 8 }] = new float[3] { 0.116f, 0.399f, 0.478f };
  171. colorMap[new BlockColor { Color = BlockColors.Aqua, Darkness = 9 }] = new float[3] { 0.556f, 0.768f, 0.901f };
  172. // Green
  173. colorMap[new BlockColor { Color = BlockColors.Green, Darkness = 0 }] = new float[3] { 0.344f, 1f, 0.579f };
  174. colorMap[new BlockColor { Color = BlockColors.Green, Darkness = 1 }] = new float[3] { 0.823f, 0.977f, 0.994f };
  175. colorMap[new BlockColor { Color = BlockColors.Green, Darkness = 2 }] = new float[3] { 0.731f, 0.966f, 0.958f };
  176. colorMap[new BlockColor { Color = BlockColors.Green, Darkness = 3 }] = new float[3] { 0.643f, 0.964f, 0.873f };
  177. colorMap[new BlockColor { Color = BlockColors.Green, Darkness = 4 }] = new float[3] { 0.498f, 0.961f, 0.721f };
  178. colorMap[new BlockColor { Color = BlockColors.Green, Darkness = 5 }] = new float[3] { 0.176f, 0.853f, 0.415f };
  179. colorMap[new BlockColor { Color = BlockColors.Green, Darkness = 6 }] = new float[3] { 0.120f, 0.728f, 0.350f };
  180. colorMap[new BlockColor { Color = BlockColors.Green, Darkness = 7 }] = new float[3] { 0.105f, 0.560f, 0.264f };
  181. colorMap[new BlockColor { Color = BlockColors.Green, Darkness = 8 }] = new float[3] { 0.122f, 0.392f, 0.221f };
  182. colorMap[new BlockColor { Color = BlockColors.Green, Darkness = 9 }] = new float[3] { 0.542f, 0.771f, 0.717f };
  183. // Lime
  184. colorMap[new BlockColor { Color = BlockColors.Lime, Darkness = 0 }] = new float[3] { 0.705f, 1f, 0.443f };
  185. colorMap[new BlockColor { Color = BlockColors.Lime, Darkness = 1 }] = new float[3] { 0.869f, 0.978f, 0.991f };
  186. colorMap[new BlockColor { Color = BlockColors.Lime, Darkness = 2 }] = new float[3] { 0.815f, 0.967f, 0.932f };
  187. colorMap[new BlockColor { Color = BlockColors.Lime, Darkness = 3 }] = new float[3] { 0.778f, 0.962f, 0.821f };
  188. colorMap[new BlockColor { Color = BlockColors.Lime, Darkness = 4 }] = new float[3] { 0.753f, 0.964f, 0.631f };
  189. colorMap[new BlockColor { Color = BlockColors.Lime, Darkness = 5 }] = new float[3] { 0.599f, 0.855f, 0.268f };
  190. colorMap[new BlockColor { Color = BlockColors.Lime, Darkness = 6 }] = new float[3] { 0.505f, 0.712f, 0.201f };
  191. colorMap[new BlockColor { Color = BlockColors.Lime, Darkness = 7 }] = new float[3] { 0.376f, 0.545f, 0.185f };
  192. colorMap[new BlockColor { Color = BlockColors.Lime, Darkness = 8 }] = new float[3] { 0.268f, 0.379f, 0.172f };
  193. colorMap[new BlockColor { Color = BlockColors.Lime, Darkness = 9 }] = new float[3] { 0.631f, 0.768f, 0.690f };
  194. // Yellow
  195. colorMap[new BlockColor { Color = BlockColors.Yellow, Darkness = 0 }] = new float[3] { 0.893f, 1f, 0.457f };
  196. colorMap[new BlockColor { Color = BlockColors.Yellow, Darkness = 1 }] = new float[3] { 0.887f, 0.981f, 0.995f };
  197. colorMap[new BlockColor { Color = BlockColors.Yellow, Darkness = 2 }] = new float[3] { 0.878f, 0.971f, 0.920f };
  198. colorMap[new BlockColor { Color = BlockColors.Yellow, Darkness = 3 }] = new float[3] { 0.874f, 0.964f, 0.802f };
  199. colorMap[new BlockColor { Color = BlockColors.Yellow, Darkness = 4 }] = new float[3] { 0.875f, 0.964f, 0.619f };
  200. colorMap[new BlockColor { Color = BlockColors.Yellow, Darkness = 5 }] = new float[3] { 0.771f, 0.846f, 0.246f };
  201. colorMap[new BlockColor { Color = BlockColors.Yellow, Darkness = 6 }] = new float[3] { 0.638f, 0.703f, 0.192f };
  202. colorMap[new BlockColor { Color = BlockColors.Yellow, Darkness = 7 }] = new float[3] { 0.477f, 0.522f, 0.142f };
  203. colorMap[new BlockColor { Color = BlockColors.Yellow, Darkness = 8 }] = new float[3] { 0.330f, 0.363f, 0.151f };
  204. colorMap[new BlockColor { Color = BlockColors.Yellow, Darkness = 9 }] = new float[3] { 0.693f, 0.763f, 0.678f };
  205. // Orange
  206. colorMap[new BlockColor { Color = BlockColors.Orange, Darkness = 0 }] = new float[3] { 0.891f, 0.750f, 0.423f };
  207. colorMap[new BlockColor { Color = BlockColors.Orange, Darkness = 1 }] = new float[3] { 0.883f, 0.948f, 0.992f };
  208. colorMap[new BlockColor { Color = BlockColors.Orange, Darkness = 2 }] = new float[3] { 0.877f, 0.873f, 0.894f };
  209. colorMap[new BlockColor { Color = BlockColors.Orange, Darkness = 3 }] = new float[3] { 0.878f, 0.831f, 0.771f };
  210. colorMap[new BlockColor { Color = BlockColors.Orange, Darkness = 4 }] = new float[3] { 0.886f, 0.801f, 0.595f };
  211. colorMap[new BlockColor { Color = BlockColors.Orange, Darkness = 5 }] = new float[3] { 0.777f, 0.621f, 0.241f };
  212. colorMap[new BlockColor { Color = BlockColors.Orange, Darkness = 6 }] = new float[3] { 0.637f, 0.507f, 0.168f };
  213. colorMap[new BlockColor { Color = BlockColors.Orange, Darkness = 7 }] = new float[3] { 0.466f, 0.364f, 0.123f };
  214. colorMap[new BlockColor { Color = BlockColors.Orange, Darkness = 8 }] = new float[3] { 0.323f, 0.266f, 0.138f };
  215. colorMap[new BlockColor { Color = BlockColors.Orange, Darkness = 9 }] = new float[3] { 0.689f, 0.672f, 0.667f };
  216. // Red
  217. colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 0 }] = new float[3] { 0.890f, 0.323f, 0.359f };
  218. colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 1 }] = new float[3] { 0.879f, 0.863f, 0.987f };
  219. colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 2 }] = new float[3] { 0.872f, 0.758f, 0.868f };
  220. colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 3 }] = new float[3] { 0.887f, 0.663f, 0.756f };
  221. colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 4 }] = new float[3] { 0.903f, 0.546f, 0.608f };
  222. colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 5 }] = new float[3] { 0.785f, 0.222f, 0.222f };
  223. colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 6 }] = new float[3] { 0.641f, 0.155f, 0.152f };
  224. colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 7 }] = new float[3] { 0.455f, 0.105f, 0.108f };
  225. colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 8 }] = new float[3] { 0.320f, 0.121f, 0.133f };
  226. colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 9 }] = new float[3] { 0.687f, 0.571f, 0.661f };
  227. // default
  228. colorMap[new BlockColor { Color = BlockColors.Default, Darkness = 0 }] = new float[3] { -1f, -1f, -1f };
  229. }
  230. private static void BuildBotColorMap()
  231. {
  232. botColorMap = new Dictionary<byte, BlockColor>();
  233. // standard colours
  234. botColorMap[0] = new BlockColor { Color = BlockColors.White, Darkness = 0 };
  235. botColorMap[1] = new BlockColor { Color = BlockColors.White, Darkness = 6 };
  236. botColorMap[4] = new BlockColor { Color = BlockColors.White, Darkness = 8 };
  237. botColorMap[5] = new BlockColor { Color = BlockColors.Red, Darkness = 5 };
  238. botColorMap[2] = new BlockColor { Color = BlockColors.Orange, Darkness = 0 };
  239. botColorMap[6] = new BlockColor { Color = BlockColors.Yellow, Darkness = 0 };
  240. botColorMap[7] = new BlockColor { Color = BlockColors.Green, Darkness = 5 };
  241. botColorMap[3] = new BlockColor { Color = BlockColors.Aqua, Darkness = 5 };
  242. botColorMap[9] = new BlockColor { Color = BlockColors.Blue, Darkness = 5 };
  243. botColorMap[10] = new BlockColor { Color = BlockColors.Purple, Darkness = 5 };
  244. // premium colours
  245. botColorMap[16] = new BlockColor { Color = BlockColors.Red, Darkness = 0 };
  246. botColorMap[17] = new BlockColor { Color = BlockColors.Red, Darkness = 7 };
  247. botColorMap[11] = new BlockColor { Color = BlockColors.Orange, Darkness = 6 };
  248. botColorMap[18] = new BlockColor { Color = BlockColors.Purple, Darkness = 9 };
  249. botColorMap[19] = new BlockColor { Color = BlockColors.Pink, Darkness = 9 };
  250. botColorMap[20] = new BlockColor { Color = BlockColors.Orange, Darkness = 5 };
  251. botColorMap[14] = new BlockColor { Color = BlockColors.Yellow, Darkness = 3 };
  252. botColorMap[21] = new BlockColor { Color = BlockColors.Green, Darkness = 7 };
  253. botColorMap[22] = new BlockColor { Color = BlockColors.Lime, Darkness = 8 };
  254. botColorMap[13] = new BlockColor { Color = BlockColors.Green, Darkness = 6 };
  255. botColorMap[12] = new BlockColor { Color = BlockColors.Lime, Darkness = 5 };
  256. // blue gang
  257. botColorMap[23] = new BlockColor { Color = BlockColors.Blue, Darkness = 8 };
  258. botColorMap[24] = new BlockColor { Color = BlockColors.Aqua, Darkness = 8 };
  259. botColorMap[25] = new BlockColor { Color = BlockColors.Blue, Darkness = 7 };
  260. botColorMap[26] = new BlockColor { Color = BlockColors.White, Darkness = 5 };
  261. botColorMap[27] = new BlockColor { Color = BlockColors.White, Darkness = 4 };
  262. botColorMap[28] = new BlockColor { Color = BlockColors.Aqua, Darkness = 4 };
  263. botColorMap[29] = new BlockColor { Color = BlockColors.Purple, Darkness = 8 };
  264. // purples & pinks
  265. botColorMap[30] = new BlockColor { Color = BlockColors.Pink, Darkness = 0 };
  266. botColorMap[8] = new BlockColor { Color = BlockColors.Pink, Darkness = 5 };
  267. botColorMap[31] = new BlockColor { Color = BlockColors.Pink, Darkness = 4 };
  268. botColorMap[15] = new BlockColor { Color = BlockColors.Red, Darkness = 3 };
  269. }
  270. }
  271. }