Skip to content

Commit d085c10

Browse files
authored
docs: make colormap attributes more visible (#56)
1 parent 7b10867 commit d085c10

File tree

5 files changed

+94
-15
lines changed

5 files changed

+94
-15
lines changed

docs/api/colormap.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
::: cmap.Colormap
44
options:
55
filters: ['(__call__|^[^_])']
6+
7+
::: cmap.ColorStops
8+
9+
::: cmap._colormap.ColorStop

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ plugins:
102102
annotations_path: "source" # default is 'brief'
103103
show_bases: false # default is true
104104
show_source: false # default is true
105+
docstring_section_style: list
106+
merge_init_into_class: true
105107

106108
extra_css:
107109
- stylesheets/extra.css

src/cmap/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
from ._color import HSLA, HSVA, RGBA, RGBA8, Color
13-
from ._colormap import Colormap
13+
from ._colormap import Colormap, ColorStops
1414

1515
if TYPE_CHECKING:
1616
from ._catalog import CatalogItem
@@ -62,10 +62,11 @@ def resolve(self, name: str) -> str:
6262
from ._catalog import Catalog, CatalogItem
6363

6464
__all__ = [
65+
"Catalog",
66+
"CatalogItem",
6567
"Color",
6668
"Colormap",
67-
"CatalogItem",
68-
"Catalog",
69+
"ColorStops",
6970
"HSLA",
7071
"HSVA",
7172
"RGBA",

src/cmap/_colormap.py

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ class ColormapDict(TypedDict):
6565
class Colormap:
6666
"""A colormap is a mapping from scalar values to RGB(A) colors.
6767
68+
The primary way to apply a colormap to data is to call the colormap object with
69+
scalar values, which will return an array of RGBA colors. See
70+
[`Colormap.__call__`][cmap.Colormap.__call__] for details.
71+
6872
Parameters
6973
----------
7074
value : Color | ColorStop | Iterable[Color | ColorStop] | dict[float, Color]
@@ -131,7 +135,65 @@ class Colormap:
131135
"__weakref__",
132136
)
133137

134-
#: ColorStops dett
138+
color_stops: ColorStops
139+
"""The color stops in the colormap.
140+
141+
This object is a sequence of color stops: a color
142+
(RGBA) and a scalar position (0-1) in the gradient. This is the primary data
143+
structure used to represent the colormap. See [`ColorStops`][cmap.ColorStops]
144+
docstring for more information.
145+
"""
146+
147+
name: str
148+
"""A display name for the colormap.
149+
150+
If not provided explicitly, and if the colormap is a catalog colormap, this
151+
will be set to `"namespace:name"`. If not a catalog colormap, it will be set
152+
to the `identifier`, or fallback to `"custom colormap"`.
153+
"""
154+
155+
identifier: str
156+
"""An identifier for the colormaps.
157+
158+
If not provided, it will be generated from `name` by discarding any
159+
characters that are not alphanumeric, spaces, dashes, or underscores, converting
160+
to lowercase, replacing spaces and dashes with underscores, and prefacing with
161+
`"_"` if the first character is a digit.
162+
"""
163+
164+
category: str | None
165+
"""An optional category for the colormap.
166+
167+
If not provided, and if the colormap is a catalog colormap, this will be set to
168+
the category of the colormap.
169+
"""
170+
171+
interpolation: Interpolation
172+
"""The interpolation mode to use when mapping scalar values to colors.
173+
174+
Either `"linear"` or `"nearest"`, where `"linear"` is the default.
175+
"""
176+
177+
under_color: Color | None
178+
"""A color to use for values below 0 when converting scalars to colors.
179+
180+
If provided, and `Colormap.lut` is called with `with_over_under=True`, `under_color`
181+
will be the third-to-last color in the LUT (`lut[-3]`).
182+
"""
183+
184+
over_color: Color | None
185+
"""A color to use for values above 1 when converting scalars to colors.
186+
187+
If provided, and `Colormap.lut` is called with `with_over_under=True`, `over_color`
188+
will be the second-to-last color in the LUT (`lut[-2]`).
189+
"""
190+
191+
bad_color: Color | None
192+
"""A color to use for missing/bad values when converting scalars to colors.
193+
194+
If provided, and `Colormap.lut` is called with `with_over_under=True`, `bad_color`
195+
will be the last color in the LUT (`lut[-1]`).
196+
"""
135197

136198
_catalog_instance: Catalog | None = None
137199

@@ -197,12 +259,12 @@ def __init__(
197259
if not name:
198260
name = value if isinstance(value, str) else "custom colormap"
199261

200-
self.interpolation: Interpolation = _norm_interp(interpolation)
262+
self.interpolation = _norm_interp(interpolation)
201263
stops._interpolation = self.interpolation
202-
self.color_stops: ColorStops = stops
203-
self.name: str = name
204-
self.identifier: str = _make_identifier(identifier or name)
205-
self.category: str | None = category
264+
self.color_stops = stops
265+
self.name = name
266+
self.identifier = _make_identifier(identifier or name)
267+
self.category = category
206268

207269
self.under_color = None if under is None else Color(under)
208270
self.over_color = None if over is None else Color(over)
@@ -695,7 +757,10 @@ def to_pyqtgraph(self) -> pyqtgraph.ColorMap:
695757

696758

697759
class ColorStop(NamedTuple):
698-
"""A color stop in a color gradient."""
760+
"""A color stop in a color gradient.
761+
762+
Just a named tuple with a `position` (`float`) and a `color` (`cmap.Color`).
763+
"""
699764

700765
position: float
701766
color: Color
@@ -789,8 +854,15 @@ def _call_lut_func(self, X: np.ndarray) -> np.ndarray:
789854
def parse(cls, colors: ColorStopsLike) -> ColorStops:
790855
"""Parse any colorstops-like object into a ColorStops object.
791856
792-
This is the more flexible constructor.
793-
see `_parse_colorstops` docstring for details.
857+
Each item in `colors` can be a color, or a 2-tuple of (position, color), where
858+
position (the "stop" along a color gradient) is a float between 0 and 1. Where
859+
not provided, color positions will be evenly distributed between neighboring
860+
specified positions (if `fill_mode` is 'neighboring') or will be replaced with
861+
`index / (len(colors)-1)` (if `fill_mode` is 'fractional').
862+
863+
Colors can be expressed as anything that can be converted to a Color, including
864+
a string, or 3/4-sequence of RGB(A) values.
865+
794866
795867
Parameters
796868
----------

src/cmap/_external.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def to_mpl(
4949
raise
5050

5151
return mpl_cm.with_extremes(
52-
bad=cm.bad_color, # type: ignore
53-
over=cm.over_color, # type: ignore
54-
under=cm.under_color, # type: ignore
52+
bad=cm.bad_color,
53+
over=cm.over_color,
54+
under=cm.under_color,
5555
)
5656

5757

0 commit comments

Comments
 (0)