11"""
2- A class for converting color arguments to RGB or RGBA
3-
4- This class instantiates a single instance colorConverter that is used
5- to convert matlab color strings to RGB. RGB is a tuple of float RGB
6- values in the range 0-1.
2+ A module for converting numbers or color arguments to *RGB* or *RGBA*
3+
4+ *RGB* and *RGBA* are sequences of, respectively, 3 or 4 floats in the
5+ range 0-1.
6+
7+ This module includes functions and classes for color specification
8+ conversions, and for mapping numbers to colors in a 1-D array of
9+ colors called a colormap. Colormapping typically involves two steps:
10+ a data array is first mapped onto the range 0-1 using an instance
11+ of :class:`Normalize` or of a subclass; then this number in the 0-1
12+ range is mapped to a color using an instance of a subclass of
13+ :class:`Colormap`. Two are provided here:
14+ :class:`LinearSegmentedColormap`, which is used to generate all
15+ the built-in colormap instances, but is also useful for making
16+ custom colormaps, and :class:`ListedColormap`, which is used for
17+ generating a custom colormap from a list of color specifications.
18+
19+ The module also provides a single instance, *colorConverter*, of the
20+ :class:`ColorConverter` class providing methods for converting single
21+ color specifications or sequences of them to *RGB* or *RGBA*.
722
823Commands which take color arguments can use several formats to specify
924the colors. For the basic builtin colors, you can use a single letter
193208 cnames [k ] = v
194209
195210def is_color_like (c ):
211+ 'Return *True* if *c* can be converted to *RGB*'
196212 try :
197213 colorConverter .to_rgb (c )
198214 return True
@@ -218,6 +234,15 @@ def hex2color(s):
218234 return tuple ([int (n , 16 )/ 255.0 for n in (s [1 :3 ], s [3 :5 ], s [5 :7 ])])
219235
220236class ColorConverter :
237+ """
238+ Provides methods for converting color specifications to *RGB* or *RGBA*
239+
240+ Caching is used for more efficient conversion upon repeated calls
241+ with the same argument.
242+
243+ Ordinarily only the single instance instantiated in this module,
244+ *colorConverter*, is needed.
245+ """
221246 colors = {
222247 'b' : (0.0 , 0.0 , 1.0 ),
223248 'g' : (0.0 , 0.5 , 0.0 ),
@@ -526,17 +551,47 @@ def is_gray(self):
526551class LinearSegmentedColormap (Colormap ):
527552 """Colormap objects based on lookup tables using linear segments.
528553
529- The lookup transfer function is a simple linear function between
530- defined intensities. There is no limit to the number of segments
531- that may be defined. Though as the segment intervals start containing
532- fewer and fewer array locations, there will be inevitable quantization
533- errors
554+ The lookup table is generated using linear interpolation for each
555+ primary color, with the 0-1 domain divided into any number of
556+ segments.
534557 """
535558 def __init__ (self , name , segmentdata , N = 256 ):
536559 """Create color map from linear mapping segments
537560
538561 segmentdata argument is a dictionary with a red, green and blue
539- entries. Each entry should be a list of x, y0, y1 tuples.
562+ entries. Each entry should be a list of *x*, *y0*, *y1* tuples,
563+ forming rows in a table.
564+
565+ Example: suppose you want red to increase from 0 to 1 over
566+ the bottom half, green to do the same over the middle half,
567+ and blue over the top half. Then you would use::
568+
569+ cdict = {'red': [(0.0, 0.0, 0.0),
570+ (0.5, 1.0, 1.0),
571+ (1.0, 1.0, 1.0)],
572+
573+ 'green': [(0.0, 0.0, 0.0),
574+ (0.25, 0.0, 0.0),
575+ (0.75, 1.0, 1.0),
576+ (1.0, 1.0, 1.0)],
577+
578+ 'blue': [(0.0, 0.0, 0.0),
579+ (0.5, 0.0, 0.0),
580+ (1.0, 1.0, 1.0)]}
581+
582+ Each row in the table for a given color is a sequence of
583+ *x*, *y0*, *y1* tuples. In each sequence, *x* must increase
584+ monotonically from 0 to 1. For any input value *z* falling
585+ between *x[i]* and *x[i+1]*, the output value of a given color
586+ will be linearly interpolated between *y1[i]* and *y0[i+1]*::
587+
588+ row i: x y0 y1
589+ /
590+ /
591+ row i+1: x y0 y1
592+
593+ Hence y0 in the first row and y1 in the last row are never used.
594+
540595
541596 .. seealso::
542597 :func:`makeMappingArray`
0 commit comments