Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit de45cf8

Browse files
committed
Deprecate BaseRGBColor.clamped_rgb_{r,g,b} property in favor BaseRGBColor.clamped()
1 parent 57b1b65 commit de45cf8

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

colormath/color_objects.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -635,37 +635,53 @@ def __init__(self, rgb_r, rgb_g, rgb_b, is_upscaled=_DO_NOT_USE):
635635
self.rgb_g = float(rgb_g)
636636
self.rgb_b = float(rgb_b)
637637

638-
def _clamp_rgb_coordinate(self, coord):
639-
"""
640-
Clamps an RGB coordinate, taking into account whether or not the
641-
color is upscaled or not.
642-
643-
:param float coord: The coordinate value.
644-
:rtype: float
645-
:returns: The clamped value.
646-
"""
647-
return min(max(coord, 0.0), 1.0)
648-
649638
@property
650639
def clamped_rgb_r(self):
651640
"""
652641
The clamped (0.0-1.0) R value.
653642
"""
654-
return self._clamp_rgb_coordinate(self.rgb_r)
643+
warnings.warn(
644+
"color.clamped_rgb_r is deprecated, use color.clamped().rgb_r instead",
645+
DeprecationWarning,
646+
stacklevel=2,
647+
)
648+
return self.clamped().rgb_r
655649

656650
@property
657651
def clamped_rgb_g(self):
658652
"""
659653
The clamped (0.0-1.0) G value.
660654
"""
661-
return self._clamp_rgb_coordinate(self.rgb_g)
655+
warnings.warn(
656+
"color.clamped_rgb_g is deprecated, use color.clamped().rgb_g instead",
657+
DeprecationWarning,
658+
stacklevel=2,
659+
)
660+
return self.clamped().rgb_g
662661

663662
@property
664663
def clamped_rgb_b(self):
665664
"""
666665
The clamped (0.0-1.0) B value.
667666
"""
668-
return self._clamp_rgb_coordinate(self.rgb_b)
667+
warnings.warn(
668+
"color.clamped_rgb_b is deprecated, use color.clamped().rgb_b instead",
669+
DeprecationWarning,
670+
stacklevel=2,
671+
)
672+
return self.clamped().rgb_b
673+
674+
def clamped(self):
675+
"""
676+
Return copy of this color with coordinates clipped to fit in 0.0-1.0 range.
677+
678+
:rtype: sRGBColor
679+
"""
680+
return type(self)(
681+
min(max(0.0, self.rgb_r), 1.0),
682+
min(max(0.0, self.rgb_g), 1.0),
683+
min(max(0.0, self.rgb_b), 1.0),
684+
)
669685

670686
def get_upscaled_value_tuple(self):
671687
"""
@@ -684,7 +700,7 @@ def get_rgb_hex(self):
684700
685701
:rtype: str
686702
"""
687-
rgb_r, rgb_g, rgb_b = self.get_upscaled_value_tuple()
703+
rgb_r, rgb_g, rgb_b = self.clamped().get_upscaled_value_tuple()
688704
return "#%02x%02x%02x" % (rgb_r, rgb_g, rgb_b)
689705

690706
@classmethod

doc_src/conversions.rst

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,9 @@ RGB conversions and out-of-gamut coordinates
8585

8686
RGB spaces tend to have a smaller gamut than some of the CIE color spaces.
8787
When converting to RGB, this can cause some of the coordinates to end up
88-
being out of the acceptable range (0.0-1.0 or 0-255, depending on whether
89-
your RGB color is upscaled).
88+
being out of the acceptable range (0.0-1.0).
9089

9190
Rather than clamp these for you, we leave them as-is. This allows for more
9291
accurate conversions back to the CIE color spaces. If you require the clamped
93-
(0.0-1.0 or 0-255) values, use the following properties on any RGB color:
94-
95-
* ``clamped_rgb_r``
96-
* ``clamped_rgb_g``
97-
* ``clamped_rgb_b``
92+
values, call ``clamped()`` from any RGB color to get copy of this color with
93+
coordinates clipped to fit in 0.0-1.0 range.

tests/test_color_objects.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,23 @@ def test_channel_clamping(self):
305305
self.assertEqual(low_b.clamped_rgb_g, low_b.rgb_g)
306306
self.assertEqual(low_b.clamped_rgb_b, 0.0)
307307

308+
def test_clamped(self):
309+
for (r, g, b), expected in [
310+
((-.482, -.784, -.196), (0., 0., 0.)),
311+
((1.482, -.784, -.196), (1., 0., 0.)),
312+
((-.482, 1.784, -.196), (0., 1., 0.)),
313+
((1.482, 1.784, -.196), (1., 1., 0.)),
314+
((-.482, -.784, 1.196), (0., 0., 1.)),
315+
((1.482, -.784, 1.196), (1., 0., 1.)),
316+
((-.482, 1.784, 1.196), (0., 1., 1.)),
317+
((1.482, 1.784, 1.196), (1., 1., 1.)),
318+
((0.482, 0.784, 0.196), (0.482, 0.784, 0.196)),
319+
]:
320+
self.assertEqual(
321+
sRGBColor(r, g, b).clamped().get_value_tuple(),
322+
expected,
323+
)
324+
308325
def test_to_xyz_and_back(self):
309326
xyz = convert_color(self.color, XYZColor)
310327
rgb = convert_color(xyz, sRGBColor)

0 commit comments

Comments
 (0)