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 |
List of named colors#
First we define a helper function for making a table of colors, then we use it on some common color categories.
import math from matplotlib.patches import Rectangle import matplotlib.pyplot as plt import matplotlib.colors as mcolors def plot_colortable(colors, *, ncols=4, sort_colors=True): cell_width = 212 cell_height = 22 swatch_width = 48 margin = 12 # Sort colors by hue, saturation, value and name. if sort_colors is True: names = sorted( colors, key=lambda c: tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(c)))) else: names = list(colors) n = len(names) nrows = math.ceil(n / ncols) width = cell_width * 4 + 2 * margin height = cell_height * nrows + 2 * margin dpi = 72 fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi) fig.subplots_adjust(margin/width, margin/height, (width-margin)/width, (height-margin)/height) ax.set_xlim(0, cell_width * 4) ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.) ax.yaxis.set_visible(False) ax.xaxis.set_visible(False) ax.set_axis_off() for i, name in enumerate(names): row = i % nrows col = i // nrows y = row * cell_height swatch_start_x = cell_width * col text_pos_x = cell_width * col + swatch_width + 7 ax.text(text_pos_x, y, name, fontsize=14, horizontalalignment='left', verticalalignment='center') ax.add_patch( Rectangle(xy=(swatch_start_x, y-9), width=swatch_width, height=18, facecolor=colors[name], edgecolor='0.7') ) return fig
Base colors#
Tableau Palette#
Python matplotlib color List of named colors
This plots a list of the named colors supported in matplotlib.
Note that :ref:`xkcd colors ` are supported as well, but are not listed here for brevity.
The following code shows how to use Python matplotlib color.
from matplotlib.patches import Rectangle import matplotlib.pyplot as plt import matplotlib.colors as mcolors def plot_colortable(colors, title, sort_colors=True, emptycols=0): cell_width = 212# w w w . d e m o 2 s. c o m cell_height = 22 swatch_width = 48 margin = 12 topmargin = 40 # Sort colors by hue, saturation, value and name. if sort_colors is True: by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))), name) for name, color in colors.items()) names = [name for hsv, name in by_hsv] else: names = list(colors) n = len(names) ncols = 4 - emptycols nrows = n // ncols + int(n % ncols > 0) width = cell_width * 4 + 2 * margin height = cell_height * nrows + margin + topmargin dpi = 72 fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi) fig.subplots_adjust(margin/width, margin/height, (width-margin)/width, (height-topmargin)/height) ax.set_xlim(0, cell_width * 4) ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.) ax.yaxis.set_visible(False) ax.xaxis.set_visible(False) ax.set_axis_off() ax.set_title(title, fontsize=24, loc="left", pad=10) for i, name in enumerate(names): row = i % nrows col = i // nrows y = row * cell_height swatch_start_x = cell_width * col text_pos_x = cell_width * col + swatch_width + 7 ax.text(text_pos_x, y, name, fontsize=14, horizontalalignment='left', verticalalignment='center') ax.add_patch( Rectangle(xy=(swatch_start_x, y-9), width=swatch_width, height=18, facecolor=colors[name], edgecolor='0.7') ) return fig plot_colortable(mcolors.BASE_COLORS, "Base Colors", sort_colors=False, emptycols=1) plot_colortable(mcolors.TABLEAU_COLORS, "Tableau Palette", sort_colors=False, emptycols=2) #sphinx_gallery_thumbnail_number = 3 plot_colortable(mcolors.CSS4_COLORS, "CSS Colors") # Optionally plot the XKCD colors (Caution: will produce large figure) #xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors") #xkcd_fig.savefig("XKCD_Colors.png") plt.show()
Related
- Python matplotlib color colormap reference
- Python matplotlib color Colors in the default property cycle
- Python matplotlib color Creating a colormap from a list of colors
- Python matplotlib color List of named colors
- Python matplotlib colors Choosing Colormaps in Matplotlib
- Python matplotlib colors Colormap Normalization
- Python matplotlib colors Creating Colormaps in Matplotlib
demo2s.com | Email: | Demo Source and Support. All rights reserved.