C How to "printf" or "cout" custom RGB colors with different fonts?

Hello everyone,

Wondering how I can "printf" or "cout" different combinations of colors and fonts using C/C++.
Basically want make a real time price list and parameters being displayed to terminal. It would be useless if it did not have some color scheme.
Since it's 2022, ANSI color codes is kinda bland boring.

I have done some research and only find answers based on using specific "ANSI Colors" to output colors to terminal.
Is it possible to use a custom specific or adjustable RGB color scheme?
I'm sure the ANSI color codes can be "hacked/modified" to adjust the colors and fonts, not sure how it could be done.

Maybe there are other methods...

Using FreeBSD 13.1 (Headless, no desktop enviroment).

Thanks.
 
Neither C nor C++ have any notion of "color", all they know is input and output streams of "bytes".

So, what you seem to want is a feature of your terminal (or, X terminal emulator). Many terminals support "ANSI" sequences for, among other things, colors. But if you want to stay portable, you shouldn't rely on it and instead use termcap(5) (the ncurses(3) lib provides an easy-to-use interface), so the TERM environment variable is respected and you can make sure your terminal receives control codes it actually understands.

For a long time, many terminals also supported an extended palette of 256 colors, and nowadays, even true-color (24bit RGB) isn't uncommon any more. On a quick search with google, I found this info (didn't check it in depth): https://github.com/termstandard/colors

edit: as for fonts, I'm not aware of any terminal (emulator) supporting a fully custom font selection. But many support rendering bold, italic and underlined text.
 
If you want to use colors in a terminal with C/C++, ncurses is THE way to go.
See https://www.linuxjournal.com/content/programming-color-ncurses for a simple example.

Here's a screenshot of a Roguelike i made using ncursesw (note the additional 'w', it allows you to use wide chars):
inventory2.png
 
Use panel(3) in concert with curses(3), if your work involves drawing windows, especially stacked-windows. You needn't use panel(3) to work with simple window, and curses(3) is enough, nevertheless, in my opinion, implementing windows via panel(3) into your curses(3) projects, makes things much easier.
 
I'm not sure whether curses has support for anything other than the standard terminal colors (16, or 8 with 2 different intensities) – I once attempted to finally learn curses by writing a little game (*), but used only standard colors.

But my point was: if you want to use extended (256 or 16.7M) colors, make sure to respect TERM (curses, and probably other libs, will handle that automatically for you, but it's an issue when going the manual route). Not every terminal supports them, and if you output control sequences to a terminal that doesn't understand them, you'll end up with a garbled screen.

(*) I even went as far as implementing a subset of curses on top of MS-DOS INT calls back then, so I could also compile the game as a 16-bit .COM DOS executable ;)
 
Use panel(3) in concert with curses(3), if your work involves drawing windows, especially stacked-windows. You needn't use panel(3) to work with simple window, and curses(3) is enough, nevertheless, in my opinion, implementing windows via panel(3) into your curses(3) projects, makes things much easier.

Any easy tutorials?
 
Any easy tutorials?

I was tempted to plug STFW and RTFM, but as it may sound counterintuitive, I dare to say there's neither good '101' tutorial, nor enough interest in programming polychromatic CLI/TUI on the Internet. Quite frankly, it's very similar to perl vs. python situation. You could find a lot of different tutorials about perl, but most of them are old-- that's is not a problem though. There are bunch of Hello-World-ish copy-pasted blogs for ncurses, on the Internet, but I think a good portion of them are all derived from the 2005 TLDP How-To on ncurses. Linux tutorials are fine, but as Zirias has mentioned in the second reply, you also need to consult the termcap(3) and ncurses(3) man-pages.

Another rather unrelated point: when you're linking libraries beyond the C Library 'libc', always keep an eye on intro(3), to find out which option(s) you must add at compile-time, in this case: -lcurses. There's also a ncursesw which handles (configure) wide-characters. Also, in some manuals, you may encounter some references to libtermcap (-ltermcap), libtermlib (-ltermlib), libcurses (-lcurses), libncurses (-lncurses) and libncursesw (-lncursesw), some of which, as far as I know, are hard-link to libcurses. But again, you should always consult the manual page of targeted OS. For example, c.f. references to libtermcap (-ltermcap) in both FreeBSD and OpenBSD's intro(3).
 
Another rather unrelated point: when you're linking libraries beyond the C Library 'libc', always keep an eye on intro(3), to find out which option(s) you must add at compile-time, in this case: -lcurses. There's also a ncursesw which handles (configure) wide-characters. Also, in some manuals, you may encounter some references to libtermcap (-ltermcap), libtermlib (-ltermlib), libcurses (-lcurses), libncurses (-lncurses) and libncursesw (-lncursesw), some of which, as far as I know, are hard-link to libcurses. But again, you should always consult the manual page of targeted OS. For example, c.f. references to libtermcap (-ltermcap) in both FreeBSD and OpenBSD's intro(3).
Thanks for the input and compilation flags. Thankfully there is a youtube video of a 101 tutorial on ncurses with many types of topics:

View: https://www.youtube.com/watch?v=pjT5wq11ZSE
 
  • Thanks
Reactions: a6h
Alright, so I did tutorial video#4. It seems that I can not change colors and stuck with the regular limited ANSI colors. The terminal does not support ncurses color change.
Do I need to use a different terminal software or shell software? Is this more of a shell thing or terminal thing or both?

How can I get 256 color support on headless freeBSD machine?

The shell I am using: echo "$SHELL" shows:
/bin/csh

For terminal I have no idea.

Thanks.
 
echo $TERM

setenv TERM xterm-256color
then
Code:
 echo 'for i in $(jot 255);do tput AB $i && echo -n " ";[ $(($i%32)) -eq 0 ] && tput AB 0 &&  echo;done;tput AB 0;echo' | sh
 
echo $TERM

setenv TERM xterm-256color
then
Code:
 echo 'for i in $(jot 255);do tput AB $i && echo -n " ";[ $(($i%32)) -eq 0 ] && tput AB 0 &&  echo;done;tput AB 0;echo' | sh

Thank You for the code and reply.

I seem to get same results but when I type tput color I get a return of value 80:

freeBSD.jpg


So it seems that I am still locked with 80 colors and not 256 colors.
 
I have an overkill graphics card "AMD Radeon PRO WX 2100".
What are some ways how I can get a headless machine to do 256 colors on terminal or console? What are some methods to do so?

It seems I have no choice but to install x-sever desktop environment and use "xterm" terminal app.

Thanks.
 
Alright, so I did tutorial video#4. It seems that I can not change colors and stuck with the regular limited ANSI colors. The terminal does not support ncurses color change.
Do I need to use a different terminal software or shell software? Is this more of a shell thing or terminal thing or both?

How can I get 256 color support on headless freeBSD machine?
If the intended output from the headless FreeBSD system goes to a system with the "AMD Radeon PRO WX 2100" you should be able to get your 256 color support. If am seeing things right, you're using obs-cli as your remote terminal emulator; am I correct? There may be a setting when you start obs-cli to specify a terminal setting that supports 256 colors (I couldn't find one at first hand). If obs-cli doesn't work out then you should consider another terminal emulator (perhaps tmux?) that does support 256 colors; "[..] so the terminal could be anything" as covacat wrote.
 
Back
Top