WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

来自多彩世界的控制台——C#控制台输出彩色字符画

Posted on 2024-05-27 22:36  WebEnh  阅读(439)  评论(2编辑  收藏  举报

引言

看到酷安上有这样一个活动,萌生了用 C# 生成字符画的想法,先放出原图。


 
酷安手绘牛啤

 
 

§1 黑白

将图像转换成字符画在 C# 中很简单,思路大致如下:

  1. 加载图像,逐像素提取明度。
  2. 根据明度映射到字符列表中对应的字符。
  3. 输出字符。

GetChars函数负责将传入的图像按一定比例导出字符画的字符串。hScale为横向比例,即每次跳过的横向像素数;vScale为纵向比例,在控制台中输出推荐为hScale的 2 倍。

private static string GetChars(Bitmap bmp, int hScale, int vScale)
{
    StringBuilder sb = new StringBuilder();
    for (int h = 0; h < bmp.Height; h += vScale)
    {
        for (int w = 0; w < bmp.Width; w += hScale)
        {
            Color color = bmp.GetPixel(w, h);
            float brightness = color.GetBrightness(); // 这里的明度也可以使用 RGB 分量合成
            char ch = GetChar(brightness);
            sb.Append(ch);
        }
        sb.AppendLine();
    }
    return sb.ToString();
}

GetChar负责做明度到字符的映射工作,由于brightness取值范围为 [0, 1],所以需要乘 0.99 防止index越界。listChar是可用的字符列表,自定义只需遵循一条规则,从左往右字符应该越来越复杂。

private static readonly List<char> listChar = 
    new List<char>() { ' ', '^', '+', '!', '$', '#', '*', '%', '@' };
private static char GetChar(float brightness)
{
    int index = (int)(brightness * 0.99 * listChar.Count);
    return listChar[index];
}

调用函数,输出结果。初具雏形,黑白样式减少了不少神韵。


 
 

§2 有限彩色

2.1 Console

一开始希望通过改变Console.ForegroundColor属性来改变色彩,但是残酷的事实是这个属性只接受ConsoleColor枚举中的 16 个颜色。将全彩图片映射成 16 色输出,费力不讨好,遂求其他方法。

2.2 Colorful.Console

找到了一个彩色控制台的库 Colorful Console。看网页介绍挺厉害的,RGB、渐变色、多色输出……妥了,这肯定符合我们的需要,通过 nuget 可以直接添加到项目中。
在引用区域加一行,就可以把代码中的ConsoleColorfulConsole替代。

using Console = Colorful.Console;

GetChars函数需要改变一下,因为每个字符的颜色不同,所以要在函数里面增加输出。好简单,输出内容后面加个颜色的参数就可以了。

private static string GetChars(Bitmap bmp, int hScale, int vScale, bool shouldDraw)
{
    StringBuilder sb = new StringBuilder();
    for (int h = 0; h < bmp.Height; h += vScale)
    {
        for (int w = 0; w < bmp.Width; w += hScale)
        {
            Color color = bmp.GetPixel(w, h);
            float brightness = color.GetBrightness();
            char ch = GetChar(brightness);
            if (shouldDraw)
            {
                Console.Write(ch, color);
            }
            sb.Append(ch);
        }
        if (shouldDraw) { Console.WriteLine(); }
        sb.AppendLine();
    }
    return sb.ToString();
}

然而现实再一次残酷起来,输出结果一片黑,使用白色背景看一看。


 
 

可能看不清,不过牛角的位置确实有几个字符不是黑色,那我们换张图片来看。可以看到确实有彩色输出,不过效果尚可的仅限最前面的一些字符,之后白色完全不见了。


 
 

在测试官网上的操作都没有问题后,我陷入了深深的思考,NMD,为什么?直到我看到了官网上最下面的一段话。

Colorful.Console can only write to the console in 16 different colors (including the black that's used as the console's background, by default!) in a single console session. This is a limitation of the Windows console itself (ref: MSDN), and it's one that I wasn't able to work my way around. If you know of a workaround, let me know!

Colorful.Console只能同时输出 16 种颜色,果然原版Console能接受的ConsoleColor枚举也是 16 种颜色是算计好的。可恶,难道只能到此为止了吗?
我不甘心。

§3 全彩

终于,我找到了这个 visual studio - Custom text color in C# console application? - Stack Overflow。在下面 Alexei Shcherbakov 和 Olivier Jacot-Descombes 的回答中,我看到了希望。

Since Windows 10 Anniversary Update, console can use ANSI/VT100 color codes
You need set flag ENABLE_VIRTUAL_TERMINAL_PROCESSING(0x4) by SetConsoleMode
Use sequences:
"\x1b[48;5;" + s + "m" - set background color by index in table (0-255)
"\x1b[38;5;" + s + "m" - set foreground color by index in table (0-255)
"\x1b[48;2;" + r + ";" + g + ";" + b + "m" - set background by r,g,b values
"\x1b[38;2;" + r + ";" + g + ";" + b + "m" - set foreground by r,g,b values
Important notice: Internally Windows have only 256 (or 88) colors in table and Windows will used nearest to (r,g,b) value from table.

有了这个神奇的ENABLE_VIRTUAL_TERMINAL_PROCESSING(0x4),就可以随意修改前后景颜色了。说干就干,首先需要增加一个NativeMethods类,用来 Call kernel32.dll里的 3 个函数。

using System;
using System.Runtime.InteropServices;

namespace Img2ColorfulChars
{
    internal class NativeMethods
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode);
        
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool GetConsoleMode(IntPtr handle, out int mode);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern IntPtr GetStdHandle(int handle);
    }
}

然后在主程序Main函数里一开始增加以下三行,-11代表STD_OUTPUT_HANDLE(GetStdHandle function - Windows Console | Microsoft Docs), 0x4就是上面所说的ENABLE_VIRTUAL_TERMINAL_PROCESSING

var handle = NativeMethods.GetStdHandle(-11);
NativeMethods.GetConsoleMode(handle, out int mode);
NativeMethods.SetConsoleMode(handle, mode | 0x4);

因为我们要修改的是字符的前景色,所以把上一节中GetChars函数里的

Console.Write(ch, color);

替换为

Console.Write($"\x1b[38;2;{color.R};{color.G};{color.B}m{ch}");

输出结果如下,完美。


 
 

尾声

多彩的细节,巧妙的象征,这就是青春啊(不是)。
而这个项目真正的用法:


 
 

项目链接

推荐阅读

 

 



作者:Kabuto_W
链接:https://www.jianshu.com/p/8a083421c11d
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。