Ejemplo n.º 1
0
    def _compute_vertices_and_box(self):
        """Compute location of the box for rendering.

        Returns
        -------
        vertices : np.ndarray
            Nx2 array of any vertices to be rendered as Markers
        face_color : str
            String of the face color of the Markers
        edge_color : str
            String of the edge color of the Markers and Line for the box
        pos : np.ndarray
            Nx2 array of vertices of the box that will be rendered using a
            Vispy Line
        width : float
            Width of the box edge
        """
        if (self._interaction_box._box is not None
                and self._interaction_box.show):
            box = self._interaction_box._box
            if self._interaction_box.show_handle:
                box = self._add_rotation_handle(box)

            face_color = self._highlight_color

            edge_color = self._highlight_color
            if self._interaction_box.show_vertices:
                colors = np.array([(1.0, 1.0, 1.0) for point in box])
                if self._interaction_box.selected_vertex is not None:
                    colors[self._interaction_box.
                           selected_vertex] = self._highlight_color
                if self._interaction_box.show_handle:
                    vertices = box[Box.WITH_HANDLE][:, ::-1]
                    face_color = ColorArray(colors[Box.WITH_HANDLE])
                else:
                    vertices = box[Box.WITHOUT_HANDLE][:, ::-1]
                    face_color = ColorArray(colors[Box.WITHOUT_HANDLE])

            else:
                vertices = np.empty((0, 2))
                face_color = 'white'

            # Use a subset of the vertices of the interaction_box to plot
            # the line around the edge
            if self._interaction_box.show_handle:
                pos = box[Box.LINE_HANDLE][:, ::-1]
            else:
                pos = box[Box.LINE][:, ::-1]
            width = self._highlight_width
            self._box = box
        else:
            # Otherwise show nothing
            vertices = np.empty((0, 2))
            face_color = 'white'
            edge_color = 'white'
            pos = None
            width = 0
            self._box = None

        return vertices, face_color, edge_color, pos, width
Ejemplo n.º 2
0
def pointer_gamut_visual(reference_colourspace='CIE xyY',
                         size=4.0,
                         edge_size=0.5,
                         uniform_colour=(0.9, 0.9, 0.9),
                         uniform_opacity=0.8,
                         uniform_edge_colour=(0.9, 0.9, 0.9),
                         uniform_edge_opacity=0.8,
                         parent=None):
    """
    Returns a :class:`vispy.scene.visuals.Symbol` class instance representing
    Pointer's Gamut data using cross symbols.

    Parameters
    ----------
    reference_colourspace : unicode, optional
        **{'CIE XYZ', 'CIE xyY', 'CIE Lab', 'CIE Luv', 'CIE UCS', 'CIE UVW',
        'IPT', 'Hunter Lab', 'Hunter Rdab'}**,
        Reference colourspace to use for colour conversions / transformations.
    size : numeric, optional
        Cross size.
    edge_size : numeric, optional
        Cross edge size.
    uniform_colour : array_like, optional
        Uniform cross colour.
    uniform_opacity : numeric, optional
        Uniform cross opacity.
    uniform_edge_colour : array_like, optional
        Uniform cross edge colour.
    uniform_edge_opacity : numeric, optional
        Uniform cross edge opacity.
    parent : Node, optional
        Parent of the Pointer's Gamut visual in the `SceneGraph`.

    Returns
    -------
    Symbol
        Pointer's Gamut visual.
    """

    points = common_colourspace_model_axis_reorder(
        XYZ_to_colourspace_model(
            POINTER_GAMUT_DATA,
            POINTER_GAMUT_ILLUMINANT,
            reference_colourspace),
        reference_colourspace)

    RGB = ColorArray(uniform_colour, alpha=uniform_opacity).rgba
    RGB_e = ColorArray(uniform_edge_colour, alpha=uniform_edge_opacity).rgba

    markers = Symbol(symbol='cross',
                     positions=points,
                     size=size,
                     edge_size=edge_size,
                     face_colour=RGB,
                     edge_colour=RGB_e,
                     parent=parent)

    return markers
Ejemplo n.º 3
0
def _glsl_step(controls=None, colors=None, texture_map_data=None):
    assert (controls[0], controls[-1]) == (0., 1.)
    ncolors = len(controls) - 1
    assert ncolors >= 2
    assert (texture_map_data is not None)

    LUT = texture_map_data
    texture_len = texture_map_data.shape[0]
    LUT_tex_idx = np.linspace(0.0, 1.0, texture_len)

    # Replicate indices to colormap texture.
    # The resulting matrix has size of (texture_len,len(controls)).
    # It is used to perform piecewise constant interpolation
    # for each RGBA color component.
    t2 = np.repeat(LUT_tex_idx[:, np.newaxis], len(controls), 1)

    # Perform element-wise comparison to find
    # control points for all LUT colors.
    bn = np.sum(controls.transpose() <= t2, axis=1)

    j = np.clip(bn - 1, 0, ncolors - 1)

    # Copying color data from ColorArray to array-like
    # makes data assignment to LUT faster.
    colors_rgba = ColorArray(colors[:])._rgba
    LUT[:, 0, :] = colors_rgba[j]

    s2 = "uniform sampler2D texture2D_LUT;"
    s = "{\n return texture2D(texture2D_LUT, \
           vec2(0.0, clamp(t, 0.0, 1.0)));\n} "

    return "%s\nvec4 colormap(float t) {\n%s\n}" % (s2, s)
Ejemplo n.º 4
0
    def __init__(self,
                 vertices,
                 faces,
                 uniform_colour=(0.5, 0.5, 1.0),
                 uniform_opacity=1.0,
                 vertex_colours=None,
                 wireframe=False,
                 wireframe_offset=None):
        """
        Creates a primitive visual based on
        :class:`vispy.visuals.mesh.MeshVisual` class.

        Parameters
        ----------
        vertices : array_like
            Vertices data.
        faces : array_like
            Faces data.
        uniform_colour : array_like, optional
            Uniform mesh colour.
        uniform_opacity : numeric, optional
            Uniform mesh opacity.
        vertex_colours : array_like, optional
            Per vertex varying colour.
        wireframe : bool, optional
            Use wireframe display.
        wireframe_offset : array_like, optional
            Wireframe offset.

        Notes
        -----
        -   `vertex_colours` argument takes precedence over `uniform_colour` if
            provided.
        -   `uniform_opacity` argument will be stacked to `vertex_colours`
            argument if the latter last dimension is equal to 3.
        """

        self._wireframe = wireframe
        self._wireframe_offset = wireframe_offset
        mode = 'lines' if self._wireframe else 'triangles'

        uniform_colour = ColorArray(uniform_colour, alpha=uniform_opacity).rgba
        if vertex_colours is not None:
            if vertex_colours.shape[-1] == 3:
                vertex_colours = np.hstack(
                    (vertex_colours,
                     np.full((vertex_colours.shape[0], 1), uniform_opacity,
                             DEFAULT_FLOAT_DTYPE)))
            else:
                vertex_colours[..., 3] = uniform_opacity

        MeshVisual.__init__(
            self,
            vertices,
            faces,
            vertex_colours,
            None,
            uniform_colour,
            mode=mode)
Ejemplo n.º 5
0
 def __init__(self, colors=None):
     # Ensure the colors are arrays.
     if colors is not None:
         self.colors = colors
     if not isinstance(self.colors, ColorArray):
         self.colors = ColorArray(self.colors)
     # Process the GLSL map function by replacing $color_i by the
     if len(self.colors) > 0:
         self.glsl_map = _process_glsl_template(self.glsl_map,
                                                self.colors.rgba)
Ejemplo n.º 6
0
 def __getitem__(self, item):
     if isinstance(item, tuple):
         raise ValueError('ColorArray indexing is only allowed along '
                          'the first dimension.')
     # Ensure item is either a scalar or a column vector.
     item = _vector(item, type='column')
     # Clip the values in [0, 1].
     item = np.clip(item, 0., 1.)
     colors = self.map(item)
     return ColorArray(colors)
