From bb19c084d1784b5e48a3684bff61c5805e3dc0fd Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sun, 16 Aug 2020 11:27:46 -0400 Subject: [PATCH] Add default colour quantization support --- Pixi/Common/ColorSpaceUtility.cs | 10 ++++++++++ Pixi/Images/ImageCommandImporter.cs | 6 +++++- Pixi/Images/ImageTextBlockImporter.cs | 2 +- Pixi/Images/PixelUtility.cs | 26 +++++++++++++++++++++++--- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Pixi/Common/ColorSpaceUtility.cs b/Pixi/Common/ColorSpaceUtility.cs index e7de250..554a843 100644 --- a/Pixi/Common/ColorSpaceUtility.cs +++ b/Pixi/Common/ColorSpaceUtility.cs @@ -65,6 +65,14 @@ namespace Pixi.Common [MethodImpl(MethodImplOptions.AggressiveInlining)] public static BlockColor QuantizeToBlockColor(float[] pixel) { + if (pixel.Length < 3 || pixel[0] < 0 || pixel[1] < 0 || pixel[2] < 0) + { + return new BlockColor + { + Color = BlockColors.Default, + Darkness = 0, + }; + } return QuantizeToBlockColor(new Color(pixel[0], pixel[1], pixel[2])); } @@ -229,6 +237,8 @@ namespace Pixi.Common colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 7 }] = new float[3] { 0.455f, 0.105f, 0.108f }; colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 8 }] = new float[3] { 0.320f, 0.121f, 0.133f }; colorMap[new BlockColor { Color = BlockColors.Red, Darkness = 9 }] = new float[3] { 0.687f, 0.571f, 0.661f }; + // default + colorMap[new BlockColor { Color = BlockColors.Default, Darkness = 0 }] = new float[3] { -1f, -1f, -1f }; } private static void BuildBotColorMap() diff --git a/Pixi/Images/ImageCommandImporter.cs b/Pixi/Images/ImageCommandImporter.cs index 1797cfb..97a9551 100644 --- a/Pixi/Images/ImageCommandImporter.cs +++ b/Pixi/Images/ImageCommandImporter.cs @@ -54,7 +54,11 @@ namespace Pixi.Images commandBlockContents[name] = text; return new BlockJsonInfo[] { - new BlockJsonInfo {name = "ConsoleBlock"} + new BlockJsonInfo + { + color = new float[] {-1f, -1f, -1f}, + name = "ConsoleBlock" + } }; } diff --git a/Pixi/Images/ImageTextBlockImporter.cs b/Pixi/Images/ImageTextBlockImporter.cs index b1288c9..7a9fc24 100644 --- a/Pixi/Images/ImageTextBlockImporter.cs +++ b/Pixi/Images/ImageTextBlockImporter.cs @@ -65,7 +65,7 @@ namespace Pixi.Images { new BlockJsonInfo { - color = new float[] {0f, 0f, 0f}, + color = new float[] {-1f, -1f, -1f}, name = "TextBlock", position = new float[] {0f, 0f, 0f}, rotation = new float[] {0f, 0f, 0f}, diff --git a/Pixi/Images/PixelUtility.cs b/Pixi/Images/PixelUtility.cs index 1161e9c..1e19ebc 100644 --- a/Pixi/Images/PixelUtility.cs +++ b/Pixi/Images/PixelUtility.cs @@ -18,18 +18,38 @@ namespace Pixi.Images { return "#"+ColorUtility.ToHtmlStringRGBA(pixel); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Color PixelHex(string hex) + { + if (ColorUtility.TryParseHtmlString(hex, out Color result)) + { + return result; + } + return default; + } public static string TextureToString(Texture2D img) { StringBuilder imgString = new StringBuilder(""); + bool lastPixelAssigned = false; + Color lastPixel = new Color(); for (int y = img.height-1; y >= 0 ; y--) // text origin is top right, but img origin is bottom right { for (int x = 0; x < img.width; x++) { Color pixel = img.GetPixel(x, y); - imgString.Append(""); + if (!lastPixelAssigned || lastPixel != pixel) + { + imgString.Append(""); + lastPixel = pixel; + if (!lastPixelAssigned) + { + lastPixelAssigned = true; + } + } imgString.Append("\u25a0"); } imgString.Append("
");