Example #1
0
def tintToColormap(tint):
    """
    Convert a tint to a matplotlib.colors.Colormap object
    tint argument can be:
    - a list tuple RGB value (for a tint) or
    - a matplotlib.colors.Colormap object for a custom color map (then it is just returned as is) or
    - a string of value TINT_FIT_TO_RGB to indicate fit RGB color mapping
    - a string of value TINT_RGB_AS_IS that indicates no tint. Will be converted to a rainbow colormap
    name (string): the name argument of the new colormap object
    returns matplotlib.colors.Colormap object
    """
    if isinstance(tint, colors.Colormap):
        return tint
    elif isinstance(tint, tuple) or isinstance(tint, list):  # a tint RGB value
        # make a gradient from black to the selected tint
        tint = colors.LinearSegmentedColormap.from_list(
            "", [(0, 0, 0), rgb_to_frgb(tint)])
    elif tint == TINT_RGB_AS_IS:
        tint = cm.get_cmap('hsv')
    elif tint == TINT_FIT_TO_RGB:  # tint Fit to RGB constant
        tint = colors.ListedColormap([(0, 0, 1), (0, 1, 0), (1, 0, 0)],
                                     'Fit to RGB')
    else:
        raise TypeError("Invalid tint type: %s" % (tint, ))
    return tint
Example #2
0
def change_brightness(colour, weight):
    """ Brighten or darken a given colour

    See also wx.lib.agw.aui.aui_utilities.StepColour() and Colour.ChangeLightness() from 3.0

    colf (tuple of 3+ 0<float<1): RGB colour (and alpha)
    weight (-1<float<1): how much to brighten (>0) or darken (<0)
    return (tuple of 3+ 0<float<1): new RGB colour

    :type colf: tuple
    :type weight: float
    :rtype : tuple
    """

    _alpha = None

    if isinstance(colour, basestring):
        _col = hex_to_frgb(colour)
        _alpha = None
    elif isinstance(colour, tuple):
        if all([isinstance(v, float) for v in colour]):
            _col = colour[:3]
            _alpha = colour[-1] if len(colour) == 4 else None
        elif all([isinstance(v, int) for v in colour]):
            _col = rgb_to_frgb(colour[:3])
            _alpha = colour[-1] if len(colour) == 4 else None
        else:
            raise ValueError("Unknown colour format (%s)" % (colour, ))
    elif isinstance(colour, wx.Colour):
        _col = wxcol_to_frgb(colour)
        _alpha = None
    else:
        raise ValueError("Unknown colour format")

    if weight > 0:
        # blend towards white
        f, lim = min, 1.0
    else:
        # blend towards black
        f, lim = max, 0.0
        weight = -weight

    new_fcol = tuple(f(c * (1 - weight) + lim * weight, lim) for c in _col[:3])

    return new_fcol + (_alpha, ) if _alpha is not None else new_fcol