简体   繁体   English

将 RGB 颜色转换为 RGBA 的绘图函数? (Python)

[英]Plotly function to convert an RGB colour to RGBA? (Python)

As far as my limited knowledge of Plotly goes, it represents colours as strings like "rgb(23, 57, 211)" , or "rgba(23, 57, 211, 0.5)" , but I can't find a function that converts from RGB to RGBA.就我对 Plotly 的有限了解而言,它将颜色表示为诸如"rgb(23, 57, 211)""rgba(23, 57, 211, 0.5)"类的字符串,但我找不到一个函数从 RGB 转换为 RGBA。 I want this function because I want to add an alpha channel to an RGB colour in DEFAULT_PLOTLY_COLORS" defined in colors.py . I know it's very simple string manipulation and I can write my own function, but is there an official Plotly function to do this?我想要这个函数,因为我想在colors.py定义的DEFAULT_PLOTLY_COLORS"为 RGB 颜色添加一个 alpha 通道。我知道这是非常简单的字符串操作,我可以编写自己的函数,但是是否有官方的 Plotly 函数来执行此操作?

As of today, no such function exists to my knowledge.截至今天,据我所知,不存在这样的功能。 For anyone looking for a solution, this worked just fine for me对于任何寻找解决方案的人来说,这对我来说很好用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM