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

Correct the RGB clamping range for upscaled values #95

Merged
merged 1 commit into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions colormath/color_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def apply_RGB_matrix(var1, var2, var3, rgb_type, convtype="xyz_to_rgb"):
Applies an RGB working matrix to convert from XYZ to RGB.
The arguments are tersely named var1, var2, and var3 to allow for the
passing of XYZ _or_ RGB values. var1 is X for XYZ, and R for RGB. var2 and
var3 follow suite.
var3 follow suit.
"""
convtype = convtype.lower()
# Retrieve the appropriate transformation matrix from the constants.
Expand Down Expand Up @@ -541,7 +541,7 @@ def XYZ_to_RGB(cobj, target_rgb, *args, **kwargs):
@color_conversion_function(BaseRGBColor, XYZColor)
def RGB_to_XYZ(cobj, target_illuminant=None, *args, **kwargs):
"""
RGB to XYZ conversion. Expects 0-255 RGB values.
RGB to XYZ conversion. Expects RGB values between 0 and 255.

Based off of: http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html
"""
Expand Down
18 changes: 9 additions & 9 deletions colormath/color_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,11 @@ class BaseRGBColor(ColorBase):

def __init__(self, rgb_r, rgb_g, rgb_b, is_upscaled=False):
"""
:param float rgb_r: R coordinate. 0...1. 1-255 if is_upscaled=True.
:param float rgb_g: G coordinate. 0...1. 1-255 if is_upscaled=True.
:param float rgb_b: B coordinate. 0...1. 1-255 if is_upscaled=True.
:param float rgb_r: R coordinate. 0.0-1.0, or 0-255 if is_upscaled=True.
:param float rgb_g: G coordinate. 0.0-1.0, or 0-255 if is_upscaled=True.
:param float rgb_b: B coordinate. 0.0-1.0, or 0-255 if is_upscaled=True.
:keyword bool is_upscaled: If False, RGB coordinate values are
beteween 0.0 and 1.0. If True, RGB values are between 1 and 255.
beteween 0.0 and 1.0. If True, RGB values are between 0 and 255.
"""
super(BaseRGBColor, self).__init__()
if is_upscaled:
Expand All @@ -539,7 +539,7 @@ def _clamp_rgb_coordinate(self, coord):
if not self.is_upscaled:
return min(max(coord, 0.0), 1.0)
else:
return min(max(coord, 1), 255)
return min(max(coord, 0.0), 255.0)

@property
def clamped_rgb_r(self):
Expand Down Expand Up @@ -612,7 +612,7 @@ class sRGBColor(BaseRGBColor):
:ivar float rgb_r: R coordinate
:ivar float rgb_g: G coordinate
:ivar float rgb_b: B coordinate
:ivar bool is_upscaled: If True, RGB values are between 1-255. If False,
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
0.0-1.0.
"""

Expand Down Expand Up @@ -645,7 +645,7 @@ class BT2020Color(BaseRGBColor):
:ivar float rgb_r: R coordinate
:ivar float rgb_g: G coordinate
:ivar float rgb_b: B coordinate
:ivar bool is_upscaled: If True, RGB values are between 1-255. If False,
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
0.0-1.0.
"""

Expand Down Expand Up @@ -678,7 +678,7 @@ class AdobeRGBColor(BaseRGBColor):
:ivar float rgb_r: R coordinate
:ivar float rgb_g: G coordinate
:ivar float rgb_b: B coordinate
:ivar bool is_upscaled: If True, RGB values are between 1-255. If False,
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
0.0-1.0.
"""

Expand Down Expand Up @@ -711,7 +711,7 @@ class AppleRGBColor(BaseRGBColor):
:ivar float rgb_r: R coordinate
:ivar float rgb_g: G coordinate
:ivar float rgb_b: B coordinate
:ivar bool is_upscaled: If True, RGB values are between 1-255. If False,
:ivar bool is_upscaled: If True, RGB values are between 0-255. If False,
0.0-1.0.
"""

Expand Down
4 changes: 2 additions & 2 deletions doc_src/conversions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ RGB conversions and out-of-gamut coordinates

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

Rather than clamp these for you, we leave them as-is. This allows for more
accurate conversions back to the CIE color spaces. If you require the clamped
(0.0-1.0 or 1-255) values, use the following properties on any RGB color:
(0.0-1.0 or 0-255) values, use the following properties on any RGB color:

* ``clamped_rgb_r``
* ``clamped_rgb_g``
Expand Down