Color Python output given #RRGGBB hex value -


i've got color user in #rrggbb hex format (#d3d3d3 light grey) , i'd color output it. imagine write print("some text", color="#0000ff") , blue colored output. possible (ideally without 3rd party software)?

you can use following escape sequences on true-color aware terminal:

  • esc[ … 38;2;<r>;<g>;<b> … m select rgb foreground color
  • esc[ … 48;2;<r>;<g>;<b> … m select rgb background color

thus can write function:

reset = '\033[0m' def get_color_escape(r, g, b, background=false):     return '\033[{};2;{};{};{}m'.format(48 if background else 38, r, g, b) 

and use like:

print(get_color_escape(255, 128, 0)        + get_color_escape(80, 30, 60, true)       + 'fancy colors!'        + reset) 

you can doubled horizontal or vertical resolution using example , both background , foreground colour.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -