How to print text with multiple different types of colors in he same line in Python 3? - python-3.x

I am trying to do some visualizing data on the terminal and I am doing lots of printing to do that. The issue I am having is that certain character symbols look the same. I figured that coloring them differently would help me see the differences. I see from this link that there is a way to do it, but I don't understand what I am looking at. There is no explanation for what is going on in those solutions.
How do you specify, in the same line, text with different colors?
I should also mention that I am building the printable string OUTSIDE of the call to print(). How do you build the colorful string outside of the print() call?
A rewritten form of my question:
1. Colorize the text I print to the string with multiple types of colors.
2. Colorize the string BEFORE it gets sent to the "print()" call.
A couple examples would be great.

Using colorama just like the answer you linked is doing:
from colorama import Fore, Style
my_str = f"{Fore.BLUE}Hello, {Style.RESET_ALL} guys. {Fore.RED} I should be red."
print(my_str)
This gives me:
As you can see Fore.<color name> changes the color of the text after it, until the Style.RESET_ALL. After that you can change the color of the text again.

There could be multiple ways to achieve this. One which doesn't require any extra packages is to use ANSI color codes. Look at this link. Below are some examples.
s = "\033[1;32;40m Bright Green on black \033[1;31;43m Red on yellow \033[1;34;42m Blue on green \033[1;37;40m"
print(s)
Here in first code \033[1;32;40m, \033[ is the escape code followed by 1 for bold, 32 for bright green text and 40 for black background. The 3 codes are separated by ; and ended with m. Adding all the 3 codes (1, 32 and 40 here) isn't mandatory though.
output:
Other ways to achieve this can be found here.

Related

ANSI Colors not showing where they are supposed to show

I would like for the entire text that pops up when you use the help() function to be affected by the color code that I used, but instead when I run the script the only part that is affected by said color code is a "None" text below the help() result
print(f'\033[34m {help("for")} \033[m')
This is the single line of code and how it looks in the terminal
The (pretty simple) workaround that I found is to just print the color codes on separate lines
Example:
print('\033[41m')
print(help('for'))
print('\033[m')
That code will make it so the output of the help('for') function has a red background

Python Matplotlib string-formatting in plots with bold and Italic font

I have a little problem with bold and italic fonts in a plot at the same time.
I tried:
plt.text(0.5,0.5,r'\it{italic} + \bf{bold}' = \it{\bf{both}}')
this gives: italic and bold = both
and with:
plt.text(0.5,0.5,r'\it{italic} + \bf{bold}' = \bf{\it{both}}')
I get: italic and bold = both
But what I want to get is both
I also tried to use the latex version with \textbf{...} and \textit{...} and the commands \mathit{...} and \mathbf{...}.
They work as they should but not if they are used combined. The combination of different "technologies" like \mathbf{\textbf{...}} does not help. It is alway only the last style that stays.
I saw a few different questions on that topic but no correct answer, so I hope that asking espacially for this contextless case helps to find an answer. I am looking for a generall solution, not for a workaround for a special case, e.g. using a special font that already looks bold.
I have found the answer. Yoi have to use a different font. The work around I mentioned is the answer. If you use:
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
you will get the desired result. The default font in matplotlib seems to not support the bold and italic edits at the same time. Maybe this helps in many other situations you will have with other edits in case you are looking for different combinations.

wxpython textctrl change caret colour

I'm using a wxpython textctrl & would like to hide the caret. After a lot of searching it would appear that the best approach would be to simply change it's colour to white. However I can not work out how to do this.
I found the following info:
SetCaretForeground(fore)
Sets the foreground color of the caret. The parameter fore is a wxColour object, a #RRGGBB string, or a color spec like "white". Returns None.
from here: http://www.yellowbrain.com/stc/caret.html#setfg
The code for my current textctrl is below. Any help would be appreciated.
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL)
Clinton.
You are using a wx.TextCtrl and the link you give is for a wx.StyledTextCtrl. These are completely different animals, and the wx.StyledTextCtrl has a lot more functionality than the simple wx.TextCtrl.
I don't think there's a way to set the caret color in an ordinary wx.TextCtrl.

Is there a wx Widget to show ANSI escape code colored text?

I can't seem to find a control that will display text colored with ANSI escape codes. For example:
"\e[0;32mHello \e[0;37mWorld"
Would display a green "Hello" and a white "World".
Does one exist?
I don't think that such a widget exists. You may look for some way of changing ANSI codes into HTML and then displaying it in wxWidgets. This may be useful:
http://code.activestate.com/recipes/577349-convert-a-transcript-with-ansi-escape-sequences-to/

Colour Name to RGB/Hex/HSL/HSV etc

I have come across this great function/command. Colour to RGB, you can do this:
col2rgb("peachpuff")
//returns hex
It will return one hex value. I want to extend this using Perl, Python or PHP but I want to be able to pass in, for example, "yellow" and the function returns all types of yellows - their hex/rgb/?/etc value.
I already have a quick solution implemented, which involves mapping colour names to hex values but now I want to get more precise and use some formulas etc to determine what's what.
However, as usual, I don't have a clue on how to do this! So I appreciate any implementation advice on how to do this.
Thanks all
The canonical CSS color names originated in X11 and the intersection of the sets - along with their RGB values can be - found at Wikipedia.
A more easily parsed list can be found in various rgb.txt files scattered over the web, but these are likely the X11 set not the CSS set.
added: Given an RGB value you can compute nearby colors by HSL conversion. Color palettes - sets of colors that go well together - are an art not a science, Google 'em.
Why don't you use the ccs Color names.
W3c webstandard --> CSS3/SVG Farben

Resources