diff --git a/colormath/color_conversions.py b/colormath/color_conversions.py index 67af1eb..5b140a1 100644 --- a/colormath/color_conversions.py +++ b/colormath/color_conversions.py @@ -32,6 +32,7 @@ IPTColor, SpectralColor, BT2020Color, + YIQColor, ) from colormath.chromatic_adaptation import apply_chromatic_adaptation from colormath.color_exceptions import InvalidIlluminantError, UndefinedConversionError @@ -933,6 +934,22 @@ def IPT_to_XYZ(cobj, *args, **kwargs): return XYZColor(*xyz_values, observer="2", illuminant="d65") +# noinspection PyPep8Naming,PyUnusedLocal +@color_conversion_function(BaseRGBColor, YIQColor) +def RGB_to_YIQ(cobj, *args, **kwargs): + rgb_values = numpy.array(cobj.get_value_tuple()) + yiq_values = numpy.dot(YIQColor.conversion_matrices['rgb_to_yiq'], rgb_values) + return YIQColor(*yiq_values) + + +# noinspection PyPep8Naming,PyUnusedLocal +@color_conversion_function(YIQColor, BaseRGBColor) +def YIQ_to_RGB(cobj, target_rgb, *args, **kwargs): + yiq_values = numpy.array(cobj.get_value_tuple()) + rgb_values = numpy.dot(YIQColor.conversion_matrices['rgb_to_yiq'], yiq_values) + return target_rgb(*rgb_values) + + # We use this as a template conversion dict for each RGB color space. They # are all identical. _RGB_CONVERSION_DICT_TEMPLATE = { @@ -947,6 +964,7 @@ def IPT_to_XYZ(cobj, *args, **kwargs): "LCHuvColor": [RGB_to_XYZ, XYZ_to_Luv, Luv_to_LCHuv], "LuvColor": [RGB_to_XYZ, XYZ_to_Luv], "IPTColor": [RGB_to_XYZ, XYZ_to_IPT], + "YIQColor": [RGB_to_YIQ], } diff --git a/colormath/color_objects.py b/colormath/color_objects.py index 0dd315b..64bbb7c 100644 --- a/colormath/color_objects.py +++ b/colormath/color_objects.py @@ -977,3 +977,31 @@ def __init__(self, ipt_i, ipt_p, ipt_t): @property def hue_angle(self): return numpy.arctan2(self.ipt_t, self.ipt_p) + + +class YIQColor(ColorBase): + """ + Represents an YIQ color + + Reference: https://en.wikipedia.org/wiki/YIQ + """ + conversion_matrices = { + 'rgb_to_yiq': numpy.array([[0.299, 0.587, 0.114], + [0.59590059, -0.27455667, -0.32134392], + [0.21153661, -0.52273617, 0.31119955]]), + 'yiq_to_rgb': numpy.linalg.inv(numpy.array([[0.299, 0.587, 0.114], + [0.59590059, -0.27455667, -0.32134392], + [0.21153661, -0.52273617, 0.31119955]])) + } + + def __init__(self, yiq_y, yiq_i, yiq_q): + """ + :param yiq_y: Y coordinate. + :param yiq_i: I coordinate. + :param yiq_q: Q coordinate. + """ + super(YIQColor, self).__init__() + self.yiq_y = yiq_y + self.yiq_i = yiq_i + self.yiq_q = yiq_q + diff --git a/doc_src/color_objects.rst b/doc_src/color_objects.rst index ecade8b..1c38f44 100644 --- a/doc_src/color_objects.rst +++ b/doc_src/color_objects.rst @@ -82,3 +82,8 @@ IPTColor --------- .. autoclass:: colormath.color_objects.IPTColor + +YIQColor +--------- + +.. autoclass:: colormath.color_objects.YIQColor