首页 > 解决方案 > Plotly函数将RGB颜色转换为RGBA?(Python)

问题描述

就我对 Plotly 的有限了解而言,它将颜色表示为"rgb(23, 57, 211)", 或之类的字符串"rgba(23, 57, 211, 0.5)",但我找不到从 RGB 转换为 RGBA 的函数。我想要这个函数,因为我想将 alpha 通道添加DEFAULT_PLOTLY_COLORS"colors.py. 我知道这是非常简单的字符串操作,我可以编写自己的函数,但是有官方的 Plotly 函数可以做到这一点吗?

标签: pythoncolorsplotly

解决方案


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

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})"


推荐阅读