增加透明度参数

This commit is contained in:
ww-rm
2025-10-27 23:33:25 +08:00
parent 29d7e8d9d8
commit 617157044c
3 changed files with 75 additions and 29 deletions

View File

@@ -1,4 +1,5 @@
using Spectre.Console; using SkiaSharp;
using Spectre.Console;
using Spectre.Console.Rendering; using Spectre.Console.Rendering;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -10,7 +11,7 @@ namespace SpineViewerCLI
{ {
public class CanvasAscii : Renderable public class CanvasAscii : Renderable
{ {
private readonly Color?[,] _pixels; private readonly SKColor?[,] _pixels;
/// <summary> /// <summary>
/// Gets the width of the canvas. /// Gets the width of the canvas.
@@ -39,9 +40,14 @@ namespace SpineViewerCLI
public int PixelWidth { get; set; } = 2; public int PixelWidth { get; set; } = 2;
/// <summary> /// <summary>
/// Gets or sets the pixel lettters, ordered by transparency. /// Gets or sets the pixel characters, ordered by transparency.
/// </summary> /// </summary>
public string PixelLetters { get; set; } = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,^`'."; // ".:-=+*#%@" ... "@%#*+=-:." public string PixelCharacters { get; set; } = ".,:;-=+*?oSXBGWM$&%#@";
/// <summary>
/// Whether to use pixel characters instead of spaces.
/// </summary>
public bool UsePixelCharacters { get; set; } = false;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="CanvasAscii"/> class. /// Initializes a new instance of the <see cref="CanvasAscii"/> class.
@@ -63,7 +69,7 @@ namespace SpineViewerCLI
Width = width; Width = width;
Height = height; Height = height;
_pixels = new Color?[Width, Height]; _pixels = new SKColor?[Width, Height];
} }
/// <summary> /// <summary>
@@ -73,7 +79,7 @@ namespace SpineViewerCLI
/// <param name="y">The Y coordinate for the pixel.</param> /// <param name="y">The Y coordinate for the pixel.</param>
/// <param name="color">The pixel color.</param> /// <param name="color">The pixel color.</param>
/// <returns>The same <see cref="CanvasAscii"/> instance so that multiple calls can be chained.</returns> /// <returns>The same <see cref="CanvasAscii"/> instance so that multiple calls can be chained.</returns>
public CanvasAscii SetPixel(int x, int y, Color color) public CanvasAscii SetPixel(int x, int y, SKColor color)
{ {
_pixels[x, y] = color; _pixels[x, y] = color;
return this; return this;
@@ -105,7 +111,7 @@ namespace SpineViewerCLI
throw new InvalidOperationException("Pixel width must be greater than zero."); throw new InvalidOperationException("Pixel width must be greater than zero.");
} }
if (PixelLetters.Length <= 0) if (UsePixelCharacters && PixelCharacters.Length <= 0)
{ {
throw new InvalidOperationException("Pixel letters can't be empty."); throw new InvalidOperationException("Pixel letters can't be empty.");
} }
@@ -141,28 +147,53 @@ namespace SpineViewerCLI
pixels = ScaleDown(width, height); pixels = ScaleDown(width, height);
} }
for (var y = 0; y < height; y++) if (UsePixelCharacters)
{ {
for (var x = 0; x < width; x++) for (var y = 0; y < height; y++)
{ {
var color = pixels[x, y]; for (var x = 0; x < width; x++)
if (color.HasValue)
{ {
yield return new Segment(emptyPixel, new Style(background: color)); var color = pixels[x, y];
if (color.HasValue)
{
var c = color.Value;
yield return new Segment(GetPixelChar(c), new Style(foreground: new(c.Red, c.Green, c.Blue)));
}
else
{
yield return new Segment(emptyPixel);
}
} }
else
{
yield return new Segment(emptyPixel);
}
}
yield return Segment.LineBreak; yield return Segment.LineBreak;
}
}
else
{
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var color = pixels[x, y];
if (color.HasValue)
{
var c = color.Value;
yield return new Segment(emptyPixel, new Style(background: new(c.Red, c.Green, c.Blue)));
}
else
{
yield return new Segment(emptyPixel);
}
}
yield return Segment.LineBreak;
}
} }
} }
private Color?[,] ScaleDown(int newWidth, int newHeight) private SKColor?[,] ScaleDown(int newWidth, int newHeight)
{ {
var buffer = new Color?[newWidth, newHeight]; var buffer = new SKColor?[newWidth, newHeight];
var xRatio = ((Width << 16) / newWidth) + 1; var xRatio = ((Width << 16) / newWidth) + 1;
var yRatio = ((Height << 16) / newHeight) + 1; var yRatio = ((Height << 16) / newHeight) + 1;
@@ -177,12 +208,10 @@ namespace SpineViewerCLI
return buffer; return buffer;
} }
private static float GetBrightness(Color c) => (0.299f * c.R + 0.587f * c.G + 0.114f * c.B) / 255f; private string GetPixelChar(SKColor c)
private string GetPixelLetter(Color c)
{ {
var index = Math.Min((int)(GetBrightness(c) * PixelLetters.Length), PixelLetters.Length - 1); var index = Math.Min((int)(c.Alpha / 255f * PixelCharacters.Length), PixelCharacters.Length - 1);
return new(PixelLetters[index], PixelWidth); return new(PixelCharacters[index], PixelWidth);
} }
} }
} }

View File

@@ -33,6 +33,16 @@ namespace SpineViewerCLI
/// </summary> /// </summary>
public int PixelWidth { get; set; } = 2; public int PixelWidth { get; set; } = 2;
/// <summary>
/// Gets or sets the pixel characters, ordered by transparency.
/// </summary>
public string PixelCharacters { get; set; } = ".,:;-=+*?oSXBGWM$&%#@";
/// <summary>
/// Whether to use pixel characters instead of spaces.
/// </summary>
public bool UsePixelCharacters { get; set; } = false;
/// <summary> /// <summary>
/// Gets or sets the <see cref="SKSamplingOptions"/> that should /// Gets or sets the <see cref="SKSamplingOptions"/> that should
/// be used when scaling the image. Defaults to bicubic sampling. /// be used when scaling the image. Defaults to bicubic sampling.
@@ -127,13 +137,15 @@ namespace SpineViewerCLI
{ {
MaxWidth = MaxWidth, MaxWidth = MaxWidth,
PixelWidth = PixelWidth, PixelWidth = PixelWidth,
PixelCharacters = PixelCharacters,
UsePixelCharacters = UsePixelCharacters,
Scale = false, Scale = false,
}; };
// XXX: 也许是 SkiaSharp@3.119.0 的 bug, 此处像素值一定是非预乘的格式 // XXX: 也许是 SkiaSharp@3.119.0 的 bug, 此处像素值一定是非预乘的格式
for (var x = 0; x < image.Width; x++) for (var y = 0; y < image.Height; y++)
{ {
for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width; x++)
{ {
var p = image.GetPixel(x, y); var p = image.GetPixel(x, y);
if (p.Alpha == 0) continue; if (p.Alpha == 0) continue;
@@ -141,7 +153,7 @@ namespace SpineViewerCLI
byte r = (byte)(p.Red * a); byte r = (byte)(p.Red * a);
byte g = (byte)(p.Green * a); byte g = (byte)(p.Green * a);
byte b = (byte)(p.Blue * a); byte b = (byte)(p.Blue * a);
canvas.SetPixel(x, y, new(r, g, b)); canvas.SetPixel(x, y, new(r, g, b, p.Alpha));
} }
} }

View File

@@ -53,6 +53,11 @@ namespace SpineViewerCLI
DefaultValueFactory = _ => 0f, DefaultValueFactory = _ => 0f,
}; };
public Option<bool> OptUseChars { get; } = new("--use-chars")
{
Description = "Whether to use characters instead of colored spaces for pixels",
};
public PreviewCommand() : base(_name, _desc) public PreviewCommand() : base(_name, _desc)
{ {
OptTime.Validators.Add(r => OptTime.Validators.Add(r =>
@@ -99,7 +104,7 @@ namespace SpineViewerCLI
using var exporter = GetExporterFilledWithArgs(result, spine); using var exporter = GetExporterFilledWithArgs(result, spine);
using var skImage = exporter.ExportMemoryImage(spine); using var skImage = exporter.ExportMemoryImage(spine);
var img = new CanvasImageAscii(skImage); var img = new CanvasImageAscii(skImage) { UsePixelCharacters = result.GetValue(OptUseChars) };
AnsiConsole.Write(img); AnsiConsole.Write(img);
} }