I would like to be able to replicate the look of a primary color ('r','g' or 'b') in matplotlib with an alpha of 0.5 over a white background, while keeping the alpha at 1.
Here is an example below, where through manual experimentation I've found the RGB values that with an alpha of 1, look similar to matplotlib default colors with an alpha 0.5.
I was wondering if someone had an automated way of achieving this.
import matplotlib.pyplot as plt
s=1000
plt.xlim([4,8])
plt.ylim([0,10])
red=(1,0.55,0.55)
blue=(0.55,0.55,1)
green=(0.54,0.77,0.56)
plt.scatter([5],[5],c='r',edgecolors='none',s=s,alpha=0.5,marker='s')
plt.scatter([6],[5],c='b',edgecolors='none',s=s,alpha=0.5,marker='s')
plt.scatter([7],[5],c='g',edgecolors='none',s=s,alpha=0.5,marker='s')
plt.scatter([5],[5.915],c=red,edgecolors='none',s=s,marker='s')
plt.scatter([6],[5.915],c=blue,edgecolors='none',s=s,marker='s')
plt.scatter([7],[5.915],c=green,edgecolors='none',s=s,marker='s')
Edit: you can use the formula from this answer
Converted to Python, it looks like this:
So you can do:
Using this function now, we can create a plot that confirms this works:
I don't know if it is standard, but on my computer the following works:
Basically, for each component, you have
c + (1 - c) * (1 - a)
wherea
is the alpha value you are trying to simulate.For "simple" color like
(1, 0, 0)
you get(1, 1 - a, 1 - a)
, for black(0, 0, 0)
you get(1 - a, 1 - a, 1 - a)
which is correct and for white(1, 1, 1)
you get(1, 1, 1)
which is also correct.I tried with various combinations of alpha and colors, and I still did not find any values for which it did not work, but still, this is not proven ;)
Here is a small code I used to randomly checked different values of
c
andalpha
: