def test_nan_cartesian_to_polar(self): """ Tests :func:`colour.algebra.coordinates.transformations.\ cartesian_to_polar` definition nan support. """ cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan] cases = set(permutations(cases * 3, r=2)) for case in cases: a_i = np.array(case) cartesian_to_polar(a_i)
def Luv_to_LCHuv(Luv): """ Converts from *CIE Luv* colourspace to *CIE LCHuv* colourspace. Parameters ---------- Luv : array_like *CIE Luv* colourspace array. Returns ------- ndarray *CIE LCHuv* colourspace array. Notes ----- - Input / output :math:`L^*` is in domain / range [0, 100]. Examples -------- >>> Luv = np.array([37.9856291 , -28.80219593, -1.35800706]) >>> Luv_to_LCHuv(Luv) # doctest: +ELLIPSIS array([ 37.9856291..., 28.8341927..., 182.6994640...]) """ L, u, v = tsplit(Luv) C, H = tsplit(cartesian_to_polar(tstack((u, v)))) LCHuv = tstack((L, C, np.degrees(H) % 360)) return LCHuv
def test_n_dimensional_cartesian_to_polar(self): """ Test :func:`colour.algebra.coordinates.transformations.\ cartesian_to_polar` definition n-dimensional arrays support. """ a_i = np.array([3, 1]) a_o = cartesian_to_polar(a_i) a_i = np.tile(a_i, (6, 1)) a_o = np.tile(a_o, (6, 1)) np.testing.assert_almost_equal(cartesian_to_polar(a_i), a_o, decimal=7) a_i = np.reshape(a_i, (2, 3, 2)) a_o = np.reshape(a_o, (2, 3, 2)) np.testing.assert_almost_equal(cartesian_to_polar(a_i), a_o, decimal=7)
def Lab_to_LCHab(Lab): """ Converts from *CIE Lab* colourspace to *CIE LCHab* colourspace. Parameters ---------- Lab : array_like *CIE Lab* colourspace array. Returns ------- ndarray *CIE LCHab* colourspace array. Notes ----- - *Lightness* :math:`L^*` is in domain [0, 100]. Examples -------- >>> Lab = np.array([37.98562910, -23.62907688, -4.41746615]) >>> Lab_to_LCHab(Lab) # doctest: +ELLIPSIS array([ 37.9856291..., 24.0384542..., 190.5892337...]) """ L, a, b = tsplit(Lab) C, H = tsplit(cartesian_to_polar(tstack((a, b)))) LCHab = tstack((L, C, np.degrees(H) % 360)) return LCHab
def test_cartesian_to_polar(self): """ Tests :func:`colour.algebra.coordinates.transformations.\ cartesian_to_polar` definition. """ np.testing.assert_almost_equal(cartesian_to_polar(np.array([3, 1])), np.array([3.16227766, 0.32175055]), decimal=7) np.testing.assert_almost_equal(cartesian_to_polar(np.array([-1, 9])), np.array([9.05538514, 1.68145355]), decimal=7) np.testing.assert_almost_equal(cartesian_to_polar( np.array([6.3434, -0.9345])), np.array([6.41186508, -0.14626640]), decimal=7)
def Luv_to_LCHuv(Luv): """ Converts from *CIE L\\*u\\*v\\** colourspace to *CIE L\\*C\\*Huv* colourspace. Parameters ---------- Luv : array_like *CIE L\\*u\\*v\\** colourspace array. Returns ------- ndarray *CIE L\\*C\\*Huv* colourspace array. Notes ----- +------------+-----------------------+-----------------+ | **Domain** | **Scale - Reference** | **Scale - 1** | +============+=======================+=================+ | ``Luv`` | ``L`` : [0, 100] | ``L`` : [0, 1] | | | | | | | ``u`` : [-100, 100] | ``u`` : [-1, 1] | | | | | | | ``v`` : [-100, 100] | ``v`` : [-1, 1] | +------------+-----------------------+-----------------+ +------------+-----------------------+-----------------+ | **Range** | **Scale - Reference** | **Scale - 1** | +============+=======================+=================+ | ``LCHuv`` | ``L`` : [0, 100] | ``L`` : [0, 1] | | | | | | | ``C`` : [0, 100] | ``C`` : [0, 1] | | | | | | | ``uv`` : [0, 360] | ``uv`` : [0, 1] | +------------+-----------------------+-----------------+ References ---------- :cite:`CIETC1-482004m` Examples -------- >>> Luv = np.array([41.52787529, 96.83626054, 17.75210149]) >>> Luv_to_LCHuv(Luv) # doctest: +ELLIPSIS array([ 41.5278752..., 98.4499795..., 10.3881634...]) """ L, u, v = tsplit(Luv) C, H = tsplit(cartesian_to_polar(tstack([u, v]))) LCHuv = tstack([L, C, from_range_degrees(np.degrees(H) % 360)]) return LCHuv
def Lab_to_LCHab(Lab): """ Converts from *CIE L\\*a\\*b\\** colourspace to *CIE L\\*C\\*Hab* colourspace. Parameters ---------- Lab : array_like *CIE L\\*a\\*b\\** colourspace array. Returns ------- ndarray *CIE L\\*C\\*Hab* colourspace array. Notes ----- +------------+-----------------------+-----------------+ | **Domain** | **Scale - Reference** | **Scale - 1** | +============+=======================+=================+ | ``Lab`` | ``L`` : [0, 100] | ``L`` : [0, 1] | | | | | | | ``a`` : [-100, 100] | ``a`` : [-1, 1] | | | | | | | ``b`` : [-100, 100] | ``b`` : [-1, 1] | +------------+-----------------------+-----------------+ +------------+-----------------------+-----------------+ | **Range** | **Scale - Reference** | **Scale - 1** | +============+=======================+=================+ | ``LCHab`` | ``L`` : [0, 100] | ``L`` : [0, 1] | | | | | | | ``C`` : [0, 100] | ``C`` : [0, 1] | | | | | | | ``ab`` : [0, 360] | ``ab`` : [0, 1] | +------------+-----------------------+-----------------+ References ---------- :cite:`CIETC1-482004m` Examples -------- >>> Lab = np.array([41.52787529, 52.63858304, 26.92317922]) >>> Lab_to_LCHab(Lab) # doctest: +ELLIPSIS array([ 41.5278752..., 59.1242590..., 27.0884878...]) """ L, a, b = tsplit(Lab) C, H = tsplit(cartesian_to_polar(tstack([a, b]))) LCHab = tstack([L, C, from_range_degrees(np.degrees(H) % 360)]) return LCHab
def UCS_Luo2006_to_JMh_CIECAM02(Jpapbp, coefficients): """ Converts from one of the *Luo et alii (2006)* *CAM02-LCD*, *CAM02-SCD*, or *CAM02-UCS* colourspaces :math:`J'a'b'` array to *CIECAM02* :math:`JMh` correlates array. Parameters ---------- Jpapbp : array_like *Luo et alii (2006)* *CAM02-LCD*, *CAM02-SCD*, or *CAM02-UCS* colourspaces :math:`J'a'b'` array. coefficients : array_like Coefficients of one of the *Luo et alii (2006)* *CAM02-LCD*, *CAM02-SCD*, or *CAM02-UCS* colourspaces. Returns ------- ndarray *CIECAM02* correlates array :math:`JMh`. Examples -------- >>> Jpapbp = np.array([54.90433134, -0.08450395, -0.06854831]) >>> UCS_Luo2006_to_JMh_CIECAM02( ... Jpapbp, COEFFICIENTS_UCS_LUO2006['CAM02-LCD']) ... # doctest: +ELLIPSIS array([ 4.1731091...e+01, 1.0884217...e-01, 2.1904843...e+02]) """ J_p, a_p, b_p = tsplit(Jpapbp) _K_L, c_1, c_2 = tsplit(coefficients) J = -J_p / (c_1 * J_p - 1 - 100 * c_1) M_p, h = tsplit(cartesian_to_polar(tstack((a_p, b_p)))) M = (np.exp(M_p / (1 / c_2)) - 1) / c_2 return tstack((J, M, np.degrees(h) % 360))
def Jab_to_JCh(Jab: ArrayLike) -> NDArray: """ Convert from *Jab* colour representation to *JCh* colour representation. This definition is used to perform conversion from *CIE L\\*a\\*b\\** colourspace to *CIE L\\*C\\*Hab* colourspace and for other similar conversions. It implements a generic transformation from *Lightness* :math:`J`, :math:`a` and :math:`b` opponent colour dimensions to the correlates of *Lightness* :math:`J`, chroma :math:`C` and hue angle :math:`h`. Parameters ---------- Jab *Jab* colour representation array. Returns ------- :class:`numpy.ndarray` *JCh* colour representation array. Notes ----- +------------+-----------------------+-----------------+ | **Domain** | **Scale - Reference** | **Scale - 1** | +============+=======================+=================+ | ``Jab`` | ``J`` : [0, 100] | ``J`` : [0, 1] | | | | | | | ``a`` : [-100, 100] | ``a`` : [-1, 1] | | | | | | | ``b`` : [-100, 100] | ``b`` : [-1, 1] | +------------+-----------------------+-----------------+ +------------+-----------------------+-----------------+ | **Range** | **Scale - Reference** | **Scale - 1** | +============+=======================+=================+ | ``JCh`` | ``J`` : [0, 100] | ``J`` : [0, 1] | | | | | | | ``C`` : [0, 100] | ``C`` : [0, 1] | | | | | | | ``h`` : [0, 360] | ``h`` : [0, 1] | +------------+-----------------------+-----------------+ References ---------- :cite:`CIETC1-482004m` Examples -------- >>> Jab = np.array([41.52787529, 52.63858304, 26.92317922]) >>> Jab_to_JCh(Jab) # doctest: +ELLIPSIS array([ 41.5278752..., 59.1242590..., 27.0884878...]) """ L, a, b = tsplit(Jab) C, H = tsplit(cartesian_to_polar(tstack([a, b]))) JCh = tstack([L, C, from_range_degrees(np.degrees(H) % 360)]) return JCh
def UCS_Luo2006_to_JMh_CIECAM02(Jpapbp: ArrayLike, coefficients: ArrayLike) -> NDArray: """ Convert from one of the *Luo et al. (2006)* *CAM02-LCD*, *CAM02-SCD*, or *CAM02-UCS* colourspaces :math:`J'a'b'` array to *CIECAM02* :math:`JMh` correlates array. Parameters ---------- Jpapbp *Luo et al. (2006)* *CAM02-LCD*, *CAM02-SCD*, or *CAM02-UCS* colourspaces :math:`J'a'b'` array. coefficients Coefficients of one of the *Luo et al. (2006)* *CAM02-LCD*, *CAM02-SCD*, or *CAM02-UCS* colourspaces. Returns ------- :class:`numpy.ndarray` *CIECAM02* correlates array :math:`JMh`. Notes ----- +------------+------------------------+------------------+ | **Domain** | **Scale - Reference** | **Scale - 1** | +============+========================+==================+ | ``Jpapbp`` | ``Jp`` : [0, 100] | ``Jp`` : [0, 1] | | | | | | | ``ap`` : [-100, 100] | ``ap`` : [-1, 1] | | | | | | | ``bp`` : [-100, 100] | ``bp`` : [-1, 1] | +------------+------------------------+------------------+ +------------+------------------------+------------------+ | **Range** | **Scale - Reference** | **Scale - 1** | +============+========================+==================+ | ``JMh`` | ``J`` : [0, 100] | ``J`` : [0, 1] | | | | | | | ``M`` : [0, 100] | ``M`` : [0, 1] | | | | | | | ``h`` : [0, 360] | ``h`` : [0, 1] | +------------+------------------------+------------------+ Examples -------- >>> Jpapbp = np.array([54.90433134, -0.08450395, -0.06854831]) >>> UCS_Luo2006_to_JMh_CIECAM02( ... Jpapbp, COEFFICIENTS_UCS_LUO2006['CAM02-LCD']) ... # doctest: +ELLIPSIS array([ 4.1731091...e+01, 1.0884217...e-01, 2.1904843...e+02]) """ J_p, a_p, b_p = tsplit(to_domain_100(Jpapbp)) _K_L, c_1, c_2 = tsplit(coefficients) J = -J_p / (c_1 * J_p - 1 - 100 * c_1) M_p, h = tsplit(cartesian_to_polar(tstack([a_p, b_p]))) M = np.expm1(M_p / (1 / c_2)) / c_2 JMh = tstack([ from_range_100(J), from_range_100(M), from_range_degrees(np.degrees(h) % 360), ]) return JMh
def UCS_Luo2006_to_JMh_CIECAM02(Jpapbp, coefficients): """ Converts from one of the *Luo et al. (2006)* *CAM02-LCD*, *CAM02-SCD*, or *CAM02-UCS* colourspaces :math:`J'a'b'` array to *CIECAM02* :math:`JMh` correlates array. Parameters ---------- Jpapbp : array_like *Luo et al. (2006)* *CAM02-LCD*, *CAM02-SCD*, or *CAM02-UCS* colourspaces :math:`J'a'b'` array. coefficients : array_like Coefficients of one of the *Luo et al. (2006)* *CAM02-LCD*, *CAM02-SCD*, or *CAM02-UCS* colourspaces. Returns ------- ndarray *CIECAM02* correlates array :math:`JMh`. Notes ----- +------------+------------------------+--------------------+ | **Domain** | **Scale - Reference** | **Scale - 1** | +============+========================+====================+ | ``Jpapbp`` | ``Jp_1`` : [0, 100] | ``Jp_1`` : [0, 1] | | | | | | | ``ap_1`` : [-100, 100] | ``ap_1`` : [-1, 1] | | | | | | | ``bp_1`` : [-100, 100] | ``bp_1`` : [-1, 1] | +------------+------------------------+--------------------+ +------------+------------------------+--------------------+ | **Range** | **Scale - Reference** | **Scale - 1** | +============+========================+====================+ | ``JMh`` | ``J`` : [0, 100] | ``J`` : [0, 1] | | | | | | | ``M`` : [0, 100] | ``M`` : [0, 1] | | | | | | | ``h`` : [0, 360] | ``h`` : [0, 1] | +------------+------------------------+--------------------+ Examples -------- >>> Jpapbp = np.array([54.90433134, -0.08450395, -0.06854831]) >>> UCS_Luo2006_to_JMh_CIECAM02( ... Jpapbp, COEFFICIENTS_UCS_LUO2006['CAM02-LCD']) ... # doctest: +ELLIPSIS array([ 4.1731091...e+01, 1.0884217...e-01, 2.1904843...e+02]) """ J_p, a_p, b_p = tsplit(to_domain_100(Jpapbp)) _K_L, c_1, c_2 = tsplit(coefficients) J = -J_p / (c_1 * J_p - 1 - 100 * c_1) M_p, h = tsplit(cartesian_to_polar(tstack([a_p, b_p]))) M = (np.exp(M_p / (1 / c_2)) - 1) / c_2 JMh = tstack([ from_range_100(J), from_range_100(M), from_range_degrees(np.degrees(h) % 360) ]) return JMh