Skip to content

Instantly share code, notes, and snippets.

@r0oland
Created October 7, 2022 17:26
Show Gist options
  • Save r0oland/e9b6e5ea285f60cd822801bd25619b6c to your computer and use it in GitHub Desktop.
Save r0oland/e9b6e5ea285f60cd822801bd25619b6c to your computer and use it in GitHub Desktop.
Practical ANSI Color Codes
# simple effects
$BOLD = "`e[1m"
$UNDERLINED = "`e[4m"
# simple colors
$RED = "`e[31m"
$GREEN = "`e[32m"
# RGB colors
$METER_BLUE = "`e[38;2;34;98;147m" # 0x22 0x62 0x93 == 34 98 147
$GREEN = "`e[38;2;111;182;57m" # 0x6F 0xB6 0x39 == 111 182 57
# reset colors
$RESET = "`e[0m"
# use as:
write-host "$($ORANGE)This would be an orange and bold message!$($RESET)"
# ------------------------------------------------------------------------------
def color_message(str, color = 'red', effects=['bold'], bgColor = ''):
'''Short warning...it makes a short warning. Really.'''
# create dictionary with all colors and corresponding escape codes
COLORS = {
'black': "\x1b[30m",
'red': "\x1b[31m",
'green': "\x1b[32m",
'yellow': "\x1b[33m",
'blue': "\x1b[34m",
'magenta': "\x1b[35m",
'cyan': "\x1b[36m",
'white': "\x1b[37m",
'meterblue': "\x1b[38;5;4m",
'grey': "\x1b[38;5;8m",
'gray': "\x1b[38;5;8m",
'dark_red': "\x1b[38;5;1m",
'purple': "\x1b[38;5;5m",
'orange': "\x1b[38;5;202m",
'': ""
}
# see https://en.wikipedia.org/wiki/ANSI_escape_code for even more colors
# create dict with background colors
BG_COLORS = {
"black": "\x1b[40m",
"red": "\x1b[41m",
"green": "\x1b[42m",
"yellow": "\x1b[43m",
"blue": "\x1b[44m",
"magenta": "\x1b[45m",
"cyan": "\x1b[46m",
"white": "\x1b[47m",
"": ""
}
EFFECTS = {
"bold": "\x1b[1m",
"underscore": "\x1b[4m",
"reverse": "\x1b[7m",
"hidden": "\x1b[8m",
"": ""
}
RESET = "\x1b[0m"
allEffects = ''
for effect in effects:
# print(effect)
allEffects = allEffects + EFFECTS[effect]
formatString = allEffects + BG_COLORS[bgColor] + COLORS[color]
print(formatString + "{:s}".format(str) + RESET, end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment