Beispiel #1
0
def make_cmap(color, rotation=.5, white=False, transparent_zero=False):
    h, s, v = rgb_to_hsv(color)
    h = h + rotation
    if h > 1:
        h -= 1
    r, g, b = color
    ri, gi, bi = hsv_to_rgb((h, s, v))
    colors = {'direct': (ri, gi, bi), 'inverted': (r, g, b)}
    cdict = {}
    for direction, (r, g, b) in colors.items():
        if white:
            cdict[direction] = {color: [(0.0, 0.0416, 0.0416),
                                        (0.18, c, c),
                                        (0.5, 1, 1),
                                        (0.62, 0.0, 0.0),
                                        (1.0, 0.0416, 0.0416)] for color, c in
                                [('blue', b), ('red', r), ('green', g)]}
        else:
            cdict[direction] = {color: [(0.0, 1, 1),
                                        (0.32, c, c),
                                        (0.5, 0.0416, 0.0416),
                                        (0.5, 0.0, 0.0),
                                        (0.87, 0.0, 0.0),
                                        (1.0, 1, 1)] for color, c in
                                [('blue', b), ('red', r), ('green', g)]}
        if transparent_zero:
            cdict[direction]['alpha']: [(0, 1, 1), (0.5, 0, 0), (1, 1, 1)]
    cmap = LinearSegmentedColormap('cmap', cdict['direct'])
    cmapi = LinearSegmentedColormap('cmap', cdict['inverted'])
    cmap._init()
    cmapi._init()
    cmap._lut = np.maximum(cmap._lut, cmapi._lut[::-1])
    # Big hack from nilearn (WTF !?)
    cmap._lut[-1, -1] = 0
    return cmap