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

Add RGB_to_YIQ and YIQ_to_RGB #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions colormath/color_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
IPTColor,
SpectralColor,
BT2020Color,
YIQColor,
)
from colormath.chromatic_adaptation import apply_chromatic_adaptation
from colormath.color_exceptions import InvalidIlluminantError, UndefinedConversionError
Expand Down Expand Up @@ -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 = {
Expand All @@ -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],
}


Expand Down
28 changes: 28 additions & 0 deletions colormath/color_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

5 changes: 5 additions & 0 deletions doc_src/color_objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ IPTColor
---------

.. autoclass:: colormath.color_objects.IPTColor

YIQColor
---------

.. autoclass:: colormath.color_objects.YIQColor