Michael and Andre, thanks for the feedback and the pointers. I wrote a
"discretepalette" tonight and put it in color.py. It works fine, and
it's simple. Might you be interested in adding it to the repository?
It surely needs error handling, docs, different color spaces, etc.,
but the basic idea is there, which is:

discretepalette takes one argument, a dictionary, with the following
structure:
- Index to the dictionary is the number of colors (n_indicies);
- Result of indexing into the dictionary is another dictionary, this
  time indexed by color index (1-n);
- Result of indexing into this sub-dictionary is a third dictionary,
  with 'r', 'g', and 'b' entries, each a float from 0.0-1.0.

Then the "select" call just uses index and n_indices to look up the
right color. Here's the discretepalette code:

class discretepalette(palette):

    """discretepalette needs description"""

    def __init__(self, colors):
        palette.__init__(self)
        self.mycolors = colors

    def select(self, index, n_indices):
        """return a color corresponding to an index out of n_indices"""
        return rgb(self.mycolors[n_indices][index+1]['r'],
                   self.mycolors[n_indices][index+1]['g'],
                   self.mycolors[n_indices][index+1]['b'])

And here's a palette that I also put in color.py that works:

palette.Pastel2        = discretepalette(
                    {3: {1: {'r': 0.70196078431372544, 'b': 
0.80392156862745101, 'g': 0.88627450980392153},
                         3: {'r': 0.79607843137254897, 'b': 
0.90980392156862744, 'g': 0.83529411764705885}, 
                         2: {'r': 0.99215686274509807, 'b': 
0.67450980392156867, 'g': 0.80392156862745101}}, 
                     4: {1: {'r': 0.70196078431372544, 'b': 
0.80392156862745101, 'g': 0.88627450980392153}, 
                         3: {'r': 0.79607843137254897, 'b': 
0.90980392156862744, 'g': 0.83529411764705885}, 
                         2: {'r': 0.99215686274509807, 'b': 
0.67450980392156867, 'g': 0.80392156862745101}, 
                         4: {'r': 0.95686274509803926, 'b': 
0.89411764705882357, 'g': 0.792156862745098}}, 
                     ...

Then using the palette is just like any other palette:

rotatingstyle = graph.style.symbol(
  graph.style.symbol.changesquare,
  symbolattrs=[deco.filled, color.palette.Pastel1])

rotatinglinestyle = graph.style.line(
  [style.linewidth.THIck, 
   style.linestyle.solid,
   color.palette.Accent])

I'd be happy to add all the colorbrewer palettes if there was
interest, but it would need the discretepalette support for that.

Feedback? It's simple code which is nice. :)

JDO


_______________________________________________
PyX-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyx-user

Reply via email to