rene-d / colors.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# 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 )) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#! /bin/bash |
# rene-d 2015 |
# définition de constantes SGR pour affichage de couleur |
# exemple: echo -e $rouge$ |
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 $ |
done |
fi |
Colors with Python
Color is a big component of visuals. It also caters our visual cortex which evolved after millions of years to process colors at conscious and subconscious levels.
Whether you are a web designer, scientist, consultant, financier, student or researcher colors you use make a huge impact in the outcomes of your work.
We created this color manipulation guide that goes through the usage of colormaps and hex color collections in detail.
You can add _r to any builtin colormap to get the reversed colormap as above.
1- cmap, color, color_palette
We will use a few very useful Python functions and parameters in this tutorial. Ahead of the examples let’s take a quick look at a few of them.
cmap: This parameter is available in some Python charting functions. It takes a Python Colormap and applies it to the whole graph which can be very convenient for quick and beautiful coloring results.
color: This parameter is used when a few items or a single item needs to be colored such as a title, axis label, text label, bar or scatter point. Some objects might require the usage of colors parameter instead.
color_palette: This is a great function from seaborn library. It can be used to create color palettes and individual colors from Python Colormaps. We will make examples below.
2- Color vs Colormap
Colormaps are collections of colors that are used by certain Python objects such as plotting functions to distribute a colormap to the values in the whole plot.
Individual colors are useful when coloring individual objects such as, title of a chart, a single trendline, points on a scatter plot (such as centroids), axes labels etc. Usually passed to color or colors parameters.
We have fantastic builtin colormaps that are available in matplotlib and seaborn and we can also derive specific individual colors from those colormaps. Colormaps are utilized via cmap parameter.
In this tutorial we will show a few color operations that you can use to step up your Python color game and make the really beautiful visual touch that’s needed to your design objects, Python charts and animations.
import seaborn as sns from PIL import Image, ImageDraw iter = 15 palette = list(reversed(sns.color_palette("Spectral_r", iter).as_hex())) print(palette) width_px=1000 new = Image.new(mode="RGB", size=(width_px,120)) for i in range(iter): newt = Image.new(mode="RGB", size=(width_px//iter,100), color=palette[i]) new.paste(newt, (i*width_px//iter,10))
['#be254a', '#dc484c', '#ef6645', '#f88c51', '#fdb365', '#fed27f', '#feeb9d', '#fffebe', '#f0f9a7', '#d8ef9b', '#b3e0a2', '#89d0a4', '#60bba8', '#3f97b7', '#4273b3']