Beispiel #1
0
def get_cmaps():
    cmaps = {}
    for cmap in vcs.listelements('colormap'):
        index = vcs.getcolormap(cmap).index
        cmaps[cmap] = [
            vcs.getcolormap(cmap).getcolorcell(i) for i in range(256)
        ]
    return cmaps
Beispiel #2
0
def colormap(name):
    try:
        cmap = vcs.getcolormap(str(name))
    except:
        abort(404)
    cmap_dict, _ = vcs.utils.dumpToDict(cmap)
    return jsonify(cmap_dict)
Beispiel #3
0
    def update(self):
        if self.textboxes:

            for box in self.textboxes:
                box.stop_editing()
                box.detach()
            del self.textboxes

        self.textboxes = []
        renWin = self.interactor.GetRenderWindow()
        w, h = renWin.GetSize()
        dpi = renWin.GetDPI()
        cmap = vcs.getcolormap()

        prop = vtkTextProperty()
        vcs.vcs2vtk.prepTextProperty(prop, (w, h),
                                     to=self.text.To,
                                     tt=self.text.Tt,
                                     cmap=cmap)
        prop.SetOrientation(-1 * self.text.angle)

        for ind, x in enumerate(self.text.x):
            self.actors[ind].SetTextProperty(prop)

            y = self.text.y[ind]
            string = self.text.string[ind]

            text_width, text_height = text_dimensions(self.text, ind, (w, h),
                                                      dpi)

            x = x * w
            y = y * h

            box_prop = vtkTextProperty()
            vcs.vcs2vtk.prepTextProperty(box_prop, (w, h),
                                         to=self.text.To,
                                         tt=self.text.Tt,
                                         cmap=cmap)
            box_prop.SetOrientation(-1 * self.text.angle)
            text_color = box_prop.GetColor()
            highlight_color = vcs.vtk_ui.text.contrasting_color(*text_color)

            textbox = Textbox(self.interactor,
                              string,
                              highlight_color=highlight_color,
                              highlight_opacity=.8,
                              movable=True,
                              on_editing_end=self.finished_editing,
                              on_drag=self.moved_textbox,
                              textproperty=box_prop,
                              on_click=self.textbox_clicked)
            textbox.x = x
            textbox.y = y
            textbox.show()
            textbox.show_highlight()

            if ind == self.index:
                textbox.start_editing()

            self.textboxes.append(textbox)
Beispiel #4
0
def text_dimensions(text, index, winsize, dpi):
    prop = vtkTextProperty()
    vcs.vcs2vtk.prepTextProperty(
        prop,
        winsize,
        text.To,
        text.Tt,
        vcs.getcolormap())
    return vcs.vtk_ui.text.text_dimensions(text.string[index], prop, dpi)
Beispiel #5
0
 def colormap(self):
     """Used for the colormap select menu."""
     cmap = None
     if self._gm.colormap:
         cmap = self._gm.colormap
     elif self._canvas and self._canvas.getcolormap():
         cmap = self._canvas.getcolormapname()
     else:
         cmap = vcs._colorMap
     return vcs.getcolormap(cmap)
Beispiel #6
0
 def change_color(self, state):
     if self.picker:
         self.picker.make_current()
     else:
         self.picker = ColorPicker(500,
                                   500,
                                   vcs.getcolormap(),
                                   0,
                                   on_save=self.set_color,
                                   on_cancel=self.cancel_color)
Beispiel #7
0
 def colormap(self):
     """Used for the colormap select menu."""
     cmap = None
     if self._gm.colormap:
         cmap = self._gm.colormap
     elif self._canvas and self._canvas.getcolormap():
         cmap = self._canvas.getcolormapname()
     else:
         cmap = vcs._colorMap
     return vcs.getcolormap(cmap)
Beispiel #8
0
 def change_color(self, state):
     if self.picker:
         self.picker.make_current()
     else:
         self.picker = ColorPicker(500,
                                   500,
                                   vcs.getcolormap(),
                                   self.text.color,
                                   parent_interactor=self.interactor,
                                   on_save=self.set_color,
                                   on_cancel=self.cancel_color)
