79188487

Date: 2024-11-14 11:14:07
Score: 2.5
Natty:
Report link

I use a custom function that wraps the text with a desired RGB(R=red, G=green, B=blue) color value in ANSI escape sequences. Here's what it looks like:

from random import randint

RANDOM_RGB_COLOR = {
"r": randint(0, 255),
"g": randint(0, 255),
"b": randint(0, 255)
}

def color(text: str, r: int = 256, g: int = 256, b: int = 256) ->  str:
    if (r > 255 or g > 255 or g > 255) or (r < 0 or g < 0 or b < 0):
    return f"\033[38;2;{RANDOM_RGB_COLOR['r']};{RANDOM_RGB_COLOR['g']};{RANDOM_RGB_COLOR['b']}m{text}\033[38;2;255;255;255m"
return f"\033[38;2;{r};{g};{b}m{text}\033[38;2;255;255;255m"

Here are some similar solutions: How do I print colored text to the terminal?

Reasons:
  • Blacklisted phrase (1): How do I
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (0.5):
Posted by: Staccato