(vals, vmin, vmax, colorscale, return_hex=True)
return [f"rgb{tuple(v)}" for v in vals_rgb]
Given a float array vals, interpolate based on a colorscale to obtain
    rgb or hex colors. Inspired by
    `user empet's answer in \
    <community.plotly.com/t/hover-background-color-on-scatter-3d/9185/6>`_.
Given a float array vals, interpolate based on a colorscale to obtain
    rgb or hex colors. Inspired by
    `user empet's answer in \
    <community.plotly.com/t/hover-background-color-on-scatter-3d/9185/6>`_.
Given a float array vals interpolate based on a colorscale to obtain rgb or hex colors . Inspired by user empet s answer in \ <community . plotly . com / t / hover - background - color - on - scatter - 3d / 9185 / 6 > _ .
def _get_colors_for_vals(vals, vmin, vmax, colorscale, return_hex=True):
    """Given a float array vals, interpolate based on a colorscale to obtain
    rgb or hex colors. Inspired by
    `user empet's answer in \
    <community.plotly.com/t/hover-background-color-on-scatter-3d/9185/6>`_."""
    from numbers import Number
    from ast import literal_eval

    if vmin >= vmax:
        raise ValueError("`vmin` should be < `vmax`.")

    if (len(colorscale[0]) == 2) and isinstance(colorscale[0][0], Number):
        scale, colors = zip(*colorscale)
    else:
        scale = np.linspace(0, 1, num=len(colorscale))
        colors = colorscale
    scale = np.asarray(scale)

    if colors[0][:3] == "rgb":
        colors = np.asarray([literal_eval(color[3:]) for color in colors],
                            dtype=float)
    elif colors[0][0] == "#":
        colors = np.asarray(list(map(_hex_to_rgb, colors)), dtype=float)
    else:
        raise ValueError("This colorscale is not supported.")

    colorscale = np.hstack([scale.reshape(-1, 1), colors])
    colorscale = np.vstack([colorscale, colorscale[0, :]])
    colorscale_diffs = np.diff(colorscale, axis=0)
    colorscale_diff_ratios = colorscale_diffs[:, 1:] / colorscale_diffs[:, [0]]
    colorscale_diff_ratios[-1, :] = np.zeros(3)

    vals_scaled = (vals - vmin) / (vmax - vmin)

    left_bin_indices = np.digitize(vals_scaled, scale) - 1
    left_endpts = colorscale[left_bin_indices]
    vals_scaled -= left_endpts[:, 0]
    diff_ratios = colorscale_diff_ratios[left_bin_indices]

    vals_rgb = (
            left_endpts[:, 1:] + diff_ratios * vals_scaled[:, np.newaxis] + 0.5
        ).astype(np.uint8)

    if return_hex:
        return list(map(_rbg_to_hex, vals_rgb))

    return [f"rgb{tuple(v)}" for v in vals_rgb]
def _get_colors_for_vals ( vals , vmin , vmax , colorscale , return_hex = True ) : from numbers import Number from ast import literal_eval if vmin >= vmax : raise ValueError ( "`vmin` should be < `vmax`." ) if ( len ( colorscale [ 0 ] ) == 2 ) and isinstance ( colorscale [ 0 ] [ 0 ] , Number ) : scale , colors = zip ( * colorscale ) else : scale = np . linspace ( 0 , 1 , num = len ( colorscale ) ) colors = colorscale scale = np . asarray ( scale ) if colors [ 0 ] [ : 3 ] == "rgb" : colors = np . asarray ( [ literal_eval ( color [ 3 : ] ) for color in colors ] , dtype = float ) elif colors [ 0 ] [ 0 ] == "#" : colors = np . asarray ( list ( map ( _hex_to_rgb , colors ) ) , dtype = float ) else : raise ValueError ( "This colorscale is not supported." ) colorscale = np . hstack ( [ scale . reshape ( - 1 , 1 ) , colors ] ) colorscale = np . vstack ( [ colorscale , colorscale [ 0 , : ] ] ) colorscale_diffs = np . diff ( colorscale , axis = 0 ) colorscale_diff_ratios = colorscale_diffs [ : , 1 : ] / colorscale_diffs [ : , [ 0 ] ] colorscale_diff_ratios [ - 1 , : ] = np . zeros ( 3 ) vals_scaled = ( vals - vmin ) / ( vmax - vmin ) left_bin_indices = np . digitize ( vals_scaled , scale ) - 1 left_endpts = colorscale [ left_bin_indices ] vals_scaled -= left_endpts [ : , 0 ] diff_ratios = colorscale_diff_ratios [ left_bin_indices ] vals_rgb = ( left_endpts [ : , 1 : ] + diff_ratios * vals_scaled [ : , np . newaxis ] + 0.5 ) . astype ( np . uint8 ) if return_hex : return list ( map ( _rbg_to_hex , vals_rgb ) ) return [ f"rgb{tuple(v)}" for v in vals_rgb ]