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

Commit fa10986

Browse files
committed
Deprecate BaseRGBColor.clamped_rgb_{r,g,b} property in favor BaseRGBColor.clamped()
1 parent 6975d4f commit fa10986

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
@@ -542,37 +542,53 @@ def __init__(self, rgb_r, rgb_g, rgb_b, is_upscaled=_DO_NOT_USE):
542542
self.rgb_g = float(rgb_g)
543543
self.rgb_b = float(rgb_b)
544544

545-
def _clamp_rgb_coordinate(self, coord):
546-
"""
547-
Clamps an RGB coordinate, taking into account whether or not the
548-
color is upscaled or not.
549-
550-
:param float coord: The coordinate value.
551-
:rtype: float
552-
:returns: The clamped value.
553-
"""
554-
return min(max(coord, 0.0), 1.0)
555-
556545
@property
557546
def clamped_rgb_r(self):
558547
"""
559548
The clamped (0.0-1.0) R value.
560549
"""
561-
return self._clamp_rgb_coordinate(self.rgb_r)
550+
warnings.warn(
551+
"color.clamped_rgb_r is deprecated, use color.clamped().rgb_r instead",
552+
DeprecationWarning,
553+
stacklevel=2,
554+
)
555+
return self.clamped().rgb_r
562556

563557
@property
564558
def clamped_rgb_g(self):
565559
"""
566560
The clamped (0.0-1.0) G value.
567561
"""
568-
return self._clamp_rgb_coordinate(self.rgb_g)
562+
warnings.warn(
563+
"color.clamped_rgb_g is deprecated, use color.clamped().rgb_g instead",
564+
DeprecationWarning,
565+
stacklevel=2,
566+
)
567+
return self.clamped().rgb_g
569568

570569
@property
571570
def clamped_rgb_b(self):
572571
"""
573572
The clamped (0.0-1.0) B value.
574573
"""
575-
return self._clamp_rgb_coordinate(self.rgb_b)
574+
warnings.warn(
575+
"color.clamped_rgb_b is deprecated, use color.clamped().rgb_b instead",
576+
DeprecationWarning,
577+
stacklevel=2,
578+
)
579+
return self.clamped().rgb_b
580+
581+
def clamped(self):
582+
"""
583+
Return copy of this color with coordinates clipped to fit in 0.0-1.0 range.
584+
585+
:rtype: sRGBColor
586+
"""
587+
return type(self)(
588+
min(max(0.0, self.rgb_r), 1.0),
589+
min(max(0.0, self.rgb_g), 1.0),
590+
min(max(0.0, self.rgb_b), 1.0),
591+
)
576592

577593
def get_upscaled_value_tuple(self):
578594
"""
@@ -591,7 +607,7 @@ def get_rgb_hex(self):
591607
592608
:rtype: str
593609
"""
594-
rgb_r, rgb_g, rgb_b = self.get_upscaled_value_tuple()
610+
rgb_r, rgb_g, rgb_b = self.clamped().get_upscaled_value_tuple()
595611
return '#%02x%02x%02x' % (rgb_r, rgb_g, rgb_b)
596612

597613
@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
@@ -264,6 +264,23 @@ def test_channel_clamping(self):
264264
self.assertEqual(low_b.clamped_rgb_g, low_b.rgb_g)
265265
self.assertEqual(low_b.clamped_rgb_b, 0.0)
266266

267+
def test_clamped(self):
268+
for (r, g, b), expected in [
269+
((-.482, -.784, -.196), (0., 0., 0.)),
270+
((1.482, -.784, -.196), (1., 0., 0.)),
271+
((-.482, 1.784, -.196), (0., 1., 0.)),
272+
((1.482, 1.784, -.196), (1., 1., 0.)),
273+
((-.482, -.784, 1.196), (0., 0., 1.)),
274+
((1.482, -.784, 1.196), (1., 0., 1.)),
275+
((-.482, 1.784, 1.196), (0., 1., 1.)),
276+
((1.482, 1.784, 1.196), (1., 1., 1.)),
277+
((0.482, 0.784, 0.196), (0.482, 0.784, 0.196)),
278+
]:
279+
self.assertEqual(
280+
sRGBColor(r, g, b).clamped().get_value_tuple(),
281+
expected,
282+
)
283+
267284
def test_to_xyz_and_back(self):
268285
xyz = convert_color(self.color, XYZColor)
269286
rgb = convert_color(xyz, sRGBColor)

0 commit comments

Comments
 (0)