Ejemplo n.º 7
0
def pointer_gamut_boundaries_visual(reference_colourspace='CIE xyY',
                                    width=2.0,
                                    uniform_colour=(0.9, 0.9, 0.9),
                                    uniform_opacity=0.8,
                                    parent=None):
    """
    Returns a :class:`vispy.scene.visuals.Line` class instance representing
    Pointer's Gamut boundaries.

    Parameters
    ----------
    reference_colourspace : unicode, optional
        See :func:`pointer_gamut_visual` argument for possible values.

        Reference colourspace to use for colour conversions / transformations.
    width : numeric, optional
        Line width.
    uniform_colour : array_like, optional
        Uniform line colour.
    uniform_opacity : numeric, optional
        Uniform line opacity.
    parent : Node, optional
        Parent of the Pointer's Gamut boundaries visual in the `SceneGraph`.

    Returns
    -------
    Line
        Pointer's Gamut boundaries visual.
    """

    XYZ = np.vstack((POINTER_GAMUT_BOUNDARIES,
                     POINTER_GAMUT_BOUNDARIES[0, ...]))

    illuminant = DEFAULT_PLOTTING_ILLUMINANT

    points = common_colourspace_model_axis_reorder(
        XYZ_to_colourspace_model(
            XYZ, illuminant, reference_colourspace),
        reference_colourspace)
    points[np.isnan(points)] = 0

    RGB = ColorArray(uniform_colour, alpha=uniform_opacity).rgba

    line = Line(points,
                RGB,
                width=width,
                method='agg',
                parent=parent)

    return line
Ejemplo n.º 8
0
def pointer_gamut_hull_visual(reference_colourspace='CIE xyY',
                              width=2.0,
                              uniform_colour=(0.9, 0.9, 0.9),
                              uniform_opacity=0.4,
                              parent=None):
    """
    Returns a :class:`vispy.scene.visuals.Node` class instance with
    :class:`vispy.scene.visuals.Lines` class instance children representing
    Pointer's Gamut hull.

    Parameters
    ----------
    reference_colourspace : unicode, optional
        See :func:`pointer_gamut_visual` argument for possible values.

        Reference colourspace to use for colour conversions / transformations.
    width : numeric, optional
        Lines width.
    uniform_colour : array_like, optional
        Uniform lines colour.
    uniform_opacity : numeric, optional
        Uniform lines opacity.
    parent : Node, optional
        Parent of the Pointer's Gamut hull visual in the `SceneGraph`.

    Returns
    -------
    Node
        Pointer's Gamut hull visual.
    """

    node = Node(parent=parent)

    pointer_gamut_data = np.reshape(POINTER_GAMUT_DATA, (16, -1, 3))
    for i in range(16):
        points = common_colourspace_model_axis_reorder(
            XYZ_to_colourspace_model(
                np.vstack((pointer_gamut_data[i],
                           pointer_gamut_data[i][0, ...])),
                POINTER_GAMUT_ILLUMINANT,
                reference_colourspace),
            reference_colourspace)

        points[np.isnan(points)] = 0

        RGB = ColorArray(uniform_colour, alpha=uniform_opacity).rgba

        Line(points, RGB, width=width, parent=node)
    return node
Ejemplo n.º 9
0
 def __init__(self, limits=(0.33, 0.66, 1.0)):
     limits = np.array(limits, float).ravel()
     if len(limits) != 3:
         raise ValueError('limits must have 3 values')
     if (np.diff(limits) < 0).any() or (limits <= 0).any():
         raise ValueError('limits must be strictly increasing and positive')
     controls = np.array([
         -limits[2], -limits[1], -limits[0], limits[0], limits[1], limits[2]
     ])
     controls = ((controls / limits[2]) + 1) / 2.
     colors = [(0., 1., 1., 1.), (0., 0., 1., 1.), (0., 0., 1., 0.),
               (1., 0., 0., 0.), (1., 0., 0., 1.), (1., 1., 0., 1.)]
     colors = ColorArray(colors)
     super(_RedYellowBlueCyan, self).__init__(colors,
                                              controls=controls,
                                              interpolation='linear')
Ejemplo n.º 10
0
    def __init__(self,
                 h_pos=20,
                 h_neg=250,
                 saturation=1.0,
                 value=0.7,
                 center="light"):
        saturation *= 99
        value *= 99

        start = husl_to_rgb(h_neg, saturation, value)
        mid = ((0.133, 0.133, 0.133) if center == "dark" else
               (0.92, 0.92, 0.92))
        end = husl_to_rgb(h_pos, saturation, value)

        colors = ColorArray([start, mid, end])

        super(_Diverging, self).__init__(colors)
Ejemplo n.º 11
0
    def __init__(self,
                 ncolors=6,
                 hue_start=0,
                 saturation=1.0,
                 value=1.0,
                 controls=None,
                 interpolation='linear'):
        hues = np.linspace(0, 360, ncolors + 1)[:-1]
        hues += hue_start
        hues %= 360

        colors = ColorArray([(hue, saturation, value) for hue in hues],
                            color_space='hsv')

        super(_HSL, self).__init__(colors,
                                   controls=controls,
                                   interpolation=interpolation)
Ejemplo n.º 12
0
def _glsl_mix(controls=None, colors=None, texture_map_data=None):
    """Generate a GLSL template function from a given interpolation patterns
    and control points.

    Parameters
    ----------
    colors : array-like, shape (n_colors, 4)
        The control colors used by the colormap.
        Elements of colors must be convertible to an instance of Color-class.

    controls : list
        The list of control points for the given colors. It should be
        an increasing list of floating-point number between 0.0 and 1.0.
        The first control point must be 0.0. The last control point must be
        1.0. The number of control points depends on the interpolation scheme.

    texture_map_data : ndarray, shape(texture_len, 4)
        Numpy array of size of 1D texture lookup data
        for luminance to RGBA conversion.

    """
    assert (controls[0], controls[-1]) == (0., 1.)
    ncolors = len(controls)
    assert ncolors >= 2
    assert (texture_map_data is not None)

    LUT = texture_map_data
    texture_len = texture_map_data.shape[0]

    # Perform linear interpolation for each RGBA color component.
    c_rgba = ColorArray(colors)._rgba
    x = np.linspace(0.0, 1.0, texture_len)
    LUT[:, 0, 0] = np.interp(x, controls, c_rgba[:, 0])
    LUT[:, 0, 1] = np.interp(x, controls, c_rgba[:, 1])
    LUT[:, 0, 2] = np.interp(x, controls, c_rgba[:, 2])
    LUT[:, 0, 3] = np.interp(x, controls, c_rgba[:, 3])

    s2 = "uniform sampler2D texture2D_LUT;"
    s = "{\n return texture2D(texture2D_LUT, \
          vec2(0.0, clamp(t, 0.0, 1.0)));\n} "

    return "%s\nvec4 colormap(float t) {\n%s\n}" % (s2, s)
Ejemplo n.º 13
0
    def __init__(self,
                 ncolors=6,
                 hue_start=0,
                 saturation=1.0,
                 value=0.7,
                 controls=None,
                 interpolation='linear'):
        hues = np.linspace(0, 360, ncolors + 1)[:-1]
        hues += hue_start
        hues %= 360

        saturation *= 99
        value *= 99

        colors = ColorArray(
            [husl_to_rgb(hue, saturation, value) for hue in hues], )

        super(_HUSL, self).__init__(colors,
                                    controls=controls,
                                    interpolation=interpolation)
