Color and ColorMap¶
All handling of colors in Plums plot rely on the special Color container class to manipulate colors
and a set of ColorMap classes to generate them.
The Color class¶
The way colors are handled in Plums plot depends on the high-level
Colorspacious color handling library and the Color
class is a simple container class which delegates all the heavy lifting to colorspacious.
It offers a typed handling of colors and introduces the concept of ctype which
represents a color space in which a Color instances live, and thus indicates how its
components should be interpreted.
Internally, the actual Color components are stored in the limited but highly common sRGB255 space.
-
class
plums.plot.engine.Color(*components, ctype='sRGB255')[source]¶ Bases:
objectA class representing a color in various spaces with distance and arithmetic.
It is implemented in a immutable fashion, i.e:
The
componentsproperty is passive and always returns theColorinctypespace.The
astype(ctype)method changes the configured ctype in a new, separateColorinstance copy.
To sum-up, this means that the
Colorclass acts like an “intelligent” container of some sort which always lives in the same color space (the one it was created in) and may spawn copies of itself in new color spaces.See also
The current implementation is merely an oriented-object wrapper around the
colorspaciouslibrary with a convenient “typed” container interface. For more information on the inner workings of color conversion and perceptual theory, please see the colorspacious documentation- Parameters
*components (int, float) – The
Colorcomponents in color spacectype.ctype (str) – Optional. Default to
sRGB255. A color space string specifying in which spacecomponentslies.
-
property
ctype¶ The color space the
Colorlives in (e.g. “sRGB255” or “JCh”).See also
For more information on valid color spaces, please see the colorspacious documentation
- Type
-
astype(ctype)[source]¶ Make a new
Colorinstance in a new colors space.The returned
Colorinstance will share the same perceptual properties (e.g. the same “coordinates” of some sort) but the returnedcomponentswill live in a new color space.Note that this is a lazy copy operation and that the actual
ctypeconversion will only be performed during the first time thecomponentsattribute is accessed.- Parameters
ctype (str) – A color space string specifying in which space
componentslies.- Returns
The converted
Colorinstance.- Return type
Note
Nothing is done to take care of “out-of-bounds” values which may occur during conversion to avoid polluting eventual future conversion [2]_.
Color Maps¶
A ColorMap performs a link between a set \(\mathcal{V} \subset \mathbb{R}\) of value and a set
\(\mathcal{C}^S \subset S\) of colors (where \(S\) is a given color space).
Note that both sets might be infinite, in which case we construct a ContinuousColorMap, or finite, in which case we
construct a DiscreteColorMap.
-
class
plums.plot.engine.ColorMap(ctype='sRGB255', map_fn=None)[source]¶ Bases:
abc.ABCAn abstract base class for all
ColorMapclasses.Subclass must implement the private
_get_color()method which maps a givenvalueto aColorinstance.- Parameters
-
map_fn¶ A function (ideally continuous and monotonically increasing) which (ideally bijectivelly) maps input values to a working range.
- Type
Callable
-
property
range¶ The input range supported by the
ColorMap.Warning
The range returned corresponds to the input range mapped through
map_fn- Type
-
__call__(array, keep_colors=False)[source]¶ Generalizes
get_color()onndarrayinputs.Note that because on the non-vectorized nature of color maps, this is not guaranteed to be a fast operation.
It is however included to allow for readable color mapping on arrays.
-
astype(ctype)[source]¶ Make a new
ColorMapinstance in a new colors space.The returned
ColorMapinstance will share the same perceptual properties (e.g. the same “coordinates” of some sort) but the returnedcomponentswill live in a new color space.Note that this is a lazy copy operation and that the actual
ctypeconversion will only be performed during the first time theget_color()attribute is called.- Parameters
ctype (str) – A color space string specifying in which space returned
Colorlie.- Returns
The converted
ColorMapinstance.- Return type
Note
Nothing is done to take care of “out-of-bounds” values which may occur during conversion to avoid polluting eventual future conversion [2]_.