Add default colour quantization support

This commit is contained in:
NGnius (Graham) 2020-08-16 11:27:46 -04:00
parent 90a653b1c4
commit bb19c084d1
4 changed files with 39 additions and 5 deletions

View file

@ -65,6 +65,14 @@ namespace Pixi.Common
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BlockColor QuantizeToBlockColor(float[] pixel) 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])); 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 = 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 = 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 }; 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() private static void BuildBotColorMap()

View file

@ -54,7 +54,11 @@ namespace Pixi.Images
commandBlockContents[name] = text; commandBlockContents[name] = text;
return new BlockJsonInfo[] return new BlockJsonInfo[]
{ {
new BlockJsonInfo {name = "ConsoleBlock"} new BlockJsonInfo
{
color = new float[] {-1f, -1f, -1f},
name = "ConsoleBlock"
}
}; };
} }

View file

@ -65,7 +65,7 @@ namespace Pixi.Images
{ {
new BlockJsonInfo new BlockJsonInfo
{ {
color = new float[] {0f, 0f, 0f}, color = new float[] {-1f, -1f, -1f},
name = "TextBlock", name = "TextBlock",
position = new float[] {0f, 0f, 0f}, position = new float[] {0f, 0f, 0f},
rotation = new float[] {0f, 0f, 0f}, rotation = new float[] {0f, 0f, 0f},

View file

@ -19,17 +19,37 @@ namespace Pixi.Images
return "#"+ColorUtility.ToHtmlStringRGBA(pixel); 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) public static string TextureToString(Texture2D img)
{ {
StringBuilder imgString = new StringBuilder("<cspace=-0.13em><line-height=40%>"); StringBuilder imgString = new StringBuilder("<cspace=-0.13em><line-height=40%>");
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 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++) for (int x = 0; x < img.width; x++)
{ {
Color pixel = img.GetPixel(x, y); Color pixel = img.GetPixel(x, y);
if (!lastPixelAssigned || lastPixel != pixel)
{
imgString.Append("<color="); imgString.Append("<color=");
imgString.Append(HexPixel(pixel)); imgString.Append(HexPixel(pixel));
imgString.Append(">"); imgString.Append(">");
lastPixel = pixel;
if (!lastPixelAssigned)
{
lastPixelAssigned = true;
}
}
imgString.Append("\u25a0"); imgString.Append("\u25a0");
} }
imgString.Append("<br>"); imgString.Append("<br>");