simply, given an initial color “#1010CA” and an alpha factor 0.5, how can I get the final color that is shown on the plot in terms of color="#1010CA", alpha=0.5?
or more straightforwardly, what should I fill in
plot(x, y, color=__)
so that it is equivalent (at least visually) to
plot(x, y, color="#1010CA", alpha=0.5)
? I guess it shouldn’t be a python question, instead how to manipulate the rgb colors by some factor to damp it, but forgive my ignorance in color palettes…
EDIT
Here is the current solution I opt for after taking @Blender’s answer
import matplotlib
import numpy as np
def alpha_blending(hex_color, alpha) :
""" alpha blending as if on the white background.
"""
foreground_tuple = matplotlib.colors.hex2color(hex_color)
foreground_arr = np.array(foreground_tuple)
final = tuple( (1. - alpha) + foreground_arr*alpha )
return(final)
You can’t get a “final color” without the background color as well.
But for rendering alpha transparency, I would guess that a weighted sum would work:
So if your color is white and your
alpha = 0.5, you can do:Matplotlib accepts RGB tuples so you can make an opacity flattening function pretty easily. Why you need to do this, however, is not something I can answer.