There was a function in C to adjust background color? (It was actually a Dos Command) - c

I am looking for the system function to adjust background color. It was like
system("color",somecolorcodes);
Does anyone know about it?
On Windows Xp or 7!

It's "color XX" where the first X is the background and the second X is the foreground.
The codes are as following:
0 = Black 8 = Gray
1 = Blue 9 = Light Blue
2 = Green A = Light Green
3 = Aqua B = Light Aqua
4 = Red C = Light Red
5 = Purple D = Light Purple
6 = Yellow E = Light Yellow
7 = White F = Bright White
So basically for black text on white ground, you do
system("color 70");
Windows only, tho.

system("cls"); //clears the screen
system("color F0"); //Creates Bright White Background with black text
system("type struct3.c struct2.c"); /*prints the file struct3 and struct2 in the
console*/
system() is a really useful function included in Windows.h library. Apparently we can do many other tasks with this function so I was searching for it when I came across this thread.
Edit:While looking at the commands in command prompt I realized that the above examples are commands in the command prompt and tried using other commands like time, help, del etc in the system() function and figured all the commands that we use in command prompt can be used by System() function.
For that we write the commands in the System() function like below
system("command");
Even though C is case sensitive the command inside system() is not case sensitive like in command prompt.

You can use the SetConsoleTextAttribute function in Windows. This will let you output text in different colors at the same time, while calling "color" doesn't.
There are also others that are less coarse -- search for color in this listing of console functions.
Also, if you're looking for a cross-platform approach take a look, for instance, at this file from Musepack.

A somewhat more portable way of doing this (not DOS or Windows specific) is:
printf("\033[%dm", 40 + color); /* set background color */
The corresponding way to set the foreground color is:
printf("\033[%dm", 30 + color); /* set foreground color */
These work with colors:
0 black
1 red
2 green
3 yellow/brown
4 blue
5 magenta
6 cyan
7 white
These aren't truly portable, either; they work wherever "ANSI control sequences" are implemented in your terminal, terminal emulator, or console.
Under Linux and/or xterm, prefixes 90 (for foreground) and 100 (for background) seem to work, also, perhaps with a slightly different set of colors.

Related

Terminal background color not always properly reset using "\033[0m"

Trying to understand terminal behavior when it comes to reset background color on a Unix-like OS (Mac, Linux).
Consider a bash script
#!/usr/bin/env bash
printf "\033[46m"
printf "On Cyan\n"
printf "\033[0m"
printf "Back to Normal\n"
When I initially run this script, everything is as expected. However, if I repeat it for several times, the printing result changes. Following "Back to Normal" is a line on cyan background color.
The screenshot (Terminal, Mac OS Mojave):
Questions:
Why is this? Can anyone explain this behavior?
If I have changed background color and have printed out a line (ending with newline), what can I do to properly reset the background and to avoid this unwanted trailing background color?
For your information, I have tested this behavior on Mac OS Mojave Terminal, and on Ubuntu 18.04 Terminal. And I have tested using an equivalent Python 3 script. Results are consistent. I also tried using fflush(stdout) in C/C++ but in vain.
P.S. This question stems from a C++ program to be run on Mac or Linux. I thought it is irrelevant to the language itself, so I simplified it down to a bash script. If possible, please suggest a solution that can be done in C/C++.
Your issue is caused by the terminal scrolling.
When your "On Cyan" cyan background issue a newline that causes the terminal to scroll, the background of the inserted blank line is filled with the current known background: cyan.
Then you reset the color attribute and your "Back to normal" text prints with the default background, but the area of the line that has not been overwritten, is still cyan.
You should reset the attributes before reaching the end of the line like this:
#!/usr/bin/env sh
printf "\033[46m"
printf "On Cyan"
printf "\033[0m"
printf "\nBack to Normal\n"
Alternatively you could issue a clear to end of line tput el or printf "\033[K" after resetting the text attributes:
#!/usr/bin/env sh
printf "\033[46m" # same as tput setab 6
printf "On Cyan\n"
printf "\033[0m\033[K" # same as tput -S <<<$'sgr0\nel'
printf "Back to Normal\n"

SDL specified window size and position

So my homework is to make Ludo in C on VS2017 with SDL1.2. The way it works is that there is a window with the board on it, initialised this way:
SDL_Surface *screen = SDL_SetVideoMode(751, 751, 0, SDL_ANYFORMAT);
The other window is the command prompt (it's a console application), in which all inputs go, using scanf. My probem is that the two windows, although somewhat randomly, always pop up overlapping each other. Is there any way for me to specify the windows' sizes and initial positions on the screen, just to make it look nicer? (The SDL window already has a fixed size of course.)
Thanks!
Hi there,
You could position the cmd prompt by doing the following:
windows -> type "CMD" -> right click, open file location -> right click again, properties and then choose the layout menu.
Uncheck the 'Let system position window' and you're good to go.

LSCOLORS actual color values

I find that the default colors I get with apps like gnome-terminal are often nearly unreadable. In particular, the bright green for executables is unreadle with a white background.
I can use LSCOLORS to remap executable to be red instead of bright green (or whatever), but what I'd rather do is to make the bright green a shade that's not quite so close to white.
Is there a file somewhere that maps the color numbers in LSCOLORS to RGB values?

How to set terminal background color on linux terminal without using ncurses?

I've wrote a simple console program in C which uses ANSI escape codes to color its text.
Is there a way to temporarily set the background of the whole terminal to black and the default font color to light gray? Can this be reverted after the program ends?
I'd prefer to avoid using ncurses.
Probably the simplest way to go is to set the background colour of your text with ANSI:
For instance using:
echo -e "\e[37m\e[41m"
will give you blue text on a red background (you can use this to test the effect in dramatic, easy to see colours).
Whereas
echo -e "\e[97m\e[40m"
will set the foreground to white and the background to black for the duration of your program.
If you find that you're getting a kind of ugly transition zone between your background colour and the terminal's just print a sufficient number of newlines to wipe the whole screen.
To use this in C, you'll obviously want printf instead of echo.
The wiki page on ANSI escape codes has additional information.
How to do this depends on the terminal the user is using. It may be ANSI, it may be VT100, it might be a line printer. ncurses abstracts this horror for you. It uses a database of information about how to talk to different kinds of terminal (see the contents of $TERM to see which one you are currently using) normally stored in /lib/terminfo or /usr/share/terminfo.
Once you look in those files, you'll probably want to reconsider not using ncurses, unless you have specific requirements to avoid it (embedded system with not enough storage, etc.)

How to print colored spaces and close the color code right after A?

Could anyone tell me how to print colored spaces using C on an xterm?
All I could end up printing was colored letters, but no matter how i try, i cant get it to print a colored space.
id_print_str("\033[22;31m A")
I cant get the above thing to print a color if the A was to be replaced by a space. yet I dont know how to close it in order not to change the whole xterm color.
How about this:
/* Print the red block. */
printf("\033[;41m \n");
/* Reset terminal. */
printf("\033[0;m");
There's a nice and short linuxgazette article about this: The mysterious ^[[ characters.
Perhaps you might use the ncurses library. For a single colored space it could be overkill, but if you want to make a complex terminal application it should be the right tool.

Resources