# -*- coding: utf-8 -*-
"""
Created on Wed Jun 29 01:56:51 2016
@author: cjs14
"""
col_dict = {
'blues': ['light_steel_blue', 'powder_blue', 'light_blue', 'sky_blue',
'light_sky_blue', 'deep_sky_blue', 'dodger_blue', 'cornflower_blue', 'steel_blue',
'royal_blue', 'blue', 'medium_blue', 'dark_blue', 'navy', 'midnight_blue'],
'oranges': ['orange_red', 'tomato', 'coral', 'dark_orange', 'orange', 'gold'],
'browns': ['cornsilk', 'blanched_almond', 'bisque', 'navajo_white', 'wheat',
'burly_wood', 'tan', 'rosy_brown', 'sandy_brown', 'goldenrod', 'dark_goldenrod',
'peru', 'chocolate', 'saddle_brown', 'sienna', 'brown', 'maroon'],
'greens': ['dark_olive_green', 'olive',
'olive_drab', 'yellow_green', 'lime_green', 'lime', 'lawn_green', 'chartreuse',
'green_yellow', 'spring_green', 'medium_spring_green', 'light_green', 'pale_green',
'dark_sea_green', 'medium_sea_green', 'sea_green', 'forest_green', 'green',
'dark_green'],
'purples': ['lavender', 'thistle', 'plum', 'violet', 'orchid', 'fuchsia', 'magenta',
'medium_orchid', 'medium_purple', 'blue_violet', 'dark_violet', 'dark_orchid',
'dark_magenta', 'purple', 'indigo', 'dark_slate_blue', 'slate_blue',
'medium_slate_blue'],
'yellows': ['yellow', 'light_yellow', 'lemon_chiffon',
'light_goldenrod_yellow', 'papaya_whip', 'moccasin', 'peach_puff', 'pale_goldenrod',
'khaki', 'dark_khaki'],
'pinks': ['pink', 'light_pink', 'hot_pink', 'deep_pink',
'pale_violet_red', 'medium_violet_red'],
'cyans': ['medium_aquamarine', 'aqua', 'cyan', 'light_cyan', 'pale_turquoise',
'aquamarine', 'turquoise', 'medium_turquoise', 'dark_turquoise', 'light_sea_green',
'cadet_blue', 'dark_cyan', 'teal'],
'reds': ['light_salmon', 'salmon', 'dark_salmon',
'light_coral', 'indian_red', 'crimson', 'fire_brick', 'dark_red', 'red']}
[docs]def available_colors():
return col_dict.copy()
import numpy as np
# X11 colors!!
# Pink colors
pink = (255, 192, 203, 255)
light_pink = (255, 182, 193, 255)
hot_pink = (255, 105, 180, 255)
deep_pink = (255, 20, 147, 255)
pale_violet_red = (219, 112, 147, 255)
medium_violet_red = (199, 21, 133, 255)
# Red colors
light_salmon = (255, 160, 122, 255)
salmon = (250, 128, 114, 255)
dark_salmon = (233, 150, 122, 255)
light_coral = (240, 128, 128, 255)
indian_red = (205, 92, 92, 255)
crimson = (220, 20, 60, 255)
fire_brick = (178, 34, 34, 255)
dark_red = (139, 0, 0, 255)
red = (255, 0, 0, 255)
# Orange colors
orange_red = (255, 69, 0, 255)
tomato = (255, 99, 71, 255)
coral = (255, 127, 80, 255)
dark_orange = (255, 140, 0, 255)
orange = (255, 165, 0, 255)
gold = (255, 215, 0, 255)
# Yellow colors
yellow = (255, 255, 0, 255)
light_yellow = (255, 255, 224, 255)
lemon_chiffon = (255, 250, 205, 255)
light_goldenrod_yellow = (250, 250, 210, 255)
papaya_whip = (255, 239, 213, 255)
moccasin = (255, 228, 181, 255)
peach_puff = (255, 218, 185, 255)
pale_goldenrod = (238, 232, 170, 255)
khaki = (240, 230, 140, 255)
dark_khaki = (189, 183, 107, 255)
# Brown colors
cornsilk = (255, 248, 220, 255)
blanched_almond = (255, 235, 205, 255)
bisque = (255, 228, 196, 255)
navajo_white = (255, 222, 173, 255)
wheat = (245, 222, 179, 255)
burly_wood = (222, 184, 135, 255)
tan = (210, 180, 140, 255)
rosy_brown = (188, 143, 143, 255)
sandy_brown = (244, 164, 96, 255)
goldenrod = (218, 165, 32, 255)
dark_goldenrod = (184, 134, 11, 255)
peru = (205, 133, 63, 255)
chocolate = (210, 105, 30, 255)
saddle_brown = (139, 69, 19, 255)
sienna = (160, 82, 45, 255)
brown = (165, 42, 42, 255)
maroon = (128, 0, 0, 255)
# Green colors
dark_olive_green = (85, 107, 47, 255)
olive = (128, 128, 0, 255)
olive_drab = (107, 142, 35, 255)
yellow_green = (154, 205, 50, 255)
lime_green = (50, 205, 50, 255)
lime = (0, 255, 0, 255)
lawn_green = (124, 252, 0, 255)
chartreuse = (127, 255, 0, 255)
green_yellow = (173, 255, 47, 255)
spring_green = (0, 255, 127, 255)
medium_spring_green = ( 0, 250, 154, 255)
light_green = (144, 238, 144, 255)
pale_green = (152, 251, 152, 255)
dark_sea_green = (143, 188, 143, 255)
medium_sea_green = ( 60, 179, 113, 255)
sea_green = ( 46, 139, 87, 255)
forest_green = ( 34, 139, 34, 255)
green = ( 0, 128, 0, 255)
dark_green = ( 0, 100, 0, 255)
# Cyan colors
medium_aquamarine = (102, 205, 170, 255)
aqua = ( 0, 255, 255, 255)
cyan = ( 0, 255, 255, 255)
light_cyan = (224, 255, 255, 255)
pale_turquoise = (175, 238, 238, 255)
aquamarine = (127, 255, 212, 255)
turquoise = ( 64, 224, 208, 255)
medium_turquoise = ( 72, 209, 204, 255)
dark_turquoise = ( 0, 206, 209, 255)
light_sea_green = ( 32, 178, 170, 255)
cadet_blue = ( 95, 158, 160, 255)
dark_cyan = ( 0, 139, 139, 255)
teal = ( 0, 128, 128, 255)
# Blue colors
light_steel_blue = (176, 196, 222, 255)
powder_blue = (176, 224, 230, 255)
light_blue = (173, 216, 230, 255)
sky_blue = (135, 206, 235, 255)
light_sky_blue = (135, 206, 250, 255)
deep_sky_blue = ( 0, 191, 255, 255)
dodger_blue = ( 30, 144, 255, 255)
cornflower_blue = (100, 149, 237, 255)
steel_blue = ( 70, 130, 180, 255)
royal_blue = ( 65, 105, 225, 255)
blue = ( 0, 0, 255, 255)
medium_blue = ( 0, 0, 205, 255)
dark_blue = ( 0, 0, 139, 255)
navy = ( 0, 0, 128, 255)
midnight_blue = ( 25, 25, 112, 255)
# Purple colors
lavender = (230, 230, 250, 255)
thistle = (216, 191, 216, 255)
plum = (221, 160, 221, 255)
violet = (238, 130, 238, 255)
orchid = (218, 112, 214, 255)
fuchsia = (255, 0, 255, 255)
magenta = (255, 0, 255, 255)
medium_orchid = (186, 85, 211, 255)
medium_purple = (147, 112, 219, 255)
blue_violet = (138, 43, 226, 255)
dark_violet = (148, 0, 211, 255)
dark_orchid = (153, 50, 204, 255)
dark_magenta = (139, 0, 139, 255)
purple = (128, 0, 128, 255)
indigo = ( 75, 0, 130, 255)
dark_slate_blue = ( 72, 61, 139, 255)
slate_blue = (106, 90, 205, 255)
medium_slate_blue = (123, 104, 238, 255)
# White/Gray/Black colors
white = (255, 255, 255, 255)
snow = (255, 250, 250, 255)
honeydew = (240, 255, 240, 255)
mint_cream = (245, 255, 250, 255)
azure = (240, 255, 255, 255)
alice_blue = (240, 248, 255, 255)
ghost_white = (248, 248, 255, 255)
white_smoke = (245, 245, 245, 255)
seashell = (255, 245, 238, 255)
beige = (245, 245, 220, 255)
old_lace = (253, 245, 230, 255)
floral_white = (255, 250, 240, 255)
ivory = (255, 255, 240, 255)
antique_white = (250, 235, 215, 255)
linen = (250, 240, 230, 255)
lavender_blush = (255, 240, 245, 255)
misty_rose = (255, 228, 225, 255)
gainsboro = (220, 220, 220, 255)
light_gray = (211, 211, 211, 255)
silver = (192, 192, 192, 255)
dark_gray = (169, 169, 169, 255)
gray = (128, 128, 128, 255)
dim_gray = (105, 105, 105, 255)
light_slate_gray = (119, 136, 153, 255)
slate_gray = (112, 128, 144, 255)
dark_slate_gray = ( 47, 79, 79, 255)
black = ( 0, 0, 0, 255)
[docs]def any_to_rgb(color):
'''If color is an rgb tuple return it, if it is a string, parse it
and return the respective rgb tuple.
'''
if isinstance(color, tuple):
if len(color) == 3:
color = color + (255,)
return color
if isinstance(color, str):
return parse_color(color)
raise ValueError("Color not recognized: {}".format(color))
[docs]def html_to_rgb(colorstring):
""" convert #RRGGBB to an (R, G, B) tuple """
colorstring = colorstring.strip()
if colorstring[0] == '#':
colorstring = colorstring[1:]
if len(colorstring) != 6:
raise ValueError("input #%s is not in #RRGGBB format" % colorstring)
r, g, b = colorstring[:2], colorstring[2:4], colorstring[4:]
r, g, b = [int(n, 16) for n in (r, g, b)]
return (r, g, b, 255)
[docs]def parse_color(color):
'''Return the RGB 0-255 representation of the current string
passed.
It first tries to match the string with DVI color names.
'''
# Let's parse the color string
if isinstance(color, str):
# Try dvi names
try:
col = get(color)
except ValueError:
# String is not present
pass
# Try html names
try:
col = html_to_rgb(color)
except ValueError:
raise ValueError("Can't parse color string: {}'".format(color))
return col
# Functions
[docs]def get(name):
"""Given a string *color*, return the color as a tuple (r, g, b,
a) where each value is between 0 and 255.
As for the color name follow the `HTML color names
<http://www.w3schools.com/tags/ref_colornames.asp>` in lowescore
style eg. *forest_green*.
"""
try:
color = globals().get(name)
except:
raise ValueError('Color %s not found' % name)
return color
[docs]def mix(a, b, ratio=0.5):
ca = get(a)
cb = get(b)
r_a, g_a, b_a = ca[0:3]
r_b, g_b, b_b = cb[0:3]
return tuple([int(comp1 * ratio + comp2 * (1-ratio))
for comp1, comp2 in zip(ca[:3], cb[:3])] + [255])
[docs]def rgb_to_hsl_hsv(a, isHSV=True):
"""
Converts RGB image data to HSV or HSL.
:param a: 3D array. Retval of numpy.asarray(Image.open(...), int)
:param isHSV: True = HSV, False = HSL
:return: H,S,L or H,S,V array
"""
R, G, B = a.T
m = np.min(a, 1).T
M = np.max(a, 1).T
C = M - m #chroma
Cmsk = C != 0
# Hue
H = np.zeros(R.shape, int)
mask = (M == R) & Cmsk
H[mask] = np.mod(60 * (G[mask] - B[mask]) / C[mask], 360)
mask = (M == G) & Cmsk
H[mask] = (60 * (B[mask] - R[mask]) / C[mask] + 120)
mask = (M == B) & Cmsk
H[mask] = (60 * (R[mask] - G[mask]) / C[mask] + 240)
H *= 255
H /= 360 # if you prefer, leave as 0-360, but don't convert to uint8
# Saturation
S = np.zeros(R.shape, int)
if isHSV:
# This code is for HSV:
# Value
V = M
# Saturation
S[Cmsk] = ((255 * C[Cmsk]) / V[Cmsk])
# H, S, and V are now defined as integers 0-255
return np.array((H.swapaxes(0, 1), S.swapaxes(0, 1), V.swapaxes(0, 1))).T
else:
# This code is for HSL:
# Value
L = 0.5 * (M + m)
# Saturation
S[Cmsk] = ((C[Cmsk]) / (1 - np.absolute(2 * L[Cmsk]/255.0 - 1)))
# H, S, and L are now defined as integers 0-255
return np.array((H.swapaxes(0, 1), S.swapaxes(0, 1), L.swapaxes(0, 1))).T
[docs]def rgb_to_hsv(a):
return rgb_to_hsl_hsv(a, True)
[docs]def rgb_to_hsl(a):
return rgb_to_hsl_hsv(a, False)
[docs]def hsl_to_rgb(arr):
"""
Converts HSL color array to RGB array
H = [0..360]
S = [0..1]
l = [0..1]
http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSL
Returns R,G,B in [0..255]
"""
H, S, L = arr.T
H = (H.copy()/255.0) * 360
S = S.copy()/255.0
L = L.copy()/255.0
C = (1 - np.absolute(2 * L - 1)) * S
Hp = H / 60.0
X = C * (1 - np.absolute(np.mod(Hp, 2) - 1))
# initilize with zero
R = np.zeros(H.shape, float)
G = np.zeros(H.shape, float)
B = np.zeros(H.shape, float)
# handle each case:
mask = (Hp >= 0) == ( Hp < 1)
R[mask] = C[mask]
G[mask] = X[mask]
mask = (Hp >= 1) == ( Hp < 2)
R[mask] = X[mask]
G[mask] = C[mask]
mask = (Hp >= 2) == ( Hp < 3)
G[mask] = C[mask]
B[mask] = X[mask]
mask = (Hp >= 3) == ( Hp < 4)
G[mask] = X[mask]
B[mask] = C[mask]
mask = (Hp >= 4) == ( Hp < 5)
R[mask] = X[mask]
B[mask] = C[mask]
mask = (Hp >= 5) == ( Hp < 6)
R[mask] = C[mask]
B[mask] = X[mask]
m = L - 0.5*C
R += m
G += m
B += m
R *= 255.0
G *= 255.0
B *= 255.0
return np.array((R.astype(int),G.astype(int),B.astype(int))).T
# Maps
default_atom_map = {
"C": gray,
"O": red,
"H": white,
"N": light_blue,
"S": gold,
"Cl": green,
"B": green,
"P": orange,
"Fe": orange,
"Ba": orange,
"Na": blue,
"Mg": forest_green,
"Zn": brown,
"Cu": brown,
"Ni": brown,
"Br": brown,
"Ca": dark_gray,
"Mn": dark_gray,
"Al": dark_gray,
"Ti": dark_gray,
"Cr": dark_gray,
"Ag": dark_gray,
"F": goldenrod,
"Si": goldenrod,
"Au": goldenrod,
"I": purple,
"Li": fire_brick,
"He": pink,
"Xx": deep_pink,
}
light_atom_map = {
"C": gainsboro,
"O": light_salmon,
"H": snow,
"N": pale_turquoise,
"S": light_goldenrod_yellow,
"Cl": pale_green,
"B": pale_green,
"P": beige,
"Fe": beige,
"Ba": beige,
"Na": lavender,
"Mg": aquamarine,
"Zn": dark_salmon,
"Cu": dark_salmon,
"Ni": dark_salmon,
"Br": dark_salmon,
"Ca": light_slate_gray,
"Mn": light_slate_gray,
"Al": light_slate_gray,
"Ti": light_slate_gray,
"Cr": light_slate_gray,
"Ag": light_slate_gray,
"F": pale_goldenrod,
"Si": pale_goldenrod,
"Au": pale_goldenrod,
"I": lavender,
"Li": light_coral,
"He": light_pink,
"Xx": deep_pink,
}