Beispiel #9
0
 def change_color(self, state):
     if self.picker:
         self.picker.make_current()
     else:
         self.picker = ColorPicker(
             500,
             500,
             vcs.getcolormap(),
             0,
             on_save=self.set_color,
             on_cancel=self.cancel_color)
Beispiel #10
0
 def change_color(self, state):
     if self.picker:
         self.picker.make_current()
     else:
         self.picker = vcs.colorpicker.ColorPicker(
             500,
             500,
             vcs.getcolormap(),
             self.tt.color,
             parent_interactor=self.interactor,
             on_save=self.set_color,
             on_cancel=self.cancel_color)
Beispiel #11
0
def set_colormap_of_graphics_method(
        canvas, parameter_colormap, method, parameters):
    if parameter_colormap is not '':
        cmap = get_colormap(parameter_colormap, parameters)
        if isinstance(cmap, str):
            if cmap in vcs.listelements('colormap'):
                method.colormap = vcs.getcolormap(cmap)
            else:
                method.colormap = vcs.matplotlib2vcs(cmap)
            cmap_range = get_color_range(method)
        else:
            # cmap is of type vcs.colormap.Cp
            cmap, cmap_range = cmap
            method.colormap = cmap
        colors = vcs.getcolors(method.levels, colors=cmap_range, white=[
                               100, 100, 100], split=0)
        method.fillareacolors = colors
Beispiel #12
0
    def update(self):
        if self.textboxes:

            for box in self.textboxes:
                box.stop_editing()
                box.detach()
            del self.textboxes

        self.textboxes = []
        renWin = self.interactor.GetRenderWindow()
        w, h = renWin.GetSize()
        dpi = renWin.GetDPI()
        cmap = vcs.getcolormap()

        prop = vtkTextProperty()
        vcs.vcs2vtk.prepTextProperty(prop, (w, h), to=self.text, tt=self.text, cmap=cmap)
        prop.SetOrientation(-1 * self.text.angle)

        for ind, x in enumerate(self.text.x):
            self.actors[ind].SetTextProperty(prop)

            y = self.text.y[ind]
            string = self.text.string[ind]

            text_width, text_height = text_dimensions(self.text, ind, (w, h),
                                                      dpi)

            x = x * w
            y = y * h

            box_prop = vtkTextProperty()
            vcs.vcs2vtk.prepTextProperty(box_prop, (w, h), to=self.text, tt=self.text, cmap=cmap)
            box_prop.SetOrientation(-1 * self.text.angle)
            text_color = box_prop.GetColor()
            highlight_color = vcs.vtk_ui.text.contrasting_color(*text_color)

            textbox = Textbox(self.interactor, string, highlight_color=highlight_color, highlight_opacity=.8, movable=True, on_editing_end=self.finished_editing, on_drag=self.moved_textbox, textproperty=box_prop, on_click=self.textbox_clicked)
            textbox.x = x
            textbox.y = y
            textbox.show()
            textbox.show_highlight()

            if ind == self.index:
                textbox.start_editing()

            self.textboxes.append(textbox)
Beispiel #13
0
def match_color(color,colormap=None):
    """
Function: cmatch_color                          # Returns the color in the colormap that is closet from the required color
Description of Function:
       Given a color (defined as rgb values -0/100 range- or a string name) and optionally a colormap name,
       returns the color number that is closet from the requested color
       (using rms difference between rgb values)
       if colormap is not map use the currently used colormap
Example of use:
       a=vcs.init()
       print vcs.match_color('salmon')
       print vcs.match_color('red')
       print vcs.match_color([0,0,100],'defaullt') # closest color from blue

"""
    # First gets the rgb values 
    if type(color)==type(''):
        vals=genutil.colors.str2rgb(color)
        vals[0]/=2.55
        vals[1]/=2.55
        vals[2]/=2.55
    else:
        vals=color

    # Now gets the colormap to look in
    if colormap is None: colormap=vcs.getcolormapname()
    cmap=vcs.getcolormap(colormap)

    # Now tries determines the min rms diff
    rmsmin=2.E40
    match=None
    for i in cmap.index.keys():
        col=cmap.index[i]
        rms=numpy.sqrt((vals[0]-col[0])**2+\
                         (vals[1]-col[1])**2+\
                         (vals[2]-col[2])**2 \
                         )
        if rms<rmsmin:
            rmsmin=rms
            match=i
    return match
