How to print RGB colour to the terminal
Question:
Can ANSI escape code SGR 38 – Set foreground color with argument 2;r;g;b be used with print function?
Example of use with code 33 is of course
OKBLUE = ' 33[94m'
I would like to use 038 instead to be able to use any RGB color. Is that posible?
I tried
GREEN = ' 38[2;0;153;0m'
ENDC = ' 33[0m'
print(f"{GREEN} some text {ENDC}")
Expected to change the color of "some text" in green
Answers:
Below code will give you an idea.
print(' 33[90m' + 'hello' + ' 33[96m' + ' there?' )
To use an RGB color space within the terminal* the following escape sequence can be used:
# Print Hello! in lime green text.
print(' 33[38;2;146;255;12mHello! 33[0m')
# ^
# |
# The 38 goes here, to indicate a foreground colour.
# Print Hello! in white text on a fuschia background.
print(' 33[48;2;246;45;112mHello! 33[0m')
Explanation:
Can ANSI escape code SGR 38 – Set foreground color with argument 2;r;g;b be used with print function?
Example of use with code 33 is of course
OKBLUE = ' 33[94m'
I would like to use 038 instead to be able to use any RGB color. Is that posible?
I tried
GREEN = ' 38[2;0;153;0m'
ENDC = ' 33[0m'
print(f"{GREEN} some text {ENDC}")
Expected to change the color of "some text" in green
Below code will give you an idea.
print(' 33[90m' + 'hello' + ' 33[96m' + ' there?' )
To use an RGB color space within the terminal* the following escape sequence can be used:
# Print Hello! in lime green text.
print(' 33[38;2;146;255;12mHello! 33[0m')
# ^
# |
# The 38 goes here, to indicate a foreground colour.
# Print Hello! in white text on a fuschia background.
print(' 33[48;2;246;45;112mHello! 33[0m')
Explanation: