Skip to content

Instantly share code, notes, and snippets.

@rene-d
Last active October 30, 2024 22:43
Show Gist options
  • Save rene-d/9e584a7dd2935d0f461904b9f2950007 to your computer and use it in GitHub Desktop.
Save rene-d/9e584a7dd2935d0f461904b9f2950007 to your computer and use it in GitHub Desktop.
ANSI color codes in Python
# SGR color constants
# rene-d 2018
class Colors:
""" ANSI color codes """
BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
BROWN = "\033[0;33m"
BLUE = "\033[0;34m"
PURPLE = "\033[0;35m"
CYAN = "\033[0;36m"
LIGHT_GRAY = "\033[0;37m"
DARK_GRAY = "\033[1;30m"
LIGHT_RED = "\033[1;31m"
LIGHT_GREEN = "\033[1;32m"
YELLOW = "\033[1;33m"
LIGHT_BLUE = "\033[1;34m"
LIGHT_PURPLE = "\033[1;35m"
LIGHT_CYAN = "\033[1;36m"
LIGHT_WHITE = "\033[1;37m"
BOLD = "\033[1m"
FAINT = "\033[2m"
ITALIC = "\033[3m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
NEGATIVE = "\033[7m"
CROSSED = "\033[9m"
END = "\033[0m"
# cancel SGR codes if we don't write to a terminal
if not __import__("sys").stdout.isatty():
for _ in dir():
if isinstance(_, str) and _[0] != "_":
locals()[_] = ""
else:
# set Windows console in VT mode
if __import__("platform").system() == "Windows":
kernel32 = __import__("ctypes").windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
del kernel32
if __name__ == '__main__':
for i in dir(Colors):
if i[0:1] != "_" and i != "END":
print("{:>16} {}".format(i, getattr(Colors, i) + i + Colors.END))
#! /bin/bash
# rene-d 2015
# définition de constantes SGR pour affichage de couleur
# exemple: echo -e ${COLOR_RED}rouge${COLOR_END}
if ! tty -s; then
# ne pas changer l'indentation des lignes suivantes
COLOR_BLACK=""
COLOR_RED=""
COLOR_GREEN=""
COLOR_BROWN=""
COLOR_BLUE=""
COLOR_PURPLE=""
COLOR_CYAN=""
COLOR_LIGHT_GRAY=""
COLOR_DARK_GRAY=""
COLOR_LIGHT_RED=""
COLOR_LIGHT_GREEN=""
COLOR_YELLOW=""
COLOR_LIGHT_BLUE=""
COLOR_LIGHT_PURPLE=""
COLOR_LIGHT_CYAN=""
COLOR_LIGHT_WHITE=""
COLOR_BOLD=""
COLOR_FAINT=""
COLOR_ITALIC=""
COLOR_UNDERLINE=""
COLOR_BLINK=""
COLOR_NEGATIVE=""
COLOR_CROSSED=""
COLOR_END=""
else
# ne pas indenter les lignes suivantes (pour le sed suivant)
COLOR_BLACK="\033[0;30m"
COLOR_RED="\033[0;31m"
COLOR_GREEN="\033[0;32m"
COLOR_BROWN="\033[0;33m"
COLOR_BLUE="\033[0;34m"
COLOR_PURPLE="\033[0;35m"
COLOR_CYAN="\033[0;36m"
COLOR_LIGHT_GRAY="\033[0;37m"
COLOR_DARK_GRAY="\033[1;30m"
COLOR_LIGHT_RED="\033[1;31m"
COLOR_LIGHT_GREEN="\033[1;32m"
COLOR_YELLOW="\033[1;33m"
COLOR_LIGHT_BLUE="\033[1;34m"
COLOR_LIGHT_PURPLE="\033[1;35m"
COLOR_LIGHT_CYAN="\033[1;36m"
COLOR_LIGHT_WHITE="\033[1;37m"
COLOR_BOLD="\033[1m"
COLOR_FAINT="\033[2m"
COLOR_ITALIC="\033[3m"
COLOR_UNDERLINE="\033[4m"
COLOR_BLINK="\033[5m"
COLOR_NEGATIVE="\033[7m"
COLOR_CROSSED="\033[9m"
# ne pas changer l'indentation des lignes suivantes
COLOR_END="\033[0m"
fi
# affiche les couleurs
if [ "$1" = "-h" ] ; then
for i in `cat $0 | sed -e 's/^\(COLOR.*\)=\(.*\)/\1/p;d'`; do
[ "$i" != "COLOR_END" ] && echo -e ${!i}$i${COLOR_END}
done
fi
@harshmavani54372
Copy link

please get code orange ANSI Code in Python

@rene-d
Copy link
Author

rene-d commented May 15, 2021

I recommend you to use such a script to find the color code you want: https://gist.github.com/b5370efb61db005d7de0ad47959cd46c

Depends on your terminal, you can also use RGB : https://en.wikipedia.org/wiki/ANSI_escape_code

@texsavi
Copy link

texsavi commented Nov 29, 2022

Cool thanks for these

@Kappei0083
Copy link

I came from 100DaysOfCode. Thank you.

@GrvExplorer
Copy link

I also came from 100days of code.

@latatan
Copy link

latatan commented Feb 12, 2023

That's what I need, Lol.

Thanks !

@MyCool-DJ
Copy link

same 🙌

@NicusPulcis
Copy link

what's blink?

@Atul-kumar11
Copy link

i also come for 100days of code on replit

@honeyvagueness
Copy link

100 days of code.
Muchas gracias.

@Satanaelcode
Copy link

great small little cheatsheet thx brother!

@amdk80
Copy link

amdk80 commented May 19, 2023

I did show up from 100days of code on Replit

@flowerMa
Copy link

gracias! 100 days of code

@jackforevergreen
Copy link

The green is my fav

@LowLevelCodingCH
Copy link

The green is my fav

@Lucas01-00
Copy link

Lucas01-00 commented Apr 11, 2024

  • m

@drie3hoek
Copy link

Hello Rene,
I would like to comment on some issues from colors.py but don' t see how to contact you personally.
Hope to hear from you,
Piet from NL

@rene-d
Copy link
Author

rene-d commented Jun 18, 2024

These comments are the best way to comment on issues.

@zubairxshah
Copy link

Merci pour les codes couleurs.

@mbaronif
Copy link

For simply white: "\033[0m"

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