Beispiel #14
0
    def process_click(self, text_index, x, y):

        if text_index == self.index:
            # Adjust cursor position
            self.textboxes[self.index].start_editing((x, y))
            return
        else:
            self.textboxes[self.index].stop_editing()
            if text_index is not None:
                # Change which one we're editing
                self.index = text_index
                self.textboxes[self.index].start_editing((x, y))
            else:
                if self.current_modifiers()["control"]:

                    self.textboxes[self.index].stop_editing()

                    # Add a new text item to self.text, update, and start
                    # editing
                    new_index = max(len(self.text.x),
                                    len(self.text.y),
                                    len(self.text.string))

                    self.text.x.append(x)
                    self.text.y.append(y)
                    self.text.string.append("Click to Edit")

                    new_actor = genTextActor(
                        self.actors[0].GetConsumer(0),
                        string=["Click to Edit"],
                        x=[x],
                        y=[y],
                        to=self.text,
                        tt=self.text,
                        cmap=vcs.getcolormap())[0]
                    new_actor.SetVisibility(0)
                    self.actors.append(new_actor)
                    self.index = new_index

                    self.update()
Beispiel #15
0
 def setcolormap(self, name, values):
     """Sets color values in a specified colormap"""
     name = str(name)
     cm = vcs.getcolormap(name)
     for i, value in enumerate(values):
         cm.setcolorcell(i, value[0], value[1], value[2], value[3])
Beispiel #16
0
    def draw_values(self, raveled, mesh, meshfill, template):
        # Values to use (data or user passed)
        if self.PLOT_SETTINGS.values.array is None:
            data = MV2.array(raveled)
        else:
            data = MV2.ravel(self.PLOT_SETTINGS.values.array)
        if isinstance(raveled, numpy.ma.core.MaskedArray):
            data.mask = data.mask + raveled.mask

        # Now remove masked values
        if data.mask is not numpy.ma.nomask:  # we have missing
            indices = numpy.argwhere(numpy.ma.logical_not(data.mask))
            data = data.take(indices).filled(0)[:, 0]
            M = mesh.filled()[indices][:, 0]
            raveled = raveled.take(indices).filled(0.)[:, 0]
        else:
            M = mesh.filled()

        # Baricenters
        xcenters = numpy.average(M[:, 1], axis=-1)
        ycenters = numpy.average(M[:, 0], axis=-1)
        self.PLOT_SETTINGS.values.text.viewport = [template.data.x1, template.data.x2,
                                                   template.data.y1, template.data.y2]
        if not numpy.allclose(meshfill.datawc_x1, 1.e20):
            self.PLOT_SETTINGS.values.text.worldcoordinate = [meshfill.datawc_x1,
                                                              meshfill.datawc_x2,
                                                              meshfill.datawc_y1,
                                                              meshfill.datawc_y2]
        else:
            self.PLOT_SETTINGS.values.text.worldcoordinate = [M[:, 1].min(),
                                                              M[:, 1].max(),
                                                              M[:, 0].min(),
                                                              M[:, 0].max()]

        self.PLOT_SETTINGS.values.text.string = [
            self.PLOT_SETTINGS.values.format.format(value) for value in data]

        # Now that we have the formatted values we need get the longest string
        lengths = [len(txt) for txt in self.PLOT_SETTINGS.values.text.string]
        longest = max(lengths)
        index = lengths.index(longest)

        tmptxt = vcs.createtext()
        tmptxt.string = self.PLOT_SETTINGS.values.text.string[index]
        tmptxt.x = xcenters[index]
        tmptxt.y = ycenters[index]
        smallY = M[index, 0, :].min()
        bigY = M[index, 0, :].max()
        smallX = M[index, 1, :].min()
        bigX = M[index, 1, :].max()
        tmptxt.worldcoordinate = self.PLOT_SETTINGS.values.text.worldcoordinate
        tmptxt.viewport = self.PLOT_SETTINGS.values.text.viewport
        # Now try to shrink until it fits
        extent = self.x.gettextextent(tmptxt)[0]
        while ((extent[1] - extent[0]) / (bigX - smallX) > 1.01 or
               (extent[3] - extent[2]) / (bigY - smallY) > 1.01) and \
                tmptxt.height >= 1:
            tmptxt.height -= 1
            extent = self.x.gettextextent(tmptxt)[0]
        self.PLOT_SETTINGS.values.text.height = tmptxt.height

        # Finally we need to split into two text objects for dark and light background
        # Step 1: figure out each bin color type (dark/light)
        colormap = self.x.colormap
        if colormap is None:
            colormap = vcs._colorMap
        cmap = vcs.getcolormap(colormap)
        colors = meshfill.fillareacolors
        dark_bins = [
            is_dark_color_type(
                *cmap.getcolorcell(color)) for color in colors]

        # Step 2: put values into bin (color where they land)
        bins = meshfill.levels[1:-1]
        binned = numpy.digitize(raveled, bins)
        isdark = [dark_bins[indx] for indx in binned]
        tmptxt = vcs.createtext(
            Tt_source=self.PLOT_SETTINGS.values.text.Tt_name,
            To_source=self.PLOT_SETTINGS.values.text.To_name)
        for pick, color in [(numpy.argwhere(isdark), self.PLOT_SETTINGS.values.lightcolor),
                            (numpy.argwhere(numpy.logical_not(isdark)), self.PLOT_SETTINGS.values.darkcolor)]:
            tmptxt.x = xcenters.take(pick)[:, 0].tolist()
            tmptxt.y = ycenters.take(pick)[:, 0].tolist()
            tmptxt.string = numpy.array(
                self.PLOT_SETTINGS.values.text.string).take(pick)[
                :, 0].tolist()
            tmptxt.color = color
            self.x.plot(tmptxt, bg=self.bg)
