简体   繁体   English

Mono C#奇怪的控制台颜色

[英]Mono C# strange console color

I have this Console art function, 我有这个Console艺术功能,

public static void Ask(string message)
{
    ConsoleColor previousColor = Console.ForegroundColor;
    Console.ForegroundColor = ConsoleColor.White;
    Console.Write (message);
    Console.Write (" : ");
    Console.ForegroundColor = previousColor;
}

This is my Main() 这是我的Main()

Console.WriteLine("Hello World");
Ask("Roll No");

The two white colors printed aren't the same, as shown below 打印的两种white颜色不同,如下所示

截图

In the debugger, I can see that previousColor is also ConsoleColor.White . 在调试器中,我可以看到previousColor也是ConsoleColor.White

This is an area which occasionally causes confusion. 这个区域偶尔会造成混乱。 Call it a limitation rather than a bug. 称之为限制而非错误。

There are two factors which are relevant: 有两个相关的因素:

  1. most terminals which implement color in X do this as xterm and rxvt have done since the mid-1990s: the terminal originally could have the default foreground and background colors specified, and ANSI (and extended) colors were added later. 大多数在X中实现颜色的终端都是这样做的,就像xterm和rxvt自20世纪90年代中期以来所做的那样:终端最初可能具有指定的默认前景色和背景色,并且稍后添加了ANSI (和扩展)颜色。 Unlike the Linux console, there is no guarantee that the default and ANSI colors are related. 与Linux控制台不同,无法保证默认颜色和ANSI颜色相关。

    VTE (which is the functional part of Terminal) followed that design, as did Konsole. VTE(终端的功能部分)遵循该设计,Konsole也是如此。

    Quoting from xterm's manual description of reverseVideo shows that there is a distinction between ANSI colors and the default colors used for the terminal: 引用xterm的reverseVideo 手册描述显示ANSI颜色与终端使用的默认颜色之间存在区别:

Other control sequences can alter the foreground and background
               colors which are used:

               o   Programs can also use the ANSI color control  sequences  to
                   set the foreground and background colors.

               o   Extensions  to the ANSI color controls (such as 16-, 88- or
                   256-colors) are treated similarly to the ANSI control.

               o   Using other control sequences (the  "dynamic  colors"  fea-
                   ture),  a  program can change the foreground and background
                   colors.
  1. Mono Console is written to imitate (and in some parts, adapt chunks of) ncurses. Mono Console是为了模仿(并在某些部分,适应大块的)ncurses而编写的。 The ncurses FAQ Ncurses resets my colors to white/black notes that ncurses assumes the default colors are white on black — Console follows that design as well. ncurses常见问题解答Ncurses将我的颜色重置为白色/黑色注释,ncurses假定默认颜色为黑色白色 - Console遵循该设计。

    It could have followed ncurses a bit further to be more aware of the distinction between an explicit "white" done by telling the terminal to draw white text versus an implicit "white" (by resetting the colors to their default values). 它可以进一步跟随ncurses更多地意识到通过告诉终端绘制白色文本与隐式“白色”(通过将颜色重置为其默认值)完成的显式“白色”之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM