【发布时间】:2012-05-18 05:03:13
【问题描述】:
简单地说,给定一个初始颜色“#1010CA”和一个 alpha 因子 0.5,我怎样才能得到以color="#1010CA", alpha=0.5 形式显示在绘图上的最终颜色?
或者更直接地说,我应该填写什么
plot(x, y, color=__)
所以它相当于(至少在视觉上)
plot(x, y, color="#1010CA", alpha=0.5)
?我想这不应该是一个 python 问题,而是如何通过某种因素操纵 rgb 颜色来抑制它,但请原谅我对调色板的无知......
编辑
这是我在接受@Blender 的回答后选择的当前解决方案
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)
【问题讨论】:
-
你只是让颜色变深了两倍。添加
(0.5, 0.5, 0.5)。 -
@Blender 你说得对,我删除了那些代码,但是“0.5 + foregroud/2”是准确的还是近似的?
-
大声笑,我实际上在第一次尝试时就猜对了 (en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending)。是的,这是白色背景颜色和 0.5 透明度的精确解决方案。使用
numpy,您只需执行tuple(0.5 + numpy.array(foreground_tuple) / 2.0)并将其插入 Matplotlib 颜色参数。 -
@Blender 赞!你确实很好地“混合”了颜色!
标签: python matplotlib python-imaging-library