Ejemplo n.º 14
0
def RGB_scatter_visual(RGB,
                       colourspace='ITU-R BT.709',
                       reference_colourspace='CIE xyY',
                       symbol='disc',
                       size=4.0,
                       edge_size=0.5,
                       uniform_colour=None,
                       uniform_opacity=1.0,
                       uniform_edge_colour=None,
                       uniform_edge_opacity=1.0,
                       resampling='auto',
                       parent=None):
    """
    Returns a :class:`vispy.scene.visuals.Symbol` class instance representing
    *RGB* data using given symbols.

    Parameters
    ----------
    RGB : array_like
        *RGB* data to draw.
    colourspace : unicode, optional
        **{'ITU-R BT.709', 'ACES2065-1', 'ACEScc', 'ACEScg', 'ACESproxy',
        'ALEXA Wide Gamut', 'Adobe RGB (1998)', 'Adobe Wide Gamut RGB',
        'Apple RGB', 'Best RGB', 'Beta RGB', 'CIE RGB', 'Cinema Gamut',
        'ColorMatch RGB', 'DCI-P3', 'DCI-P3+', 'DRAGONcolor', 'DRAGONcolor2',
        'Don RGB 4', 'ECI RGB v2', 'ERIMM RGB', 'Ekta Space PS 5', 'Max RGB',
        'NTSC', 'Pal/Secam', 'ProPhoto RGB', 'REDcolor', 'REDcolor2',
        'REDcolor3', 'REDcolor4', 'RIMM RGB', 'ROMM RGB', 'ITU-R BT.2020',
        'Russell RGB', 'S-Gamut', 'S-Gamut3', 'S-Gamut3.Cine', 'SMPTE-C RGB',
        'V-Gamut', 'Xtreme RGB', 'sRGB'}**,
        :class:`colour.RGB_Colourspace` class instance name defining the *RGB*
        colourspace of the data to draw.
    reference_colourspace : unicode, optional
        **{'CIE XYZ', 'CIE xyY', 'CIE Lab', 'CIE Luv', 'CIE UCS', 'CIE UVW',
        'IPT', 'Hunter Lab', 'Hunter Rdab'}**,
        Reference colourspace to use for colour conversions / transformations.
    symbol : unicode, optional
        Symbol type to draw.
    size : numeric, optional
        Symbol size.
    edge_size : numeric, optional
        Symbol edge size.
    uniform_colour : array_like, optional
        Uniform symbol colour.
    uniform_opacity : numeric, optional
        Uniform symbol opacity.
    uniform_edge_colour : array_like, optional
        Uniform symbol edge colour.
    uniform_edge_opacity : numeric, optional
        Uniform symbol edge opacity.
    resampling : numeric or unicode, optional
        Resampling value, if numeric input, one pixel every `resampling`
        argument value will be kept.
    parent : Node, optional
        Parent of the *RGB* scatter visual in the `SceneGraph`.

    Returns
    -------
    Symbol
        *RGB* scatter visual.
    """

    colourspace = get_RGB_colourspace(colourspace)

    RGB = np.asarray(RGB)

    if resampling == 'auto':
        resampling = max(int((0.0078125 * np.average(RGB.shape[0:1])) // 2), 1)

        RGB = RGB[::resampling, ::resampling].reshape((-1, 3))

    XYZ = RGB_to_XYZ(RGB, colourspace.whitepoint, colourspace.whitepoint,
                     colourspace.RGB_to_XYZ_matrix)

    points = common_colourspace_model_axis_reorder(
        XYZ_to_colourspace_model(XYZ, colourspace.whitepoint,
                                 reference_colourspace), reference_colourspace)

    points[np.isnan(points)] = 0

    RGB = np.clip(RGB, 0, 1)

    if uniform_colour is None:
        RGB = np.hstack((RGB,
                         np.full((RGB.shape[0], 1), uniform_opacity,
                                 DEFAULT_FLOAT_DTYPE)))
    else:
        RGB = ColorArray(uniform_colour, alpha=uniform_opacity).rgba

    if uniform_edge_colour is None:
        RGB_e = RGB
    else:
        RGB_e = ColorArray(uniform_edge_colour,
                           alpha=uniform_edge_opacity).rgba

    markers = Symbol(symbol=symbol,
                     positions=points,
                     size=size,
                     edge_size=edge_size,
                     face_colour=RGB,
                     edge_colour=RGB_e,
                     parent=parent)

    return markers
Ejemplo n.º 15
0
def spectral_locus_visual(reference_colourspace='CIE xyY',
                          cmfs='CIE 1931 2 Degree Standard Observer',
                          width=2.0,
                          uniform_colour=None,
                          uniform_opacity=1.0,
                          method='gl',
                          parent=None):
    """
    Returns a :class:`vispy.scene.visuals.Line` class instance representing
    the spectral locus.

    Parameters
    ----------
    reference_colourspace : unicode, optional
        **{'CIE XYZ', 'CIE xyY', 'CIE Lab', 'CIE Luv', 'CIE UCS', 'CIE UVW',
        'IPT', 'Hunter Lab', 'Hunter Rdab'}**,
        Reference colourspace to use for colour conversions / transformations.
    cmfs : unicode, optional
        Standard observer colour matching functions used to draw the spectral
        locus.
    width : numeric, optional
        Line width.
    uniform_colour : array_like, optional
        Uniform symbol colour.
    uniform_opacity : numeric, optional
        Uniform symbol opacity.
    method : unicode, optional
        **{'gl', 'agg'}**,
        Line drawing method.
    parent : Node, optional
        Parent of the spectral locus visual in the `SceneGraph`.

    Returns
    -------
    Line
        Spectral locus visual.
    """

    cmfs = first_item(filter_cmfs(cmfs).values())
    XYZ = cmfs.values

    XYZ = np.vstack([XYZ, XYZ[0, ...]])

    illuminant = DEFAULT_PLOTTING_ILLUMINANT

    points = common_colourspace_model_axis_reorder(
        XYZ_to_colourspace_model(XYZ, illuminant, reference_colourspace),
        reference_colourspace)
    points[np.isnan(points)] = 0

    if uniform_colour is None:
        RGB = normalise_maximum(XYZ_to_sRGB(XYZ, illuminant), axis=-1)
        RGB = np.hstack([
            RGB,
            np.full((RGB.shape[0], 1), uniform_opacity, DEFAULT_FLOAT_DTYPE)
        ])
    else:
        RGB = ColorArray(uniform_colour, alpha=uniform_opacity).rgba

    line = Line(points,
                np.clip(RGB, 0, 1),
                width=width,
                method=method,
                parent=parent)

    return line
Ejemplo n.º 16
0
    # Some colormap presets
    autumn=Colormap([(1., 0., 0., 1.), (1., 1., 0., 1.)]),
    blues=Colormap([(1., 1., 1., 1.), (0., 0., 1., 1.)]),
    cool=Colormap([(0., 1., 1., 1.), (1., 0., 1., 1.)]),
    greens=Colormap([(1., 1., 1., 1.), (0., 1., 0., 1.)]),
    reds=Colormap([(1., 1., 1., 1.), (1., 0., 0., 1.)]),
    spring=Colormap([(1., 0., 1., 1.), (1., 1., 0., 1.)]),
    summer=Colormap([(0., .5, .4, 1.), (1., 1., .4, 1.)]),
    fire=_Fire(),
    grays=_Grays(),
    hot=_Hot(),
    ice=_Ice(),
    winter=_Winter(),
    light_blues=_SingleHue(),
    orange=_SingleHue(hue=35),
    viridis=Colormap(ColorArray(_viridis_data)),
    # Diverging presets
    coolwarm=Colormap(
        ColorArray([(226, 0.59, 0.92), (222, 0.44, 0.99), (218, 0.26, 0.97),
                    (30, 0.01, 0.87), (20, 0.3, 0.96), (15, 0.5, 0.95),
                    (8, 0.66, 0.86)],
                   color_space="hsv")),
    PuGr=_Diverging(145, 280, 0.85, 0.30),
    GrBu=_Diverging(255, 133, 0.75, 0.6),
    GrBu_d=_Diverging(255, 133, 0.75, 0.6, "dark"),
    RdBu=_Diverging(220, 20, 0.75, 0.5),

    # Configurable colormaps
    cubehelix=CubeHelixColormap,
    single_hue=_SingleHue,
    hsl=_HSL,
Ejemplo n.º 17
0
 def __init__(self, hue=200, saturation_range=[0.1, 0.8], value=1.0):
     colors = ColorArray([(hue, saturation_range[0], value),
                          (hue, saturation_range[1], value)],
                         color_space='hsv')
     super(_SingleHue, self).__init__(colors)