Skip to content

Instantly share code, notes, and snippets.

@GammaGames
Last active December 21, 2021 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GammaGames/720af145380a2ac608299745b4b4a8fc to your computer and use it in GitHub Desktop.
Save GammaGames/720af145380a2ac608299745b4b4a8fc to your computer and use it in GitHub Desktop.
Create colored text to copy/paste into other applications using python
import click
import webbrowser
import os
import random
import tempfile
from time import sleep
random.seed()
tmp = tempfile.NamedTemporaryFile()
@click.command()
@click.argument("text")
@click.option("--colors", default="crimson,chartreuse")
@click.option("--pattern", type=click.Choice(["loop", "random"]), default="loop")
@click.option("--split", type=click.Choice(["character", "word"]), default="character")
def color(text, colors, pattern, split):
filename = f"{tmp.name}.html"
with open(filename, "w") as out_file:
cols = colors.split(",")
chs = text if split == "character" else text.split()
for ch in chs:
if pattern == "loop":
col = cols[0]
else:
col = random.choice(cols)
out_file.write(f"<span style='color: {col}'>{ch}</span>")
if split == "word":
out_file.write(" ")
if ch != " " and pattern == "loop":
cols.append(cols.pop(0))
webbrowser.open(f"file://{os.path.realpath(filename)}")
if __name__ == "__main__":
color()
@GammaGames
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment