Mono C#奇怪的控制台颜色

时间:2015-09-04 16:00:42

标签: c# terminal mono

我有这个Console艺术功能,

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

这是我的Main()

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

打印的两种white颜色不相同,如下所示

Screenshot

在调试器中,我可以看到previousColor也是ConsoleColor.White

1 个答案:

答案 0 :(得分:3)

这个区域偶尔会造成混乱。称之为限制而非错误。

有两个相关的因素:

  1. 大多数在X中实现颜色的终端都是这样做的,就像xterm和rxvt自20世纪90年代中期以来所做的那样:终端最初可以拥有默认前景和指定了背景颜色,稍后添加了 ANSI (和扩展)颜色。与Linux控制台不同,无法保证默认 ANSI 颜色相关。

    VTE(终端的功能部分)遵循该设计,与Konsole一样。

    引用xterm manual reverseVideo的描述,表明 ANSI 颜色与终端使用的默认颜色有区别:

  2.                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是为了模仿(并在某些部分,适应大块)ncurses而编写的。 ncurses FAQ Ncurses resets my colors to white/black注意到ncurses假设默认颜色为黑色白色 - Console也遵循该设计。

      它可能会进一步遵循ncurses以更加清楚明确的" white"通过告诉终端绘制白色文本与隐含的"白色" (通过将颜色重置为默认值)。