3

My wish is to make every character or line or whatever you think might look best for an ASCII to look. Basically I have tried colorama and it was only based on one color. What is the best method?

What I have is

print("""


   _____ _             _                        __ _
  / ____| |           | |                      / _| |
 | (___ | |_ __ _  ___| | _______   _____ _ __| |_| | _____      __
  \___ \| __/ _` |/ __| |/ / _ \ \ / / _ \ '__|  _| |/ _ \ \ /\ / /
  ____) | || (_| | (__|   < (_) \ V /  __/ |  | | | | (_) \ V  V /
 |_____/ \__\__,_|\___|_|\_\___/ \_/ \___|_|  |_| |_|\___/ \_/\_/




    """)

and that is pretty much it. Let me know your thoughts through this!

martineau
  • 119,623
  • 25
  • 170
  • 301
WeInThis
  • 537
  • 2
  • 8
  • 22

1 Answers1

10

The valid foreground colors provided by colorama are variables on colorama.Fore. We can retrieve them using vars(colorama.Fore).values(). We can random select a foreground color by using random.choice, feeding it the foreground colors as obtained by vars.

Then we simply apply a randomly-chosen color to each character:

text = """   


   _____ _             _                        __ _               
  / ____| |           | |                      / _| |              
 | (___ | |_ __ _  ___| | _______   _____ _ __| |_| | _____      __
  \___ \| __/ _` |/ __| |/ / _ \ \ / / _ \ '__|  _| |/ _ \ \ /\ / /
  ____) | || (_| | (__|   < (_) \ V /  __/ |  | | | | (_) \ V  V / 
 |_____/ \__\__,_|\___|_|\_\___/ \_/ \___|_|  |_| |_|\___/ \_/\_/  




    """

import colorama
import random

colors = list(vars(colorama.Fore).values())
colored_chars = [random.choice(colors) + char for char in text]

print(''.join(colored_chars))

This will print every character in a different color:

color characters

If you want colored lines instead, it's a simple change:

colored_lines = [random.choice(colors) + line for line in text.split('\n')]
print('\n'.join(colored_lines))

enter image description here

You can tailor the list of colors to your needs. For instance, if you want to remove colors which may be similar to your terminal background (black, white, etc.) you can write:

bad_colors = ['BLACK', 'WHITE', 'LIGHTBLACK_EX', 'RESET']
codes = vars(colorama.Fore)
colors = [codes[color] for color in codes if color not in bad_colors]
colored_chars = [random.choice(colors) + char for char in text]

print(''.join(colored_chars))

Which gives:

enter image description here

jme
  • 19,895
  • 6
  • 41
  • 39
  • Straight beautiful! Thank you very much! – WeInThis Sep 26 '17 at 11:58
  • I do wish I could removed just the black color because of the CMD but I think it will be fine :) If it is easy enough to remove the black one only. Would be awesome! – WeInThis Sep 26 '17 at 12:01
  • Oh. I do have the color for black now but how would that be possible to remove it in that case from the value? Is there a possible code for just removing instead of changing the whole line code? :) – WeInThis Sep 26 '17 at 12:11
  • 1
    Any set of colors can be used by modifying `colors`; see the edited answer. – jme Sep 26 '17 at 12:11
  • Oh. Just when I entered the comment, I saw a edited 33 secs ago. Will check it @jme! I really do appreciate this really! – WeInThis Sep 26 '17 at 12:11