RGB特定控制台文本颜色C ++ [英] RGB Specific Console Text Color C++

查看:849
本文介绍了RGB特定控制台文本颜色C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将win32控制台应用程序的字体颜色设置为特定的RGB值,分别为50,75,90。我已经尝试过 SetConsoleTextAttribute(),但不幸的是它似乎限制为0或255的R,G或B.

I'm trying to set a win32 console application's font color to a specific RGB value like 50, 75, 90 respectively. I've already tried SetConsoleTextAttribute(), but unfortunately it seems to be limited to 0 or 255 for R, G, or B.

这是可能的,因为在命令提示符属性窗口中可以设置特定的颜色,例如

This has to be possible, because in the command prompt properties window you can set the specific color, like so

我搜索了很多,但似乎唯一的答案是 SetConsoleTextAttribute()

I've searched quite a bit but it seems that the only answer is SetConsoleTextAttribute().

推荐答案

您需要使用 SetConsoleScreenBufferInfoEx 设置此项,请参阅 CONSOLE_SCREEN_BUFFER_INFOEX struct。

You need to use SetConsoleScreenBufferInfoEx to set this, see the ColorTable entry of the CONSOLE_SCREEN_BUFFER_INFOEX struct.

控制台颜色是一个两级过程:控制台属性有四个位用于前景和背景(红色,绿色,蓝色和强度),这似乎将颜色限制为基本的主要和次要颜色。但是这些值被用作颜色表的索引,以确定实际的显示值。所以认为字符属性颜色位为逻辑红色等而不是物理红色。 (默认情况下,字符属性红色映射到的值实际上是RGB红色,但不一定是)。因此,您始终限制为16个索引颜色;但是你可以通过ColorTable将它们设置为任何16种全RGB颜色。

Console colors are a two-level process: there's the console attribute, which has four bits each for foreground and background (red, green, blue and intensity), which appears to limit the colors to the basic primary and secondary colors. But these values are used as indices to the color table, to determine the actual display value. So think of the character attribute 'color' bits as "logical red" etc rather than physical red. (The value that Character Attribute 'red' maps to is actually RGB red by default, but doesn't have to be.) So you're always limited to 16 indexed colors; but you can set those to whatever 16 full-RGB colors you want via the ColorTable.

在上面的对话框中看到的彩色方块的本质就是那个颜色表,并在其字符属性顺序中列出颜色,第一个suqare是逻辑黑色,等等。

The strip of colored squares you see in the dialog above is essentially that color table, and lists the colors in their Character Attribute order, the first suqare being 'logical black', and so on.

这篇关于RGB特定控制台文本颜色C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