Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Giving the color treatment to the other plots #40

Merged
merged 1 commit into from

3 participants

@cfarrow
Owner

This implements effective colors described in #23 to line, multi-line and scatter plots.

@WarrenWeckesser

Works for me.

@nmichaud

All the affected examples run fine. Merging

@nmichaud nmichaud merged commit e93249a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 28, 2011
  1. @cfarrow
This page is out of date. Refresh to see the latest.
View
17 chaco/lineplot.py
@@ -12,7 +12,7 @@
# Enthought library imports
from enable.api import black_color_trait, ColorTrait, LineStyle
-from traits.api import Enum, Float, List, Str
+from traits.api import Enum, Float, List, Str, Property, Tuple, cached_property
from traitsui.api import Item, View
# Local relative imports
@@ -32,6 +32,11 @@ class LinePlot(BaseXYPlot):
# The color of the line.
color = black_color_trait
+ # The RGBA tuple for rendering lines. It is always a tuple of length 4.
+ # It has the same RGB values as color_, and its alpha value is the alpha
+ # value of self.color multiplied by self.alpha.
+ effective_color = Property(Tuple, depends_on=['line_color', 'alpha'])
+
# The color to use to highlight the line when selected.
selected_color = ColorTrait("lightyellow")
@@ -310,7 +315,7 @@ def _render(self, gc, points, selected_points=None):
render(gc, selected_points, self.orientation)
# Render using the normal style
- gc.set_stroke_color(self.color_)
+ gc.set_stroke_color(self.effective_color)
gc.set_line_width(self.line_width)
gc.set_line_dash(self.line_style_)
render(gc, points, self.orientation)
@@ -356,7 +361,7 @@ def _render_connected_hold(cls, gc, points, orientation):
def _render_icon(self, gc, x, y, width, height):
with gc:
- gc.set_stroke_color(self.color_)
+ gc.set_stroke_color(self.effective_color)
gc.set_line_width(self.line_width)
gc.set_line_dash(self.line_style_)
gc.set_antialias(0)
@@ -384,7 +389,6 @@ def _downsample_vectorized(self):
return
def _alpha_changed(self):
- self.color_ = self.color_[0:3] + (self.alpha,)
self.invalidate_draw()
self.request_redraw()
return
@@ -412,5 +416,10 @@ def __getstate__(self):
return state
+ @cached_property
+ def _get_effective_color(self):
+ alpha = self.color_[-1] if len(self.color_) == 4 else 1
+ c = self.color_[:3] + (alpha * self.alpha,)
+ return c
# EOF
View
7 chaco/multi_line_plot.py
@@ -407,6 +407,9 @@ def _render(self, gc, line_points, selected_points=None):
# Note: the list is reversed for testing with _render_filled.
for k, points in reversed(tmp):
color = color_func(k)
+ # Apply the alpha
+ alpha = color[-1] if len(color) == 4 else 1
+ color = color[:3] + (alpha * self.alpha,)
gc.set_stroke_color(color)
gc.set_line_width(self.line_width)
gc.set_line_dash(self.line_style_)
@@ -436,14 +439,10 @@ def _render_icon(self, gc, x, y, width, height):
def _alpha_changed(self):
- self.color_ = self.color_[0:3] + (self.alpha,)
self.invalidate_draw()
self.request_redraw()
return
- def _default_color(self):
- return black_color_trait
-
def _color_changed(self):
self.invalidate_draw()
self.request_redraw()
View
42 chaco/scatterplot.py
@@ -11,7 +11,8 @@
from enable.api import black_color_trait, ColorTrait, AbstractMarker, \
CustomMarker, MarkerNameDict, MarkerTrait
from kiva.constants import STROKE
-from traits.api import Any, Array, Bool, Float, Int, Trait, Callable
+from traits.api import Any, Array, Bool, Float, Trait, Callable, Property, \
+ Tuple, cached_property
from traitsui.api import View, VGroup, Item
# Local relative imports
@@ -181,6 +182,17 @@ class ScatterPlot(BaseXYPlot):
# The color of the outline to draw around the marker.
outline_color = black_color_trait
+ # The RGBA tuple for rendering lines. It is always a tuple of length 4.
+ # It has the same RGB values as color_, and its alpha value is the alpha
+ # value of self.color multiplied by self.alpha.
+ effective_color = Property(Tuple, depends_on=['color', 'alpha'])
+
+ # The RGBA tuple for rendering the fill. It is always a tuple of length 4.
+ # It has the same RGB values as outline_color_, and its alpha value is the
+ # alpha value of self.outline_color multiplied by self.alpha.
+ effective_outline_color = Property(Tuple, depends_on=['outline_color', 'alpha'])
+
+
# Traits UI View for customizing the plot.
traits_view = ScatterPlotView()
@@ -457,8 +469,9 @@ def _render(self, gc, points, icon_mode=False):
gc.clip_to_rect(self.x, self.y, self.width, self.height)
self.render_markers_func(gc, points, self.marker, self.marker_size,
- self.color_, self.line_width, self.outline_color_,
- self.custom_symbol)
+ self.effective_color, self.line_width,
+ self.effective_outline_color,
+ self.custom_symbol)
if self.show_selection and self._cached_selected_pts is not None and len(self._cached_selected_pts) > 0:
sel_pts = self.map_screen(self._cached_selected_pts)
@@ -483,8 +496,6 @@ def _render_icon(self, gc, x, y, width, height):
#------------------------------------------------------------------------
def _alpha_changed(self):
- self.color_ = self.color_[0:3] + (self.alpha,)
- self.outline_color_ = self.outline_color_[0:3] + (self.alpha,)
self.invalidate_draw()
self.request_redraw()
@@ -513,5 +524,26 @@ def _either_metadata_changed(self):
self.invalidate_draw()
self.request_redraw()
+ #------------------------------------------------------------------------
+ # Properties
+ #------------------------------------------------------------------------
+
+ @cached_property
+ def _get_effective_color(self):
+ if len(self.color_) == 4:
+ edge_alpha = self.color_[-1]
+ else:
+ edge_alpha = 1.0
+ c = self.color_[:3] + (edge_alpha * self.alpha,)
+ return c
+
+ @cached_property
+ def _get_effective_outline_color(self):
+ if len(self.outline_color_) == 4:
+ edge_alpha = self.outline_color_[-1]
+ else:
+ edge_alpha = 1.0
+ c = self.outline_color_[:3] + (edge_alpha * self.alpha,)
+ return c
# EOF
Something went wrong with that request. Please try again.