可以将RGB颜色转换为RGBA的功能吗? (蟒蛇)

时间:2019-07-10 13:16:24

标签: python colors plotly

据我对Plotly的有限了解,它以"rgb(23, 57, 211)""rgba(23, 57, 211, 0.5)"之类的字符串表示颜色,但是我找不到从RGB转换为RGBA的函数。我需要此功能,因为我想向DEFAULT_PLOTLY_COLORS"中定义的colors.py中的RGB颜色添加Alpha通道。我知道这是非常简单的字符串操作,我可以编写自己的函数,但是有官方的Plotly函数来执行此操作吗?

1 个答案:

答案 0 :(得分:1)

截至今天,据我所知,不存在此类功能。对于任何寻找解决方案的人来说,这对我来说很好

def rgb_to_rgba(rgb_value, alpha):
    """
    Adds the alpha channel to an RGB Value and returns it as an RGBA Value
    :param rgb_value: Input RGB Value
    :param alpha: Alpha Value to add  in range [0,1]
    :return: RGBA Value
    """
    return f"rgba{rgb_value[3:-1]}, {alpha})"