Browse Source

Add default colour quantization support

tags/v1.0.0
NGnius (Graham) 4 years ago
parent
commit
bb19c084d1
4 changed files with 39 additions and 5 deletions
  1. +10
    -0
      Pixi/Common/ColorSpaceUtility.cs
  2. +5
    -1
      Pixi/Images/ImageCommandImporter.cs
  3. +1
    -1
      Pixi/Images/ImageTextBlockImporter.cs
  4. +23
    -3
      Pixi/Images/PixelUtility.cs

+ 10
- 0
Pixi/Common/ColorSpaceUtility.cs View File

@@ -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()


+ 5
- 1
Pixi/Images/ImageCommandImporter.cs View File

@@ -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"
}
};
}



+ 1
- 1
Pixi/Images/ImageTextBlockImporter.cs View File

@@ -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},


+ 23
- 3
Pixi/Images/PixelUtility.cs View File

@@ -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("<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 x = 0; x < img.width; x++)
{
Color pixel = img.GetPixel(x, y);
imgString.Append("<color=");
imgString.Append(HexPixel(pixel));
imgString.Append(">");
if (!lastPixelAssigned || lastPixel != pixel)
{
imgString.Append("<color=");
imgString.Append(HexPixel(pixel));
imgString.Append(">");
lastPixel = pixel;
if (!lastPixelAssigned)
{
lastPixelAssigned = true;
}
}
imgString.Append("\u25a0");
}
imgString.Append("<br>");


Loading…
Cancel
Save