diff --git a/scripts/gray_visual.py b/scripts/gray_visual.py new file mode 100644 index 0000000..f0cab6e --- /dev/null +++ b/scripts/gray_visual.py @@ -0,0 +1,544 @@ +import numpy as np +import matplotlib as mpl +import matplotlib.pyplot as plt + +def white_to_alpha(r, g, b): + """Add transparency preserving the color when blended with white. + + RGB color components are mapped to HLS. A new color with lower luminance + (L_new < 0.5) and higher saturation (S_new = 1) and a transparency value + A_new are then computed, in such a way that the new color produces the + original color if blended with a white background. + + Parameters + ---------- + r, g, b : float + RGB components of the original color, in the [0, 1] range. + + Returns + ------- + r, g, b, a : float + RGBA components of the new color, with white component made transparent. + """ + import colorsys + + h, l, s = colorsys.rgb_to_hls(r, g, b) + if l > 0.5: + l_new = s/(1+s) + else: + l_new = 1 - (1-l)/(1 + l*(s-1)) + a = (1-l)/(1-l_new) + r_new, g_new, b_new = colorsys.hls_to_rgb(h, l_new, 1) + return r_new, g_new, b_new, a + + +def cmap_lin_alpha(name, min_loc=0, reverse_alpha=False, register=True): + """ + Set linear alpha (transparency) value in a colormap. + + A new colormap is returned, with unchanged RGB values, and transparency + changing linearly from A=0 (fully transparent) at min_alpha_loc to A=1 + (fully opaque) at the range ends. + + Parameters + ---------- + name : str + Name of builtin (or registered) colormap. + min_loc : float, default=0 + Location in the map of maximum transparency. + If min_loc=0, transparency decreases linearly from the lower end to + the upper end of the map. + If min_loc=1, transparency increases linearly from the lower end to + the upper end of the map. + If 0 ncol) {ncol=NF}; " + "{if (NF>0) {print $0} else " + r'{for (i=0; i