Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

windows - RGB Specific Console Text Color C++

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

http://www.yourgamercard.net/screen/i/4a8c57.png

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...