Beispiel #17
0
    def update(self):
        if self.textboxes:

            for box in self.textboxes:
                box.stop_editing()
                box.detach()
            del self.textboxes

        self.textboxes = []
        w, h = self.interactor.GetRenderWindow().GetSize()
        cmap = vcs.getcolormap()

        prop = vtkTextProperty()
        vcs.vcs2vtk.prepTextProperty(prop, (w, h),
                                     to=self.text,
                                     tt=self.text,
                                     cmap=cmap)
        prop.SetOrientation(-1 * self.text.angle)

        for ind, x in enumerate(self.text.x):
            self.actors[ind].SetTextProperty(prop)

            y = self.text.y[ind]
            string = self.text.string[ind]

            text_width, text_height = text_dimensions(self.text, ind, (w, h))
            x = x * w
            y = h - y * h  # mirror the y axis for widgets

            if self.text.halign in ("right", 2):
                x -= text_width
            elif self.text.halign in ("center", 1):
                x -= text_width / 2.0

            if self.text.valign in ("half", 2):
                y -= text_height / 2.0
            elif self.text.valign in ("top", 0):
                y -= text_height

            box_prop = vtkTextProperty()
            vcs.vcs2vtk.prepTextProperty(box_prop, (w, h),
                                         to=self.text,
                                         tt=self.text,
                                         cmap=cmap)
            box_prop.SetOrientation(-1 * self.text.angle)

            textbox = Textbox(self.interactor,
                              string,
                              left=x,
                              top=y,
                              movable=True,
                              on_editing_end=self.finished_editing,
                              on_drag=self.moved_textbox,
                              textproperty=box_prop,
                              on_click=self.textbox_clicked)
            textbox.show()

            if ind == self.index:
                textbox.start_editing()

            self.textboxes.append(textbox)
Beispiel #18
0
def get_cmaps():
    cmaps = {}
    for cmap in vcs.listelements('colormap'):
        index = vcs.getcolormap(cmap).index
        cmaps[cmap] = [vcs.getcolormap(cmap).getcolorcell(i) for i in range(256)]
    return cmaps
Beispiel #19
0
 def setcolormap(self, name, values):
     """Sets color values in a specified colormap"""
     name = str(name)
     cm = vcs.getcolormap(name)
     for i, value in enumerate(values):
         cm.setcolorcell(i, value[0], value[1], value[2], value[3])
