Newer
Older
Sunfish / src / Sunfish.Cli / Tui / TuiIcon.cs
@Ivan Dominguez Ivan Dominguez 21 hours ago 3 KB 3.0
using System.Globalization;
using System.Text;

namespace Sunfish.Cli.Tui;

/// <summary>
/// El pez luna del icono, en 16x16 pixeles (art/logo.png reducido). Se dibuja con medios bloques:
/// cada celda es dos pixeles, el de arriba como color de texto y el de abajo como fondo.
/// "......" es transparente.
/// </summary>
internal static class TuiIcon
{
    public const int Columns = 16;
    public const int Rows = 8;

    private static readonly string[] Pixels =
    [
        "...... ...... ...... ...... ...... ...... ...... ...... ...... ...... ...... ...... ...... ...... ...... ......",
        "...... ...... ...... ...... ...... ...... 94A3D4 8588C1 ...... ...... ...... ...... ...... ...... ...... ......",
        "...... ...... ...... ...... ...... 7E86BF E6FFFF 9EA8D1 ...... ...... ...... ...... ...... ...... ...... ......",
        "...... ...... ...... ...... ...... 87A7D3 EAFFFF 98A1CB ...... ...... ...... ...... ...... ...... ...... ......",
        "...... ...... ...... ...... 827CBC ADCAE5 E0F9FD B7CCE6 A3A2CC A298C7 8475B4 ...... ...... ...... ...... ......",
        "...... ...... ...... 94AAD7 D3F8FF DCF2FA E2F2F9 F6FFFF FFFFFF FEFFFF EBFAFF B0BADB ...... ...... ...... ......",
        "...... ...... 91ACD8 B0E8FF ADDCF2 C8E6F6 CFEBF7 DCF1F8 E1F2FA DAEFF8 D4EEF9 E7FFFF A4B1D7 ...... B6E0EE ......",
        "...... ...... 87C4EF 9AD9F3 A0D6F0 BDE3F5 C5E6F7 C4E3F3 B5DEF1 ABD9F1 B9E2F5 9EC4E3 ADD8F6 7789C2 ...... ......",
        "...... ...... 75C1EF 94D4F1 98D3EF ADDCF3 BCE2F5 A7D5ED 70BFE5 8DCDEC B7E0F3 9CCCE9 98D5F7 6877B8 ...... ......",
        "...... ...... 62B5EA 91D5F3 96D1EE 95D1EF 97D2EF 9ED5F0 91CFEC 9FD5F0 9ED4EF A5E0F9 7FC3F2 ...... ...... ......",
        "...... ...... 487EBF 5FC7F5 76CDF2 83CCEE 8CCEED 8ACFEE 8AD1F1 87CFF0 82D2F4 66C3F5 4366AE ...... ...... ......",
        "...... ...... ...... 4580C2 3E8DC8 42A2D3 51B5E3 4FB0E1 4BA7DC 4CA9DD 4495CE 4265AD ...... ...... ...... ......",
        "...... ...... ...... ...... ...... 4289C2 45BFF2 4377B9 ...... ...... ...... ...... ...... ...... ...... ......",
        "...... ...... ...... ...... ...... 4685C1 4BBFF7 4F59A4 ...... ...... ...... ...... ...... ...... ...... ......",
        "...... ...... ...... ...... ...... 4971B2 4CAFED ...... ...... ...... ...... ...... ...... ...... ...... ......",
        "...... ...... ...... ...... ...... ...... 4C64AD ...... ...... ...... ...... ...... ...... ...... ...... ......",
    ];

    /// <summary>Las 8 lineas del icono con sus secuencias de color; cada una ocupa 16 columnas.</summary>
    public static string[] Render()
    {
        var grid = Pixels.Select(row => row.Split(' ')).ToArray();
        var lines = new string[Rows];
        for (int row = 0; row < Rows; row++)
        {
            var sb = new StringBuilder();
            for (int x = 0; x < Columns; x++)
            {
                var top = Parse(grid[row * 2][x]);
                var bottom = Parse(grid[row * 2 + 1][x]);
                if (top is null && bottom is null)
                    sb.Append(Terminal.Reset).Append(' ');
                else if (bottom is null)
                    sb.Append(Terminal.Reset).Append(Terminal.Fg(top!.Value.R, top.Value.G, top.Value.B)).Append('▀'); // ▀
                else if (top is null)
                    sb.Append(Terminal.Reset).Append(Terminal.Fg(bottom.Value.R, bottom.Value.G, bottom.Value.B)).Append('▄'); // ▄
                else
                    sb.Append(Terminal.Fg(top.Value.R, top.Value.G, top.Value.B))
                      .Append(Terminal.Bg(bottom.Value.R, bottom.Value.G, bottom.Value.B)).Append('▀');
            }
            sb.Append(Terminal.Reset);
            lines[row] = sb.ToString();
        }
        return lines;
    }

    private static (int R, int G, int B)? Parse(string hex)
    {
        if (hex.StartsWith('.'))
            return null;
        int v = int.Parse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
        return (v >> 16, (v >> 8) & 0xFF, v & 0xFF);
    }
}