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

Commit 0a4275f

Browse files
committed
Deprecate is_upscaled flag in favor BaseRGBColor.new_from_upscaled()
1 parent c93df75 commit 0a4275f

2 files changed

Lines changed: 45 additions & 50 deletions

File tree

colormath/color_objects.py

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import logging
77
import math
8+
import warnings
89

910
import numpy
1011

@@ -499,6 +500,9 @@ def __init__(self, xyy_x, xyy_y, xyy_Y, observer='2', illuminant='d50'):
499500
self.set_illuminant(illuminant)
500501

501502

503+
_DO_NOT_USE = object()
504+
505+
502506
class BaseRGBColor(ColorBase):
503507
"""
504508
Base class for all RGB color spaces.
@@ -508,24 +512,35 @@ class BaseRGBColor(ColorBase):
508512

509513
VALUES = ['rgb_r', 'rgb_g', 'rgb_b']
510514

511-
def __init__(self, rgb_r, rgb_g, rgb_b, is_upscaled=False):
512-
"""
513-
:param float rgb_r: R coordinate. 0.0-1.0, or 0-255 if is_upscaled=True.
514-
:param float rgb_g: G coordinate. 0.0-1.0, or 0-255 if is_upscaled=True.
515-
:param float rgb_b: B coordinate. 0.0-1.0, or 0-255 if is_upscaled=True.
516-
:keyword bool is_upscaled: If False, RGB coordinate values are
517-
beteween 0.0 and 1.0. If True, RGB values are between 0 and 255.
518-
"""
515+
def __new__(cls, rgb_r, rgb_g, rgb_b, is_upscaled=_DO_NOT_USE):
516+
if is_upscaled is not _DO_NOT_USE:
517+
warnings.warn(
518+
(
519+
"is_upscaled flag is deprecated, use %s.new_from_upscaled"
520+
"(rgb_r, rgb_g, rgb_b) instead"
521+
) % cls.__name__,
522+
DeprecationWarning,
523+
stacklevel=2,
524+
)
525+
if is_upscaled:
526+
# __init__ will be called twice here
527+
return cls.new_from_upscaled(rgb_r, rgb_g, rgb_b)
528+
return super(BaseRGBColor, cls).__new__(cls)
529+
530+
def __init__(self, rgb_r, rgb_g, rgb_b, is_upscaled=_DO_NOT_USE):
531+
"""
532+
:param float rgb_r: R coordinate.
533+
:param float rgb_g: G coordinate.
534+
:param float rgb_b: B coordinate.
535+
:keyword is_upscaled: deprecated.
536+
"""
537+
if is_upscaled is not _DO_NOT_USE:
538+
# avoid second __init__ call
539+
return
519540
super(BaseRGBColor, self).__init__()
520-
if is_upscaled:
521-
self.rgb_r = rgb_r / 255.0
522-
self.rgb_g = rgb_g / 255.0
523-
self.rgb_b = rgb_b / 255.0
524-
else:
525-
self.rgb_r = float(rgb_r)
526-
self.rgb_g = float(rgb_g)
527-
self.rgb_b = float(rgb_b)
528-
self.is_upscaled = is_upscaled
541+
self.rgb_r = float(rgb_r)
542+
self.rgb_g = float(rgb_g)
543+
self.rgb_b = float(rgb_b)
529544

530545
def _clamp_rgb_coordinate(self, coord):
531546
"""
@@ -536,10 +551,7 @@ def _clamp_rgb_coordinate(self, coord):
536551
:rtype: float
537552
:returns: The clamped value.
538553
"""
539-
if not self.is_upscaled:
540-
return min(max(coord, 0.0), 1.0)
541-
else:
542-
return min(max(coord, 0.0), 255.0)
554+
return min(max(coord, 0.0), 1.0)
543555

544556
@property
545557
def clamped_rgb_r(self):
@@ -595,25 +607,26 @@ def new_from_rgb_hex(cls, hex_str):
595607
colorstring = colorstring[1:]
596608
if len(colorstring) != 6:
597609
raise ValueError("input #%s is not in #RRGGBB format" % colorstring)
598-
r, g, b = colorstring[:2], colorstring[2:4], colorstring[4:]
599-
r, g, b = [int(n, 16) / 255.0 for n in (r, g, b)]
600-
return cls(r, g, b)
610+
return cls.new_from_upscaled(
611+
int(colorstring[:2], 16),
612+
int(colorstring[2:4], 16),
613+
int(colorstring[4:], 16),
614+
)
615+
616+
@classmethod
617+
def new_from_upscaled(cls, r, g, b):
618+
"""Create new RGB color from coordinates in range 0-255."""
619+
return cls(r / 255.0, g / 255.0, b / 255.0)
601620

602621

603622
# noinspection PyPep8Naming
604623
class sRGBColor(BaseRGBColor):
605624
"""
606625
Represents an sRGB color.
607626
608-
.. note:: If you pass in upscaled values, we automatically scale them
609-
down to 0.0-1.0. If you need the old upscaled values, you can
610-
retrieve them with :py:meth:`get_upscaled_value_tuple`.
611-
612627
:ivar float rgb_r: R coordinate
613628
:ivar float rgb_g: G coordinate
614629
:ivar float rgb_b: B coordinate
615-
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
616-
0.0-1.0.
617630
"""
618631

619632
#: RGB space's gamma constant.
@@ -638,15 +651,9 @@ class BT2020Color(BaseRGBColor):
638651
"""
639652
Represents a ITU-R BT.2020 color.
640653
641-
.. note:: If you pass in upscaled values, we automatically scale them
642-
down to 0.0-1.0. If you need the old upscaled values, you can
643-
retrieve them with :py:meth:`get_upscaled_value_tuple`.
644-
645654
:ivar float rgb_r: R coordinate
646655
:ivar float rgb_g: G coordinate
647656
:ivar float rgb_b: B coordinate
648-
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
649-
0.0-1.0.
650657
"""
651658

652659
#: RGB space's gamma constant.
@@ -671,15 +678,9 @@ class AdobeRGBColor(BaseRGBColor):
671678
"""
672679
Represents an Adobe RGB color.
673680
674-
.. note:: If you pass in upscaled values, we automatically scale them
675-
down to 0.0-1.0. If you need the old upscaled values, you can
676-
retrieve them with :py:meth:`get_upscaled_value_tuple`.
677-
678681
:ivar float rgb_r: R coordinate
679682
:ivar float rgb_g: G coordinate
680683
:ivar float rgb_b: B coordinate
681-
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
682-
0.0-1.0.
683684
"""
684685

685686
#: RGB space's gamma constant.
@@ -704,15 +705,9 @@ class AppleRGBColor(BaseRGBColor):
704705
"""
705706
Represents an AppleRGB color.
706707
707-
.. note:: If you pass in upscaled values, we automatically scale them
708-
down to 0.0-1.0. If you need the old upscaled values, you can
709-
retrieve them with :py:meth:`get_upscaled_value_tuple`.
710-
711708
:ivar float rgb_r: R coordinate
712709
:ivar float rgb_g: G coordinate
713710
:ivar float rgb_b: B coordinate
714-
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
715-
0.0-1.0.
716711
"""
717712

718713
#: RGB space's gamma constant.

tests/test_color_objects.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,12 @@ def test_to_xyz_and_back(self):
270270
self.assertColorMatch(rgb, self.color)
271271

272272
def test_conversion_to_hsl_max_r(self):
273-
color = sRGBColor(255, 123, 50, is_upscaled=True)
273+
color = sRGBColor.new_from_upscaled(255, 123, 50)
274274
hsl = convert_color(color, HSLColor)
275275
self.assertColorMatch(hsl, HSLColor(21.366, 1.000, 0.598))
276276

277277
def test_conversion_to_hsl_max_g(self):
278-
color = sRGBColor(123, 255, 50, is_upscaled=True)
278+
color = sRGBColor.new_from_upscaled(123, 255, 50)
279279
hsl = convert_color(color, HSLColor)
280280
self.assertColorMatch(hsl, HSLColor(98.634, 1.000, 0.598))
281281

0 commit comments

Comments
 (0)