Beispiel #20
0
 def getcolormap(self, name):
     """Returns the color values in a colormap"""
     name = str(name)
     return list(vcs.getcolormap(name).getindex().values())
Beispiel #21
0
def set_colormap_of_graphics_method(canvas, parameter_colormap, method):
    if parameter_colormap is not '':
        method.colormap = vcs.getcolormap(parameter_colormap)
        colors = vcs.getcolors(method.levels, colors=range(6, 240))
        method.fillareacolors = colors
Beispiel #22
0
 def getcolormap(self, name):
     """Returns the color values in a colormap"""
     name = str(name)
     return vcs.getcolormap(name).getindex().values()
Beispiel #23
0
    def __init__(self, width, height, colormap, color, parent_interactor=None, on_save=None, on_cancel=None):
        self.render_window = vtk.vtkRenderWindow()
        self.render_window.SetWindowName("Color Picker")
        self.render_window.SetNumberOfLayers(3)
        self.render_window.SetSize(width, height)
        self.color_renderer = vtk.vtkRenderer()
        self.parent_interactor = parent_interactor

        if colormap is None:
            colormap = "default"
        if not vcs.iscolormap(colormap):
            colormap = vcs.getcolormap(colormap)

        self.colormap = colormap
        self.colors =  [[int(c / 100.0 * 255.0) for c in colormap.index[i]] for i in range(len(colormap.index))]

        self.actor = make_color_plane(16, 16, self.colors)

        self.color_renderer.SetViewport([.1, .1, .9, .9])
        self.color_renderer.SetBackground((.5, .5, .5))
        self.color_renderer.AddActor(self.actor)
        self.color_renderer.InteractiveOff()
        self.color_renderer.SetLayer(1)

        bg = vtk.vtkRenderer()
        bg.SetBackground(1,1,1)
        bg.SetViewport([0,0,1,1])
        bg.SetLayer(0)
        self.render_window.AddRenderer(bg)
        self.render_window.AddRenderer(self.color_renderer)
        inter = vtk.vtkRenderWindowInteractor()

        self.style = vtk.vtkInteractorStyleUser()
        inter.SetInteractorStyle(self.style)
        inter.SetRenderWindow(self.render_window)
        manager = vcs.vtk_ui.manager.get_manager(inter)
        self.render_window.AddRenderer(manager.renderer)
        self.render_window.AddRenderer(manager.actor_renderer)
        manager.elevate()
        self.render_window.Render()

        self.on_save = on_save
        self.on_cancel = on_cancel

        maps = vcs.elements["colormap"]

        current_state = None
        states = []
        self.colormaps = []
        for ind, mapname in enumerate(maps.keys()):
            states.append(ButtonState(label=mapname))
            if colormap.name == mapname:
                current_state = ind
            self.colormaps.append(maps[mapname])

        self.colormap_button = Button(inter, states=states, action=self.change_map, left = 10, top=10)
        self.colormap_button.set_state(current_state)

        self.save_button = Button(inter, action=self.save, label="Choose Color", left=int(width * .75) - 10, top=int(height * .85))
        self.cancel_button = Button(inter, action=self.cancel, label="Cancel", left=10, top=int(height * .85))

        self.colormap_button.show()
        self.save_button.show()
        self.cancel_button.show()

        self.make_current()

        self.selectedMapper = vtk.vtkDataSetMapper()
        self.selectedActor = vtk.vtkActor()
        self.selectedActor.SetMapper(self.selectedMapper);
        self.selectedActor.GetProperty().EdgeVisibilityOn();
        self.selectedActor.GetProperty().SetEdgeColor(0,0,0);
        self.selectedActor.GetProperty().SetLineWidth(3);
        self.color = color
        # Make sure the current color is selected
        self.selectCell(color)
        self.color_renderer.AddActor(self.selectedActor)
        self.click_handler = inter.AddObserver(vtk.vtkCommand.LeftButtonReleaseEvent, self.clickEvent)
        def noop(obj, event):
            pass
        self.char_handler = inter.AddObserver(vtk.vtkCommand.CharEvent, noop)
