1

Need to print colored block in terminal. I wrote this:

from termcolor import colored
...
print(colored('\u2588\u2588\', '#%s' %(color)))

Where color is color in hex, seems like termcolor can't work with hex
Also tried module colored:

import colored
...
color_t = fg('#'+color)

but Python raises KeyError: '#4e1747' (random color)
What's wrong?

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Ican
  • 119
  • 2
  • 2
  • 10
  • 3
    Have You even bothered to read manual for any of those libraries? First one supports 8 colors, while second supports 256 colors. – Fejs Dec 04 '16 at 10:02
  • Possible duplicate of [How can I color Python logging output?](http://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output) – Blag Dec 04 '16 at 13:15

3 Answers3

0

Have a look at the Colorama package, it provides cross-platform coloring of terminal output and is very easy to use.

MatanRubin
  • 985
  • 8
  • 16
0

This will print the hex string in Bright Green. This is a fun color ANSI escape "\033["

import sys
print("\033[1;32;40m '\u2588\u2588\\' \n")

Good sources: http://www.jwrr.com/content/Console-Escape-Sequences/ http://ozzmaker.com/add-colour-to-text-in-python/

Freshnuts
  • 26
  • 4
0

OK, I found answer

message = 'some string'
hex_color = '#4e1747'
r, g, b = [int(hex_color[i:i+2], 16) for i in range(1, len(hex_color), 2)]
print("\x1b[38;2;{r};{g};{b}m{message}\x1b)
Ican
  • 119
  • 2
  • 2
  • 10