Search code examples
pythoncolorsplotly

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. 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?


Solution

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