diff options
author | Thomas Touhey <thomas@touhey.fr> | 2019-05-15 11:56:30 +0200 |
---|---|---|
committer | Thomas Touhey <thomas@touhey.fr> | 2019-05-15 11:56:30 +0200 |
commit | 191aa991c32b9e0124bf8256c1ae7c130b74b248 (patch) | |
tree | 16b2e4ca0021a50dbf38ca469a8aa8ea0be00c5c /thcolor/_sys.py | |
parent | 71d92d00bb625a2eb9bf767e81b0b35cd56b9113 (diff) |
Added a few other systems and RGB color profiles.
Diffstat (limited to 'thcolor/_sys.py')
-rwxr-xr-x | thcolor/_sys.py | 64 |
1 files changed, 53 insertions, 11 deletions
diff --git a/thcolor/_sys.py b/thcolor/_sys.py index d0e0227..23c1ec0 100755 --- a/thcolor/_sys.py +++ b/thcolor/_sys.py @@ -5,12 +5,14 @@ #****************************************************************************** """ Conversions between color systems. """ -from math import ceil as _ceil +from math import (ceil as _ceil, atan2 as _atan2, sqrt as _sqrt, cos as _cos, + sin as _sin, pow as _pow) from ._angle import Angle as _Angle __all__ = ["hls_to_rgb", "rgb_to_hls", "rgb_to_hwb", "hwb_to_rgb", - "cmyk_to_rgb", "rgb_to_cmyk", "netscape_color"] + "cmyk_to_rgb", "rgb_to_cmyk", "lab_to_rgb", "rgb_to_lab", + "lch_to_lab", "lab_to_lch", "netscape_color"] # --- # Color systems conversion utilities. @@ -21,6 +23,18 @@ def _rgb(r, g, b): def _hls(hue, s, l): return _Angle(_Angle.Type.DEG, round(hue, 2)), round(l, 2), round(s, 2) +def _rgb_to_lrgb(r, g, b): + def _linearize(val): + # Undo gamma encoding. + + val /= 255 + if val < 0.04045: + return val / 12.92 + + return _pow((val + 0.055) / 1.055, 2.4) + + return (*map(_linearize, (r, g, b))) + def hls_to_rgb(hue, l, s): """ Convert HLS to RGB. """ @@ -62,6 +76,43 @@ def hwb_to_rgb(hue, w, bl): w, bl = map(lambda x: x / (w + bl), (w, bl)) return _rgb(*map(lambda x: x * (1 - w - bl) + w, (r, g, b))) +def cmyk_to_rgb(c, m, y, k): + """ Convert CMYK to RGB. """ + + r = 1 - min(1, c * (1 - k) + k) + g = 1 - min(1, m * (1 - k) + k) + b = 1 - min(1, y * (1 - k) + k) + + return _rgb(r, g, b) + +def lab_to_rgb(l, a, b): + """ Convert LAB to RGB. """ + + # TODO + raise NotImplementedError + +def rgb_to_lab(r, g, b): + """ Convert RGB to LAB. """ + + # TODO + raise NotImplementedError + +def lab_to_lch(l, a, b): + """ Convert RGB to LAB. """ + + h = _Angle(_Angle.Type.RAD, _atan2(b, a)) + c = _sqrt(a * a + b * b) + + return (l, c, h) + +def lch_to_lab(l, c, h): + """ Convert LCH to LAB. """ + + a = c * _cos(h.radians) + b = c * _sin(h.radians) + + return (l, a, b) + def rgb_to_hls(r, g, b): """ Convert RGB to HLS. """ @@ -117,15 +168,6 @@ def rgb_to_hwb(r, g, b): return _Angle(_Angle.Type.DEG, hue), w, b -def cmyk_to_rgb(c, m, y, k): - """ Convert CMYK to RGB. """ - - r = 1 - min(1, c * (1 - k) + k) - g = 1 - min(1, m * (1 - k) + k) - b = 1 - min(1, y * (1 - k) + k) - - return _rgb(r, g, b) - def rgb_to_cmyk(r, g, b): """ Convert RGB to CMYK. """ |