# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# Author: Markus Ritschel# eMail: git@markusritschel.de# Date: 2024-06-08# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#importloggingimportmatplotlib.colorsasmcolorsimportnumpyasnplog=logging.getLogger(__name__)
[docs]defhex_to_rgb(value:str)->tuple:"""Convert hex to rgb colors Parameters ---------- value: string String of 6 characters representing a hex colour. Returns ------- list length 3 of RGB values Examples -------- >>> hex_to_rgb('#f00') # red (255, 0, 0) >>> hex_to_rgb('fff') # white (255, 255, 255) """value=value.strip("#")# removes hash symbol if presentiflen(value)==3:value=''.join(c*2forcinvalue)eliflen(value)!=6:raiseValueError("HEX value must be of length 3 or 6.")returntuple(int(value[i:i+2],16)foriinrange(0,6,2))
[docs]defrgb_to_dec(value:list[float])->tuple[float]:"""Convert rgb to decimal colours (i.e. divides each value by 255) Parameters ---------- value: list (length 3) of RGB values Returns ------- list (length 3) of decimal values Example ------- >>> rgb_to_dec((255, 0, 0)) (1.0, 0.0, 0.0) """returntuple([v/255forvinvalue])
[docs]defbuild_continuous_cmap(hex_list:list[str],float_list=None,N=256,name='my_cmap')->mcolors.LinearSegmentedColormap:"""Create and return a color map that can be used in heat map figures. If `float_list` is not provided, colour map graduates linearly between each color in hex_list. If `float_list` is provided, each color in `hex_list` is mapped to the respective location in `float_list`. Source: https://towardsdatascience.com/beautiful-custom-colormaps-with-matplotlib-5bab3d1f0e72 Parameters ---------- hex_list: list of hex code strings float_list: list of floats between 0 and 1, same length as hex_list. Must start with 0 and end with 1. Returns ------- colour map """rgb_list=[rgb_to_dec(hex_to_rgb(i))foriinhex_list]iffloat_list:passelse:float_list=list(np.linspace(0,1,len(rgb_list)))cdict=dict()fornum,colinenumerate(['red','green','blue']):col_list=[[float_list[i],rgb_list[i][num],rgb_list[i][num]]foriinrange(len(float_list))]cdict[col]=col_listcmap=mcolors.LinearSegmentedColormap(name,segmentdata=cdict,N=N)returncmap