def RGB_to_RGB(RGB, input_colourspace, output_colourspace, chromatic_adaptation_method='CAT02'): """ Converts from given input *RGB* colourspace to output *RGB* colourspace using given *chromatic adaptation* method. Parameters ---------- RGB : array_like, (3,) *RGB* colourspace matrix. input_colourspace : RGB_Colourspace *RGB* input colourspace. output_colourspace : RGB_Colourspace *RGB* output colourspace. chromatic_adaptation_method : unicode, optional ('XYZ Scaling', 'Bradford', 'Von Kries', 'Fairchild', 'CAT02') *Chromatic adaptation* method. ndarray, (3,) *RGB* colourspace matrix. Notes ----- - *RGB* colourspace matrices are in domain [0, 1]. Examples -------- >>> from colour import sRGB_COLOURSPACE, PROPHOTO_RGB_COLOURSPACE >>> RGB = np.array([0.35521588, 0.41, 0.24177934]) >>> RGB_to_RGB( ... RGB, ... sRGB_COLOURSPACE, ... PROPHOTO_RGB_COLOURSPACE) # doctest: +ELLIPSIS array([ 0.3579334..., 0.4007138..., 0.2615704...]) """ cat = chromatic_adaptation_matrix( xy_to_XYZ(input_colourspace.whitepoint), xy_to_XYZ(output_colourspace.whitepoint), chromatic_adaptation_method) trs_matrix = np.dot(output_colourspace.to_RGB, np.dot(cat, input_colourspace.to_XYZ)) return np.dot(trs_matrix, RGB)
def RGB_to_RGB(RGB, input_colourspace, output_colourspace, chromatic_adaptation_method='CAT02'): """ Converts from given input *RGB* colourspace to output *RGB* colourspace using given *chromatic adaptation* method. Parameters ---------- RGB : array_like, (3,) *RGB* colourspace matrix. input_colourspace : RGB_Colourspace *RGB* input colourspace. output_colourspace : RGB_Colourspace *RGB* output colourspace. chromatic_adaptation_method : unicode, optional ('XYZ Scaling', 'Bradford', 'Von Kries', 'Fairchild', 'CAT02') *Chromatic adaptation* method. ndarray, (3,) *RGB* colourspace matrix. Notes ----- - *RGB* colourspace matrices are in domain [0, 1]. Examples -------- >>> from colour import sRGB_COLOURSPACE, PROPHOTO_RGB_COLOURSPACE >>> RGB = np.array([0.35521588, 0.41, 0.24177934]) >>> RGB_to_RGB( ... RGB, ... sRGB_COLOURSPACE, ... PROPHOTO_RGB_COLOURSPACE) # doctest: +ELLIPSIS array([ 0.3579334..., 0.4007138..., 0.2615704...]) """ cat = chromatic_adaptation_matrix(xy_to_XYZ(input_colourspace.whitepoint), xy_to_XYZ(output_colourspace.whitepoint), chromatic_adaptation_method) trs_matrix = np.dot(output_colourspace.to_RGB, np.dot(cat, input_colourspace.to_XYZ)) return np.dot(trs_matrix, RGB)
def XYZ_to_RGB(XYZ, illuminant_XYZ, illuminant_RGB, to_RGB, chromatic_adaptation_method='CAT02', transfer_function=None): """ Converts from *CIE XYZ* colourspace to *RGB* colourspace using given *CIE XYZ* colourspace matrix, *illuminants*, *chromatic adaptation* method, *normalised primary matrix* and *transfer function*. Parameters ---------- XYZ : array_like, (3,) *CIE XYZ* colourspace matrix. illuminant_XYZ : array_like *CIE XYZ* colourspace *illuminant* *xy* chromaticity coordinates. illuminant_RGB : array_like *RGB* colourspace *illuminant* *xy* chromaticity coordinates. to_RGB : array_like, (3, 3) *Normalised primary matrix*. chromatic_adaptation_method : unicode, optional ('XYZ Scaling', 'Bradford', 'Von Kries', 'Fairchild', 'CAT02') *Chromatic adaptation* method. transfer_function : object, optional *Transfer function*. Returns ------- ndarray, (3,) *RGB* colourspace matrix. Notes ----- - Input *CIE XYZ* colourspace matrix is in domain [0, 1]. - Input *illuminant_XYZ* *xy* chromaticity coordinates are in domain [0, 1]. - Input *illuminant_RGB* *xy* chromaticity coordinates are in domain [0, 1]. - Output *RGB* colourspace matrix is in domain [0, 1]. Examples -------- >>> XYZ = np.array([0.1151847498, 0.1008, 0.0508937252]) >>> illuminant_XYZ = (0.34567, 0.35850) >>> illuminant_RGB = (0.31271, 0.32902) >>> chromatic_adaptation_method = 'Bradford' >>> to_RGB = np.array([ ... [3.24100326, -1.53739899, -0.49861587], ... [-0.96922426, 1.87592999, 0.04155422], ... [0.05563942, -0.2040112, 1.05714897]]) >>> XYZ_to_RGB( ... XYZ, ... illuminant_XYZ, ... illuminant_RGB, ... to_RGB, ... chromatic_adaptation_method) # doctest: +ELLIPSIS array([ 0.1730350..., 0.0821103..., 0.0567249...]) """ np.array([ [3.24100326, -1.53739899, -0.49861587], [-0.96922426, 1.87592999, 0.04155422], [0.05563942, -0.2040112, 1.05714897]]) cat = chromatic_adaptation_matrix(xy_to_XYZ(illuminant_XYZ), xy_to_XYZ(illuminant_RGB), method=chromatic_adaptation_method) adapted_XYZ = np.dot(cat, XYZ) RGB = np.dot(to_RGB.reshape((3, 3)), adapted_XYZ.reshape((3, 1))) if transfer_function is not None: RGB = np.array([transfer_function(x) for x in np.ravel(RGB)]) return np.ravel(RGB)
def RGB_to_XYZ(RGB, illuminant_RGB, illuminant_XYZ, to_XYZ, chromatic_adaptation_method='CAT02', inverse_transfer_function=None): """ Converts from *RGB* colourspace to *CIE XYZ* colourspace using given *RGB* colourspace matrix, *illuminants*, *chromatic adaptation* method, *normalised primary matrix* and *transfer function*. Parameters ---------- RGB : array_like, (3,) *RGB* colourspace matrix. illuminant_RGB : array_like *RGB* colourspace *illuminant* chromaticity coordinates. illuminant_XYZ : array_like *CIE XYZ* colourspace *illuminant* chromaticity coordinates. to_XYZ : array_like, (3, 3) *Normalised primary matrix*. chromatic_adaptation_method : unicode, optional ('XYZ Scaling', 'Bradford', 'Von Kries', 'Fairchild', 'CAT02') *Chromatic adaptation* method. inverse_transfer_function : object, optional *Inverse transfer function*. Returns ------- ndarray, (3,) *CIE XYZ* colourspace matrix. Notes ----- - Input *RGB* colourspace matrix is in domain [0, 1]. - Input *illuminant_RGB* *xy* chromaticity coordinates are in domain [0, 1]. - Input *illuminant_XYZ* *xy* chromaticity coordinates are in domain [0, 1]. - Output *CIE XYZ* colourspace matrix is in domain [0, 1]. Examples -------- >>> RGB = np.array([0.17303501, 0.08211033, 0.05672498]) >>> illuminant_RGB = (0.31271, 0.32902) >>> illuminant_XYZ = (0.34567, 0.35850) >>> chromatic_adaptation_method = 'Bradford' >>> to_XYZ = np.array([ ... [0.41238656, 0.35759149, 0.18045049], ... [0.21263682, 0.71518298, 0.0721802], ... [0.01933062, 0.11919716, 0.95037259]]) >>> RGB_to_XYZ( ... RGB, ... illuminant_RGB, ... illuminant_XYZ, ... to_XYZ, ... chromatic_adaptation_method) # doctest: +ELLIPSIS array([ 0.1151847..., 0.1008 , 0.0508937...]) """ if inverse_transfer_function is not None: RGB = np.array([inverse_transfer_function(x) for x in np.ravel(RGB)]) XYZ = np.dot(to_XYZ.reshape((3, 3)), RGB.reshape((3, 1))) cat = chromatic_adaptation_matrix( xy_to_XYZ(illuminant_RGB), xy_to_XYZ(illuminant_XYZ), method=chromatic_adaptation_method) adapted_XYZ = np.dot(cat, XYZ.reshape((3, 1))) return np.ravel(adapted_XYZ)
def XYZ_to_RGB(XYZ, illuminant_XYZ, illuminant_RGB, to_RGB, chromatic_adaptation_method='CAT02', transfer_function=None): """ Converts from *CIE XYZ* colourspace to *RGB* colourspace using given *CIE XYZ* colourspace matrix, *illuminants*, *chromatic adaptation* method, *normalised primary matrix* and *transfer function*. Parameters ---------- XYZ : array_like, (3,) *CIE XYZ* colourspace matrix. illuminant_XYZ : array_like *CIE XYZ* colourspace *illuminant* *xy* chromaticity coordinates. illuminant_RGB : array_like *RGB* colourspace *illuminant* *xy* chromaticity coordinates. to_RGB : array_like, (3, 3) *Normalised primary matrix*. chromatic_adaptation_method : unicode, optional ('XYZ Scaling', 'Bradford', 'Von Kries', 'Fairchild', 'CAT02') *Chromatic adaptation* method. transfer_function : object, optional *Transfer function*. Returns ------- ndarray, (3,) *RGB* colourspace matrix. Notes ----- - Input *CIE XYZ* colourspace matrix is in domain [0, 1]. - Input *illuminant_XYZ* *xy* chromaticity coordinates are in domain [0, 1]. - Input *illuminant_RGB* *xy* chromaticity coordinates are in domain [0, 1]. - Output *RGB* colourspace matrix is in domain [0, 1]. Examples -------- >>> XYZ = np.array([0.1151847498, 0.1008, 0.0508937252]) >>> illuminant_XYZ = (0.34567, 0.35850) >>> illuminant_RGB = (0.31271, 0.32902) >>> chromatic_adaptation_method = 'Bradford' >>> to_RGB = np.array([ ... [3.24100326, -1.53739899, -0.49861587], ... [-0.96922426, 1.87592999, 0.04155422], ... [0.05563942, -0.2040112, 1.05714897]]) >>> XYZ_to_RGB( ... XYZ, ... illuminant_XYZ, ... illuminant_RGB, ... to_RGB, ... chromatic_adaptation_method) # doctest: +ELLIPSIS array([ 0.1730350..., 0.0821103..., 0.0567249...]) """ np.array([[3.24100326, -1.53739899, -0.49861587], [-0.96922426, 1.87592999, 0.04155422], [0.05563942, -0.2040112, 1.05714897]]) cat = chromatic_adaptation_matrix(xy_to_XYZ(illuminant_XYZ), xy_to_XYZ(illuminant_RGB), method=chromatic_adaptation_method) adapted_XYZ = np.dot(cat, XYZ) RGB = np.dot(to_RGB.reshape((3, 3)), adapted_XYZ.reshape((3, 1))) if transfer_function is not None: RGB = np.array([transfer_function(x) for x in np.ravel(RGB)]) return np.ravel(RGB)
def RGB_to_XYZ(RGB, illuminant_RGB, illuminant_XYZ, to_XYZ, chromatic_adaptation_method='CAT02', inverse_transfer_function=None): """ Converts from *RGB* colourspace to *CIE XYZ* colourspace using given *RGB* colourspace matrix, *illuminants*, *chromatic adaptation* method, *normalised primary matrix* and *transfer function*. Parameters ---------- RGB : array_like, (3,) *RGB* colourspace matrix. illuminant_RGB : array_like *RGB* colourspace *illuminant* chromaticity coordinates. illuminant_XYZ : array_like *CIE XYZ* colourspace *illuminant* chromaticity coordinates. to_XYZ : array_like, (3, 3) *Normalised primary matrix*. chromatic_adaptation_method : unicode, optional ('XYZ Scaling', 'Bradford', 'Von Kries', 'Fairchild', 'CAT02') *Chromatic adaptation* method. inverse_transfer_function : object, optional *Inverse transfer function*. Returns ------- ndarray, (3,) *CIE XYZ* colourspace matrix. Notes ----- - Input *RGB* colourspace matrix is in domain [0, 1]. - Input *illuminant_RGB* *xy* chromaticity coordinates are in domain [0, 1]. - Input *illuminant_XYZ* *xy* chromaticity coordinates are in domain [0, 1]. - Output *CIE XYZ* colourspace matrix is in domain [0, 1]. Examples -------- >>> RGB = np.array([0.17303501, 0.08211033, 0.05672498]) >>> illuminant_RGB = (0.31271, 0.32902) >>> illuminant_XYZ = (0.34567, 0.35850) >>> chromatic_adaptation_method = 'Bradford' >>> to_XYZ = np.array([ ... [0.41238656, 0.35759149, 0.18045049], ... [0.21263682, 0.71518298, 0.0721802], ... [0.01933062, 0.11919716, 0.95037259]]) >>> RGB_to_XYZ( ... RGB, ... illuminant_RGB, ... illuminant_XYZ, ... to_XYZ, ... chromatic_adaptation_method) # doctest: +ELLIPSIS array([ 0.1151847..., 0.1008 , 0.0508937...]) """ if inverse_transfer_function is not None: RGB = np.array([inverse_transfer_function(x) for x in np.ravel(RGB)]) XYZ = np.dot(to_XYZ.reshape((3, 3)), RGB.reshape((3, 1))) cat = chromatic_adaptation_matrix(xy_to_XYZ(illuminant_RGB), xy_to_XYZ(illuminant_XYZ), method=chromatic_adaptation_method) adapted_XYZ = np.dot(cat, XYZ.reshape((3, 1))) return np.ravel(adapted_XYZ)
def test_chromatic_adaptation_matrix(self): """ Tests :func:`colour.adaptation.cat.chromatic_adaptation_matrix` definition. """ numpy.testing.assert_almost_equal( chromatic_adaptation_matrix( numpy.array([1.09923822, 1.000, 0.35445412]), numpy.array([0.96907232, 1.000, 1.121792157])), numpy.array( [0.87145615, -0.13204674, 0.40394832, -0.09638805, 1.04909781, 0.1604033, 0.0080207, 0.02826367, 3.06023194]).reshape((3, 3)), decimal=7) numpy.testing.assert_almost_equal( chromatic_adaptation_matrix( numpy.array([1.92001986, 1, -0.1241347]), numpy.array([1.0131677, 1.000, 2.11217686])), numpy.array( [0.91344833, -1.20588903, -3.74768526, -0.81680514, 2.3858187, -1.46988227, -0.05367575, -0.31122239, -20.35255049]).reshape((3, 3)), decimal=7) numpy.testing.assert_almost_equal( chromatic_adaptation_matrix( numpy.array([1.92001986, 1, -0.1241347]), numpy.array([1.0131677, 1.000, 2.11217686])), numpy.linalg.inv(chromatic_adaptation_matrix( numpy.array([1.0131677, 1.000, 2.11217686]), numpy.array([1.92001986, 1., -0.1241347])))) numpy.testing.assert_almost_equal( chromatic_adaptation_matrix( numpy.array([1.09850, 1.00000, 0.35585]), numpy.array([0.99072, 1.00000, 0.85223]), method='XYZ Scaling'), numpy.array([0.90188439, 0., 0., 0., 1., 0., 0., 0., 2.39491359]).reshape((3, 3)), decimal=7) numpy.testing.assert_almost_equal( chromatic_adaptation_matrix( numpy.array([1.09850, 1.00000, 0.35585]), numpy.array([0.99072, 1.00000, 0.85223]), method='Bradford'), numpy.array( [0.89051629, -0.08291357, 0.26809449, -0.09715236, 1.07542618, 0.08794629, 0.05389701, -0.09085576, 2.48385527]).reshape((3, 3)), decimal=7) numpy.testing.assert_almost_equal( chromatic_adaptation_matrix( numpy.array([1.09850, 1.00000, 0.35585]), numpy.array([0.99072, 1.00000, 0.85223]), method='Von Kries'), numpy.array( [0.9574884, -0.16436134, 0.29023559, -0.01805393, 1.01853791, 0.00363729, 0., 0., 2.39491359]).reshape((3, 3)), decimal=7)