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

Commit 8368a37

Browse files
committed
Deprecate is_upscaled flag in favor BaseRGBColor.new_from_upscaled()
1 parent c761f00 commit 8368a37

File tree

2 files changed

+44
-48
lines changed

2 files changed

+44
-48
lines changed

colormath/color_objects.py

Lines changed: 42 additions & 46 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

@@ -591,6 +592,9 @@ def __init__(self, xyy_x, xyy_y, xyy_Y, observer="2", illuminant="d50"):
591592
self.set_illuminant(illuminant)
592593

593594

595+
_DO_NOT_USE = object()
596+
597+
594598
class BaseRGBColor(ColorBase):
595599
"""
596600
Base class for all RGB color spaces.
@@ -600,24 +604,36 @@ class BaseRGBColor(ColorBase):
600604

601605
VALUES = ["rgb_r", "rgb_g", "rgb_b"]
602606

603-
def __init__(self, rgb_r, rgb_g, rgb_b, is_upscaled=False):
607+
def __new__(cls, rgb_r, rgb_g, rgb_b, is_upscaled=_DO_NOT_USE):
608+
if is_upscaled is not _DO_NOT_USE:
609+
warnings.warn(
610+
(
611+
"is_upscaled flag is deprecated, use %s.new_from_upscaled"
612+
"(rgb_r, rgb_g, rgb_b) instead"
613+
)
614+
% cls.__name__,
615+
DeprecationWarning,
616+
stacklevel=2,
617+
)
618+
if is_upscaled:
619+
# __init__ will be called twice here
620+
return cls.new_from_upscaled(rgb_r, rgb_g, rgb_b)
621+
return super(BaseRGBColor, cls).__new__(cls)
622+
623+
def __init__(self, rgb_r, rgb_g, rgb_b, is_upscaled=_DO_NOT_USE):
604624
"""
605-
:param float rgb_r: R coordinate. 0.0-1.0, or 0-255 if is_upscaled=True.
606-
:param float rgb_g: G coordinate. 0.0-1.0, or 0-255 if is_upscaled=True.
607-
:param float rgb_b: B coordinate. 0.0-1.0, or 0-255 if is_upscaled=True.
608-
:keyword bool is_upscaled: If False, RGB coordinate values are
609-
beteween 0.0 and 1.0. If True, RGB values are between 0 and 255.
625+
:param float rgb_r: R coordinate.
626+
:param float rgb_g: G coordinate.
627+
:param float rgb_b: B coordinate.
628+
:keyword is_upscaled: deprecated.
610629
"""
630+
if is_upscaled is not _DO_NOT_USE:
631+
# avoid second __init__ call
632+
return
611633
super(BaseRGBColor, self).__init__()
612-
if is_upscaled:
613-
self.rgb_r = rgb_r / 255.0
614-
self.rgb_g = rgb_g / 255.0
615-
self.rgb_b = rgb_b / 255.0
616-
else:
617-
self.rgb_r = float(rgb_r)
618-
self.rgb_g = float(rgb_g)
619-
self.rgb_b = float(rgb_b)
620-
self.is_upscaled = is_upscaled
634+
self.rgb_r = float(rgb_r)
635+
self.rgb_g = float(rgb_g)
636+
self.rgb_b = float(rgb_b)
621637

622638
def _clamp_rgb_coordinate(self, coord):
623639
"""
@@ -628,10 +644,7 @@ def _clamp_rgb_coordinate(self, coord):
628644
:rtype: float
629645
:returns: The clamped value.
630646
"""
631-
if not self.is_upscaled:
632-
return min(max(coord, 0.0), 1.0)
633-
else:
634-
return min(max(coord, 0.0), 255.0)
647+
return min(max(coord, 0.0), 1.0)
635648

636649
@property
637650
def clamped_rgb_r(self):
@@ -687,25 +700,26 @@ def new_from_rgb_hex(cls, hex_str):
687700
colorstring = colorstring[1:]
688701
if len(colorstring) != 6:
689702
raise ValueError("input #%s is not in #RRGGBB format" % colorstring)
690-
r, g, b = colorstring[:2], colorstring[2:4], colorstring[4:]
691-
r, g, b = [int(n, 16) / 255.0 for n in (r, g, b)]
692-
return cls(r, g, b)
703+
return cls.new_from_upscaled(
704+
int(colorstring[:2], 16),
705+
int(colorstring[2:4], 16),
706+
int(colorstring[4:], 16),
707+
)
708+
709+
@classmethod
710+
def new_from_upscaled(cls, r, g, b):
711+
"""Create new RGB color from coordinates in range 0-255."""
712+
return cls(r / 255.0, g / 255.0, b / 255.0)
693713

694714

695715
# noinspection PyPep8Naming
696716
class sRGBColor(BaseRGBColor):
697717
"""
698718
Represents an sRGB color.
699719
700-
.. note:: If you pass in upscaled values, we automatically scale them
701-
down to 0.0-1.0. If you need the old upscaled values, you can
702-
retrieve them with :py:meth:`get_upscaled_value_tuple`.
703-
704720
:ivar float rgb_r: R coordinate
705721
:ivar float rgb_g: G coordinate
706722
:ivar float rgb_b: B coordinate
707-
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
708-
0.0-1.0.
709723
"""
710724

711725
#: RGB space's gamma constant.
@@ -734,15 +748,9 @@ class BT2020Color(BaseRGBColor):
734748
"""
735749
Represents a ITU-R BT.2020 color.
736750
737-
.. note:: If you pass in upscaled values, we automatically scale them
738-
down to 0.0-1.0. If you need the old upscaled values, you can
739-
retrieve them with :py:meth:`get_upscaled_value_tuple`.
740-
741751
:ivar float rgb_r: R coordinate
742752
:ivar float rgb_g: G coordinate
743753
:ivar float rgb_b: B coordinate
744-
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
745-
0.0-1.0.
746754
"""
747755

748756
#: RGB space's gamma constant.
@@ -771,15 +779,9 @@ class AdobeRGBColor(BaseRGBColor):
771779
"""
772780
Represents an Adobe RGB color.
773781
774-
.. note:: If you pass in upscaled values, we automatically scale them
775-
down to 0.0-1.0. If you need the old upscaled values, you can
776-
retrieve them with :py:meth:`get_upscaled_value_tuple`.
777-
778782
:ivar float rgb_r: R coordinate
779783
:ivar float rgb_g: G coordinate
780784
:ivar float rgb_b: B coordinate
781-
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
782-
0.0-1.0.
783785
"""
784786

785787
#: RGB space's gamma constant.
@@ -808,15 +810,9 @@ class AppleRGBColor(BaseRGBColor):
808810
"""
809811
Represents an AppleRGB color.
810812
811-
.. note:: If you pass in upscaled values, we automatically scale them
812-
down to 0.0-1.0. If you need the old upscaled values, you can
813-
retrieve them with :py:meth:`get_upscaled_value_tuple`.
814-
815813
:ivar float rgb_r: R coordinate
816814
:ivar float rgb_g: G coordinate
817815
:ivar float rgb_b: B coordinate
818-
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
819-
0.0-1.0.
820816
"""
821817

822818
#: RGB space's gamma constant.

tests/test_color_objects.py

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

313313
def test_conversion_to_hsl_max_r(self):
314-
color = sRGBColor(255, 123, 50, is_upscaled=True)
314+
color = sRGBColor.new_from_upscaled(255, 123, 50)
315315
hsl = convert_color(color, HSLColor)
316316
self.assertColorMatch(hsl, HSLColor(21.366, 1.000, 0.598))
317317

318318
def test_conversion_to_hsl_max_g(self):
319-
color = sRGBColor(123, 255, 50, is_upscaled=True)
319+
color = sRGBColor.new_from_upscaled(123, 255, 50)
320320
hsl = convert_color(color, HSLColor)
321321
self.assertColorMatch(hsl, HSLColor(98.634, 1.000, 0.598))
322322

0 commit comments

Comments
 (0)