Recent

Author Topic: Text Colors in Linux Console/Terminal  (Read 3040 times)

CVAlkan

  • New member
  • *
  • Posts: 9
Text Colors in Linux Console/Terminal
« on: March 02, 2018, 10:36:03 pm »
Hi:

I have a text mode Linux app that is run from a linux console/terminal on a few different machines. Unfortunately these machines have different colors set in their terminal profiles. So if, for instance, I wish to use multiple colors (e.g. Red for important warnings, or Bold for titles), I think I need to first detect what the terminal's default foreground and background colors are (so I can return to them as needed). Setting the terminal colors and such from within the app at the beginning is of course possible, but not an option in this case.

I've looked through the crt unit, but can't find anything that will do that. Does anyone have any ideas? Do I need to write a snippet of assembly language to do that?

Thanks for any advice.

guest60499

  • Guest
Re: Text Colors in Linux Console/Terminal
« Reply #1 on: March 03, 2018, 06:44:27 am »
The problem in this case is that the terminals have not been set up properly: an entire colorscheme should be set, which explicitly calls out a foreground and background color, and sets the other available colors so that they are pleasant to look at.

If you do anything else the issue you are experiencing is unavoidable. The best compromise is to have a flag that disables all colored output. This will be useful if the output of your program must be interpreted by other programs, or copied from the terminal.

See this link for a demonstration. There is a specific color that is automatically set as foreground or background, like so:
Code: [Select]
! special
*.foreground:   #c5c8c6
*.background:   #1d1f21
*.cursorColor:  #c5c8c6

! black
*.color0:       #282a2e
*.color8:       #373b41

! red
*.color1:       #a54242
*.color9:       #cc6666

! green
*.color2:       #8c9440
*.color10:      #b5bd68

! yellow
*.color3:       #de935f
*.color11:      #f0c674

! blue
*.color4:       #5f819d
*.color12:      #81a2be

! magenta
*.color5:       #85678f
*.color13:      #b294bb

! cyan
*.color6:       #5e8d87
*.color14:      #8abeb7

! white
*.color7:       #707880
*.color15:      #c5c8c6

If you want the precise value of the foreground and background you can parse .Xdefaults or .Xresources, but you must rely on the predefined colors to be portable. You can also use 256-color sequences (terminal-colors demo) which allow you to pick colors regardless of the color scheme, but it is a good idea to avoid this.

CVAlkan

  • New member
  • *
  • Posts: 9
Re: Text Colors in Linux Console/Terminal
« Reply #2 on: March 03, 2018, 05:57:38 pm »
Thanks very much for the response, but I'm not sure what you mean by "the terminals have not been set up properly."

On the machine I'm on at the moment (Ubuntu 16.04), I'm using Gnome Terminal 3.18.3, and the colors are set by selecting existing "Profiles" (or creating new ones) and, in each profile, there is a full set of 16 colors defined. Whether these colors are "all pleasant to look at" is, of course, subjective ('de gustibus non disputandum' as Horace said).

Part of my searching around was attempting to find a way to read these profile values, but I didn't find anything relevant; even so, I doubt if it would be usable in any cross-platform context. I did a locate on .Xdefaults and .Xresources as root and neither is present on this machine.

Life in the twenty-first century is so hectic; I guess I'll just drop back to the 1960s and output everything on paper tape, although now that I think about it, we had several different colors of paper tape available; if there was a reason for that, I can't recall.

But, again, thanks for responding...

 

TinyPortal © 2005-2018