Beispiel #24
0
 def cmap(self, value):
     if not vcs.iscolormap(value):
         value = vcs.getcolormap(value)
     self._real_map = value
     self._cmap = vcs.createcolormap(Cp_name_src=self._real_map.name)
     self.update_table()
Beispiel #25
0
    def __init__(self,
                 width,
                 height,
                 colormap,
                 color,
                 parent_interactor=None,
                 on_save=None,
                 on_cancel=None):
        self.render_window = vtk.vtkRenderWindow()
        self.render_window.SetWindowName("Color Picker")
        self.render_window.SetNumberOfLayers(3)
        self.render_window.SetSize(width, height)
        self.color_renderer = vtk.vtkRenderer()
        self.parent_interactor = parent_interactor

        if colormap is None:
            colormap = "default"
        if not vcs.iscolormap(colormap):
            colormap = vcs.getcolormap(colormap)

        self.colormap = colormap
        self.colors = [[int(c / 100.0 * 255.0) for c in colormap.index[i]]
                       for i in range(len(colormap.index))]

        self.actor = make_color_plane(16, 16, self.colors)

        self.color_renderer.SetViewport([.1, .1, .9, .9])
        self.color_renderer.SetBackground((.5, .5, .5))
        self.color_renderer.AddActor(self.actor)
        self.color_renderer.InteractiveOff()
        self.color_renderer.SetLayer(1)

        bg = vtk.vtkRenderer()
        bg.SetBackground(1, 1, 1)
        bg.SetViewport([0, 0, 1, 1])
        bg.SetLayer(0)
        self.render_window.AddRenderer(bg)
        self.render_window.AddRenderer(self.color_renderer)
        inter = vtk.vtkRenderWindowInteractor()

        self.style = vtk.vtkInteractorStyleUser()
        inter.SetInteractorStyle(self.style)
        inter.SetRenderWindow(self.render_window)
        manager = vcs.vtk_ui.manager.get_manager(inter)
        self.render_window.AddRenderer(manager.renderer)
        self.render_window.AddRenderer(manager.actor_renderer)
        manager.elevate()
        self.render_window.Render()

        self.on_save = on_save
        self.on_cancel = on_cancel

        maps = vcs.elements["colormap"]

        current_state = None
        states = []
        self.colormaps = []
        for ind, mapname in enumerate(maps.keys()):
            states.append(ButtonState(label=mapname))
            if colormap.name == mapname:
                current_state = ind
            self.colormaps.append(maps[mapname])

        self.colormap_button = Button(inter,
                                      states=states,
                                      action=self.change_map,
                                      left=10,
                                      top=10)
        self.colormap_button.set_state(current_state)

        self.save_button = Button(inter,
                                  action=self.save,
                                  label="Choose Color",
                                  left=int(width * .75) - 10,
                                  top=int(height * .85))
        self.cancel_button = Button(inter,
                                    action=self.cancel,
                                    label="Cancel",
                                    left=10,
                                    top=int(height * .85))

        self.colormap_button.show()
        self.save_button.show()
        self.cancel_button.show()

        self.make_current()

        self.selectedMapper = vtk.vtkDataSetMapper()
        self.selectedActor = vtk.vtkActor()
        self.selectedActor.SetMapper(self.selectedMapper)
        self.selectedActor.GetProperty().EdgeVisibilityOn()
        self.selectedActor.GetProperty().SetEdgeColor(0, 0, 0)
        self.selectedActor.GetProperty().SetLineWidth(3)
        self.color = color
        # Make sure the current color is selected
        self.selectCell(color)
        self.color_renderer.AddActor(self.selectedActor)
        self.click_handler = inter.AddObserver(
            vtk.vtkCommand.LeftButtonReleaseEvent, self.clickEvent)

        def noop(obj, event):
            pass

        self.char_handler = inter.AddObserver(vtk.vtkCommand.CharEvent, noop)
Beispiel #26
0
def getcolorcell(cell,obj=None):
  if obj is None:
    cmap = vcs.getcolormap()
  else:
    cmap = vcs.getcolormap(obj.colormap)
  return cmap.index[cell]