5
5
6
6
import logging
7
7
import math
8
+ import warnings
8
9
9
10
import numpy
10
11
@@ -591,6 +592,9 @@ def __init__(self, xyy_x, xyy_y, xyy_Y, observer="2", illuminant="d50"):
591
592
self .set_illuminant (illuminant )
592
593
593
594
595
+ _DO_NOT_USE = object ()
596
+
597
+
594
598
class BaseRGBColor (ColorBase ):
595
599
"""
596
600
Base class for all RGB color spaces.
@@ -600,24 +604,36 @@ class BaseRGBColor(ColorBase):
600
604
601
605
VALUES = ["rgb_r" , "rgb_g" , "rgb_b" ]
602
606
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 ):
604
624
"""
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.
610
629
"""
630
+ if is_upscaled is not _DO_NOT_USE :
631
+ # avoid second __init__ call
632
+ return
611
633
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 )
621
637
622
638
def _clamp_rgb_coordinate (self , coord ):
623
639
"""
@@ -628,10 +644,7 @@ def _clamp_rgb_coordinate(self, coord):
628
644
:rtype: float
629
645
:returns: The clamped value.
630
646
"""
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 )
635
648
636
649
@property
637
650
def clamped_rgb_r (self ):
@@ -687,25 +700,26 @@ def new_from_rgb_hex(cls, hex_str):
687
700
colorstring = colorstring [1 :]
688
701
if len (colorstring ) != 6 :
689
702
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 )
693
713
694
714
695
715
# noinspection PyPep8Naming
696
716
class sRGBColor (BaseRGBColor ):
697
717
"""
698
718
Represents an sRGB color.
699
719
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
-
704
720
:ivar float rgb_r: R coordinate
705
721
:ivar float rgb_g: G coordinate
706
722
: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.
709
723
"""
710
724
711
725
#: RGB space's gamma constant.
@@ -734,15 +748,9 @@ class BT2020Color(BaseRGBColor):
734
748
"""
735
749
Represents a ITU-R BT.2020 color.
736
750
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
-
741
751
:ivar float rgb_r: R coordinate
742
752
:ivar float rgb_g: G coordinate
743
753
: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.
746
754
"""
747
755
748
756
#: RGB space's gamma constant.
@@ -771,15 +779,9 @@ class AdobeRGBColor(BaseRGBColor):
771
779
"""
772
780
Represents an Adobe RGB color.
773
781
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
-
778
782
:ivar float rgb_r: R coordinate
779
783
:ivar float rgb_g: G coordinate
780
784
: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.
783
785
"""
784
786
785
787
#: RGB space's gamma constant.
@@ -808,15 +810,9 @@ class AppleRGBColor(BaseRGBColor):
808
810
"""
809
811
Represents an AppleRGB color.
810
812
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
-
815
813
:ivar float rgb_r: R coordinate
816
814
:ivar float rgb_g: G coordinate
817
815
: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.
820
816
"""
821
817
822
818
#: RGB space's gamma constant.
0 commit comments