def set_transfer_function(self, colors=None, opacities=None): """ Update the volume mapper's transfer function. """ lerp = lambda x: self.vmin + x * (self.vmax - self.vmin) # noqa if colors is not None: self.colors = colors if opacities is not None: self.opacities = opacities color_tf = tvtk.ColorTransferFunction() for color in self.colors.values(): color_tf.add_rgb_point(lerp(color[0]), *(color[1:])) opacity_tf = tvtk.PiecewiseFunction() alphas = self.opacities.values() for i, alpha in enumerate(alphas): x = alpha[0] if i > 0: # Look back one item. VTK doesn't like exact vertical jumps, so # we need to jog a value that is exactly equal by a little bit. if alphas[i-1][0] == alpha[0]: x += 1e-8 opacity_tf.add_point(lerp(x), alpha[1] * self.global_alpha) self._set_volume_ctf(color_tf, opacity_tf)
def __init__(self, num_entries): self.size = num_entries self.table = tvtk.ColorTransferFunction() try: self.table.range = (0.0, 1.0) except Exception: # VTK versions < 5.2 don't seem to need this. pass self.alpha = tvtk.PiecewiseFunction() # These VTK classes perform the interpolation for us. # insert the control points for the left and the right end of the # gradient. These are fixed (i.e. cannot be moved or deleted) and # allow one to set begin and end colors. left_control_point = ColorControlPoint(fixed=True, active_channels="hsva") left_control_point.set_pos(0.0) left_control_point.color.set_rgb(0.0, 0.0, 0.0) right_control_point = ColorControlPoint(fixed=True, active_channels="hsva") right_control_point.set_pos(1.0) right_control_point.color.set_rgb(1.0, 1.0, 1.0) self.control_points = [left_control_point, right_control_point] # note: The array of control points always has to be sorted by gradient # position of the control points. # insert another control point. This one has no real function, it # is just there to make the gradient editor more colorful initially # and suggest to the (first time user) that it is actually possible to # place more control points. mid_control_point = ColorControlPoint(active_channels="hsv") mid_control_point.set_pos(0.4) mid_control_point.color.set_rgb(1.0, 0.4, 0.0) self.insert_control_point(mid_control_point) # These variables are only for compatibility with GradientTableOld. self.scaling_function_string = "" # will receive the function string if # set, e.g. "x**(4*a)" self.scaling_function_parameter = 0.5 # the parameter a, slider controlled self.scaling_function = None # the actual function object. takes one # position parameter. None if disabled. self.update()