Interpolate an ordered set of RGB color samples.
Input values are clamped to the unit interval.
Source code in hakowan/common/colormap/colormap.py
| class ColorMap:
"""Interpolate an ordered set of RGB color samples.
Input values are clamped to the unit interval.
"""
def __init__(self, samples: npt.NDArray):
"""Construct color map from color samples.
Args:
samples: A numpy array of shape (n, 3). Each row is a color sample.
"""
assert len(samples) >= 2, "Color map must have at least 2 samples."
self.samples = samples
def __call__(self, value: float):
"""Evaluate color map at a value between 0 and 1.
Args:
value: A value between 0 and 1.
Returns:
(Color): The interpolated color.
"""
value = max(0.0, min(1.0, value))
n = len(self.samples) - 1
i0 = math.floor(n * value)
i1 = math.ceil(n * value)
t = n * value - i0
c0 = self.samples[i0]
c1 = self.samples[i1]
c = c0 * (1 - t) + c1 * t
return Color(*c)
def num_colors(self):
"""Return the number of stored color samples."""
return len(self.samples)
def reversed(self):
"""Return a new color map with the sample order reversed.
Returns:
(ColorMap): A color map mapping 0->last color and 1->first color.
"""
return ColorMap(self.samples[::-1])
|
__init__(samples)
Construct color map from color samples.
Parameters:
| Name |
Type |
Description |
Default |
samples
|
NDArray
|
A numpy array of shape (n, 3). Each row is a color sample.
|
required
|
Source code in hakowan/common/colormap/colormap.py
| def __init__(self, samples: npt.NDArray):
"""Construct color map from color samples.
Args:
samples: A numpy array of shape (n, 3). Each row is a color sample.
"""
assert len(samples) >= 2, "Color map must have at least 2 samples."
self.samples = samples
|
__call__(value)
Evaluate color map at a value between 0 and 1.
Parameters:
| Name |
Type |
Description |
Default |
value
|
float
|
|
required
|
Returns:
Source code in hakowan/common/colormap/colormap.py
| def __call__(self, value: float):
"""Evaluate color map at a value between 0 and 1.
Args:
value: A value between 0 and 1.
Returns:
(Color): The interpolated color.
"""
value = max(0.0, min(1.0, value))
n = len(self.samples) - 1
i0 = math.floor(n * value)
i1 = math.ceil(n * value)
t = n * value - i0
c0 = self.samples[i0]
c1 = self.samples[i1]
c = c0 * (1 - t) + c1 * t
return Color(*c)
|
num_colors()
Return the number of stored color samples.
Source code in hakowan/common/colormap/colormap.py
| def num_colors(self):
"""Return the number of stored color samples."""
return len(self.samples)
|
reversed()
Return a new color map with the sample order reversed.
Returns:
| Type |
Description |
ColorMap
|
A color map mapping 0->last color and 1->first color.
|
Source code in hakowan/common/colormap/colormap.py
| def reversed(self):
"""Return a new color map with the sample order reversed.
Returns:
(ColorMap): A color map mapping 0->last color and 1->first color.
"""
return ColorMap(self.samples[::-1])
|