<gh_stars>0
# -*- coding: utf-8 -*-
"""
colXYZ.py
Author: SMFSW
Copyright (c) 2016-2021 SMFSW
Description: XYZ color space class & references
"""

from colorConv import *
from colorConvTemperature import getTempXYZ
from colBase import ColBase as cB
import colRGB as cRGB
import colYxy as cYxy
import colHunterLab as cHunterLab
import colCIELab as cCIELab
import colCIELuv as cCIELuv


class RefXYZSpectral(object):
    """ XYZ Spectral dicts
    XYZ luminous efficiency and color matching functions
    2° or 10° observer in 5nm steps """
    lfields_wavelength = ['Wavelength', 'Color Matching Function (CMF) XYZ_bar', 'XYZ']

    dXYZSpectral = {
        '2' : {380: ([0.0014, 0.0000, 0.0065], [0.1741, 0.0050, 0.8209]),
               385: ([0.0022, 0.0001, 0.0105], [0.1740, 0.0050, 0.8210]),
               390: ([0.0042, 0.0001, 0.0201], [0.1738, 0.0049, 0.8213]),
               395: ([0.0076, 0.0002, 0.0362], [0.1736, 0.0049, 0.8215]),
               400: ([0.0143, 0.0004, 0.0679], [0.1733, 0.0048, 0.8219]),
               405: ([0.0232, 0.0006, 0.1102], [0.1730, 0.0048, 0.8222]),
               410: ([0.0435, 0.0012, 0.2074], [0.1726, 0.0048, 0.8226]),
               415: ([0.0776, 0.0022, 0.3713], [0.1721, 0.0048, 0.8231]),
               420: ([0.1344, 0.0040, 0.6456], [0.1714, 0.0051, 0.8235]),
               425: ([0.2148, 0.0073, 1.0391], [0.1703, 0.0058, 0.8239]),
               430: ([0.2839, 0.0116, 1.3856], [0.1689, 0.0069, 0.8242]),
               435: ([0.3285, 0.0168, 1.6230], [0.1669, 0.0086, 0.8245]),
               440: ([0.3483, 0.0230, 1.7471], [0.1644, 0.0109, 0.8247]),
               445: ([0.3481, 0.0298, 1.7826], [0.1611, 0.0138, 0.8251]),
               450: ([0.3362, 0.0380, 1.7721], [0.1566, 0.0177, 0.8257]),
               455: ([0.3187, 0.0480, 1.7441], [0.1510, 0.0227, 0.8263]),
               460: ([0.2908, 0.0600, 1.6692], [0.1440, 0.0297, 0.8263]),
               465: ([0.2511, 0.0739, 1.5281], [0.1355, 0.0399, 0.8246]),
               470: ([0.1954, 0.0910, 1.2876], [0.1241, 0.0578, 0.8181]),
               475: ([0.1421, 0.1126, 1.0419], [0.1096, 0.0868, 0.8036]),
               480: ([0.0956, 0.1390, 0.8130], [0.0913, 0.1327, 0.7760]),
               485: ([0.0580, 0.1693, 0.6162], [0.0687, 0.2007, 0.7306]),
               490: ([0.0320, 0.2080, 0.4652], [0.0454, 0.2950, 0.6596]),
               495: ([0.0147, 0.2586, 0.3533], [0.0235, 0.4127, 0.5638]),
               500: ([0.0049, 0.3230, 0.2720], [0.0082, 0.5384, 0.4534]),
               505: ([0.0024, 0.4073, 0.2123], [0.0039, 0.6548, 0.3413]),
               510: ([0.0093, 0.5030, 0.1582], [0.0139, 0.7502, 0.2359]),
               515: ([0.0291, 0.6082, 0.1117], [0.0389, 0.8120, 0.1491]),
               520: ([0.0633, 0.7100, 0.0782], [0.0743, 0.8338, 0.0919]),
               525: ([0.1096, 0.7932, 0.0573], [0.1142, 0.8262, 0.0596]),
               530: ([0.1655, 0.8620, 0.0422], [0.1547, 0.8059, 0.0394]),
               535: ([0.2257, 0.9149, 0.0298], [0.1929, 0.7816, 0.0255]),
               540: ([0.2904, 0.9540, 0.0203], [0.2296, 0.7543, 0.0161]),
               545: ([0.3597, 0.9803, 0.0134], [0.2658, 0.7243, 0.0099]),
               550: ([0.4334, 0.9950, 0.0087], [0.3016, 0.6923, 0.0061]),
               555: ([0.5121, 1.0000, 0.0057], [0.3373, 0.6589, 0.0038]),
               560: ([0.5945, 0.9950, 0.0039], [0.3731, 0.6245, 0.0024]),
               565: ([0.6784, 0.9786, 0.0027], [0.4087, 0.5896, 0.0017]),
               570: ([0.7621, 0.9520, 0.0021], [0.4441, 0.5547, 0.0012]),
               575: ([0.8425, 0.9154, 0.0018], [0.4788, 0.5202, 0.0010]),
               580: ([0.9163, 0.8700, 0.0017], [0.5125, 0.4866, 0.0009]),
               585: ([0.9786, 0.8163, 0.0014], [0.5448, 0.4544, 0.0008]),
               590: ([1.0263, 0.7570, 0.0011], [0.5752, 0.4242, 0.0006]),
               595: ([1.0567, 0.6949, 0.0010], [0.6029, 0.3965, 0.0006]),
               600: ([1.0622, 0.6310, 0.0008], [0.6270, 0.3725, 0.0005]),
               605: ([1.0456, 0.5668, 0.0006], [0.6482, 0.3514, 0.0004]),
               610: ([1.0026, 0.5030, 0.0003], [0.6658, 0.3340, 0.0002]),
               615: ([0.9384, 0.4412, 0.0002], [0.6801, 0.3197, 0.0002]),
               620: ([0.8544, 0.3810, 0.0002], [0.6915, 0.3083, 0.0002]),
               625: ([0.7514, 0.3210, 0.0001], [0.7006, 0.2993, 0.0001]),
               630: ([0.6424, 0.2650, 0.0000], [0.7079, 0.2920, 0.0001]),
               635: ([0.5419, 0.2170, 0.0000], [0.7140, 0.2859, 0.0001]),
               640: ([0.4479, 0.1750, 0.0000], [0.7190, 0.2809, 0.0001]),
               645: ([0.3608, 0.1382, 0.0000], [0.7230, 0.2770, 0.0000]),
               650: ([0.2835, 0.1070, 0.0000], [0.7260, 0.2740, 0.0000]),
               655: ([0.2187, 0.0816, 0.0000], [0.7283, 0.2717, 0.0000]),
               660: ([0.1649, 0.0610, 0.0000], [0.7300, 0.2700, 0.0000]),
               665: ([0.1212, 0.0446, 0.0000], [0.7311, 0.2689, 0.0000]),
               670: ([0.0874, 0.0320, 0.0000], [0.7320, 0.2680, 0.0000]),
               675: ([0.0636, 0.0232, 0.0000], [0.7327, 0.2673, 0.0000]),
               680: ([0.0468, 0.0170, 0.0000], [0.7334, 0.2666, 0.0000]),
               685: ([0.0329, 0.0119, 0.0000], [0.7340, 0.2660, 0.0000]),
               690: ([0.0227, 0.0082, 0.0000], [0.7344, 0.2656, 0.0000]),
               695: ([0.0158, 0.0057, 0.0000], [0.7346, 0.2654, 0.0000]),
               700: ([0.0114, 0.0041, 0.0000], [0.7347, 0.2653, 0.0000]),
               705: ([0.0081, 0.0029, 0.0000], [0.7347, 0.2653, 0.0000]),
               710: ([0.0058, 0.0021, 0.0000], [0.7347, 0.2653, 0.0000]),
               715: ([0.0041, 0.0015, 0.0000], [0.7347, 0.2653, 0.0000]),
               720: ([0.0029, 0.0010, 0.0000], [0.7347, 0.2653, 0.0000]),
               725: ([0.0020, 0.0007, 0.0000], [0.7347, 0.2653, 0.0000]),
               730: ([0.0014, 0.0005, 0.0000], [0.7347, 0.2653, 0.0000]),
               735: ([0.0010, 0.0004, 0.0000], [0.7347, 0.2653, 0.0000]),
               740: ([0.0007, 0.0002, 0.0000], [0.7347, 0.2653, 0.0000]),
               745: ([0.0005, 0.0002, 0.0000], [0.7347, 0.2653, 0.0000]),
               750: ([0.0003, 0.0001, 0.0000], [0.7347, 0.2653, 0.0000]),
               755: ([0.0002, 0.0001, 0.0000], [0.7347, 0.2653, 0.0000]),
               760: ([0.0002, 0.0001, 0.0000], [0.7347, 0.2653, 0.0000]),
               765: ([0.0001, 0.0000, 0.0000], [0.7347, 0.2653, 0.0000]),
               770: ([0.0001, 0.0000, 0.0000], [0.7347, 0.2653, 0.0000]),
               775: ([0.0001, 0.0000, 0.0000], [0.7347, 0.2653, 0.0000]),
               780: ([0.0000, 0.0000, 0.0000], [0.7347, 0.2653, 0.0000])},
        '10': {380: ([0.0002, 0.0000, 0.0007], [0.1813, 0.0197, 0.7990]),
               385: ([0.0007, 0.0001, 0.0029], [0.1809, 0.0195, 0.7996]),
               390: ([0.0024, 0.0003, 0.0105], [0.1803, 0.0194, 0.8003]),
               395: ([0.0072, 0.0008, 0.0323], [0.1795, 0.0190, 0.8015]),
               400: ([0.0191, 0.0020, 0.0860], [0.1784, 0.0187, 0.8029]),
               405: ([0.0434, 0.0045, 0.1971], [0.1771, 0.0184, 0.8045]),
               410: ([0.0847, 0.0088, 0.3894], [0.1755, 0.0181, 0.8064]),
               415: ([0.1406, 0.0145, 0.6568], [0.1732, 0.0178, 0.8090]),
               420: ([0.2045, 0.0214, 0.9725], [0.1706, 0.0179, 0.8115]),
               425: ([0.2647, 0.0295, 1.2825], [0.1679, 0.0187, 0.8134]),
               430: ([0.3147, 0.0387, 1.5535], [0.1650, 0.0203, 0.8147]),
               435: ([0.3577, 0.0496, 1.7985], [0.1622, 0.0225, 0.8153]),
               440: ([0.3837, 0.0621, 1.9673], [0.1590, 0.0257, 0.8153]),
               445: ([0.3867, 0.0747, 2.0273], [0.1554, 0.0300, 0.8146]),
               450: ([0.3707, 0.0895, 1.9948], [0.1510, 0.0364, 0.8126]),
               455: ([0.3430, 0.1063, 1.9007], [0.1459, 0.0452, 0.8089]),
               460: ([0.3023, 0.1282, 1.7454], [0.1389, 0.0589, 0.8022]),
               465: ([0.2541, 0.1528, 1.5549], [0.1295, 0.0779, 0.7926]),
               470: ([0.1956, 0.1852, 1.3176], [0.1152, 0.1090, 0.7758]),
               475: ([0.1323, 0.2199, 1.0302], [0.0957, 0.1591, 0.7452]),
               480: ([0.0805, 0.2536, 0.7721], [0.0728, 0.2292, 0.6980]),
               485: ([0.0411, 0.2977, 0.5701], [0.0452, 0.3275, 0.6273]),
               490: ([0.0162, 0.3391, 0.4153], [0.0210, 0.4401, 0.5389]),
               495: ([0.0051, 0.3954, 0.3024], [0.0073, 0.5625, 0.4302]),
               500: ([0.0038, 0.4608, 0.2185], [0.0056, 0.6745, 0.3199]),
               505: ([0.0154, 0.5314, 0.1592], [0.0219, 0.7526, 0.2255]),
               510: ([0.0375, 0.6067, 0.1120], [0.0495, 0.8023, 0.1482]),
               515: ([0.0714, 0.6857, 0.0822], [0.0850, 0.8170, 0.0980]),
               520: ([0.1177, 0.7618, 0.0607], [0.1252, 0.8102, 0.0646]),
               525: ([0.1730, 0.8233, 0.0431], [0.1664, 0.7922, 0.0414]),
               530: ([0.2365, 0.8752, 0.0305], [0.2071, 0.7663, 0.0266]),
               535: ([0.3042, 0.9238, 0.0206], [0.2436, 0.7399, 0.0165]),
               540: ([0.3768, 0.9620, 0.0137], [0.2786, 0.7113, 0.0101]),
               545: ([0.4516, 0.9822, 0.0079], [0.3132, 0.6813, 0.0055]),
               550: ([0.5298, 0.9918, 0.0040], [0.3473, 0.6501, 0.0026]),
               555: ([0.6161, 0.9991, 0.0011], [0.3812, 0.6182, 0.0006]),
               560: ([0.7052, 0.9973, 0.0000], [0.4142, 0.5858, 0.0000]),
               565: ([0.7938, 0.9824, 0.0000], [0.4469, 0.5531, 0.0000]),
               570: ([0.8787, 0.9556, 0.0000], [0.4790, 0.5210, 0.0000]),
               575: ([0.9512, 0.9152, 0.0000], [0.5096, 0.4904, 0.0000]),
               580: ([1.0142, 0.8689, 0.0000], [0.5386, 0.4614, 0.0000]),
               585: ([1.0743, 0.8256, 0.0000], [0.5654, 0.4346, 0.0000]),
               590: ([1.1185, 0.7774, 0.0000], [0.5900, 0.4100, 0.0000]),
               595: ([1.1343, 0.7204, 0.0000], [0.6116, 0.3884, 0.0000]),
               600: ([1.1240, 0.6583, 0.0000], [0.6306, 0.3694, 0.0000]),
               605: ([1.0891, 0.5939, 0.0000], [0.6471, 0.3529, 0.0000]),
               610: ([1.0305, 0.5280, 0.0000], [0.6612, 0.3388, 0.0000]),
               615: ([0.9507, 0.4618, 0.0000], [0.6731, 0.3269, 0.0000]),
               620: ([0.8563, 0.3981, 0.0000], [0.6827, 0.3173, 0.0000]),
               625: ([0.7549, 0.3396, 0.0000], [0.6898, 0.3102, 0.0000]),
               630: ([0.6475, 0.2835, 0.0000], [0.6955, 0.3045, 0.0000]),
               635: ([0.5351, 0.2283, 0.0000], [0.7010, 0.2990, 0.0000]),
               640: ([0.4316, 0.1798, 0.0000], [0.7059, 0.2941, 0.0000]),
               645: ([0.3437, 0.1402, 0.0000], [0.7102, 0.2898, 0.0000]),
               650: ([0.2683, 0.1076, 0.0000], [0.7137, 0.2863, 0.0000]),
               655: ([0.2043, 0.0812, 0.0000], [0.7156, 0.2844, 0.0000]),
               660: ([0.1526, 0.0603, 0.0000], [0.7168, 0.2832, 0.0000]),
               665: ([0.1122, 0.0441, 0.0000], [0.7179, 0.2821, 0.0000]),
               670: ([0.0813, 0.0318, 0.0000], [0.7187, 0.2813, 0.0000]),
               675: ([0.0579, 0.0226, 0.0000], [0.7193, 0.2807, 0.0000]),
               680: ([0.0409, 0.0159, 0.0000], [0.7198, 0.2802, 0.0000]),
               685: ([0.0286, 0.0111, 0.0000], [0.7200, 0.2800, 0.0000]),
               690: ([0.0199, 0.0077, 0.0000], [0.7202, 0.2798, 0.0000]),
               695: ([0.0138, 0.0054, 0.0000], [0.7203, 0.2797, 0.0000]),
               700: ([0.0096, 0.0037, 0.0000], [0.7204, 0.2796, 0.0000]),
               705: ([0.0066, 0.0026, 0.0000], [0.7203, 0.2797, 0.0000]),
               710: ([0.0046, 0.0018, 0.0000], [0.7202, 0.2798, 0.0000]),
               715: ([0.0031, 0.0012, 0.0000], [0.7201, 0.2799, 0.0000]),
               720: ([0.0022, 0.0008, 0.0000], [0.7199, 0.2801, 0.0000]),
               725: ([0.0015, 0.0006, 0.0000], [0.7197, 0.2803, 0.0000]),
               730: ([0.0010, 0.0004, 0.0000], [0.7194, 0.2806, 0.0000]),
               735: ([0.0007, 0.0003, 0.0000], [0.7192, 0.2808, 0.0000]),
               740: ([0.0005, 0.0002, 0.0000], [0.7189, 0.2811, 0.0000]),
               745: ([0.0004, 0.0001, 0.0000], [0.7186, 0.2814, 0.0000]),
               750: ([0.0003, 0.0001, 0.0000], [0.7183, 0.2817, 0.0000]),
               755: ([0.0002, 0.0001, 0.0000], [0.7180, 0.2820, 0.0000]),
               760: ([0.0001, 0.0000, 0.0000], [0.7176, 0.2824, 0.0000]),
               765: ([0.0001, 0.0000, 0.0000], [0.7172, 0.2828, 0.0000]),
               770: ([0.0001, 0.0000, 0.0000], [0.7169, 0.2831, 0.0000]),
               775: ([0.0000, 0.0000, 0.0000], [0.7165, 0.2835, 0.0000]),
               780: ([0.0000, 0.0000, 0.0000], [0.7161, 0.2839, 0.0000])}
    }


class ColXYZ(cB, RefXYZSpectral):
    """ XYZ color class
    Inherits from ColBase, XYZSpectral """
    lfields = ['X', 'Y', 'Z']

    def __init__(self, X=0.0, Y=0.0, Z=0.0, *args, **kwargs):    # default: Black
        """ Init self with X, Y, Z """
        cB.__init__(self, *args, **kwargs)
        self.type = 'XYZ'   # can be used instead of isinstance on an object
        self.X, self.Y, self.Z = X, Y, Z

    # CLASS PROPERTIES (IMPLEMENTED AS METHODS)
    @property
    def colorTemp(self):
        """ get color temperature in Kelvin (from XYZ values)
        :param self: ColXYZ object
        :return: Temperature in Kelvin """
        return getTempXYZ(self.X, self.Y, self.Z)

    @property
    def colorLum(self):
        """ get luminance from Y value
        :param self: ColXYZ object
        :return: Luminance in cd/m2 """
        return self.Y

    # TO COLOR SPACE (DIRECT)
    def toXYZ(self):
        """ :return: XYZ class from self """
        return self

    def toRGB(self):
        """ :return: RGB class from self """
        return cRGB.ColRGB(*XYZtoRGB(*self.refs(), rgb_space=self.RGBSpace))

    def toYxy(self):
        """ :return: Yxy class from self """
        return cYxy.ColYxy(*XYZtoYxy(*self.refs()))

    def toHunterLab(self):
        """ :return: Hunter-L*ab class from self """
        tmp = self.refs(), self     # append self for observer reference
        return cHunterLab.ColHunterLab(*XYZtoHunterLab(*tmp))

    def toCIELab(self):
        """ :return: CIE-L*ab class from self """
        tmp = self.refs(), self     # append self for observer reference
        return cCIELab.ColCIELab(*XYZtoCIELab(*tmp))

    def toCIELuv(self):
        """ :return: CIE-L*uv class from self """
        tmp = self.refs(), self     # append self for observer reference
        return cCIELuv.ColCIELuv(*XYZtoCIELuv(*tmp))

    # FROM COLOR SPACE (DIRECT)
    @cB.cancel_on(TypeError)
    def fromXYZ(self, *col):
        """ XYZ -> XYZ conversion
        :param col: either XYZ tuple or ColXYZ class
        :return: XYZ class """
        self.X, self.Y, self.Z = self._parse_input(ColXYZ, *col)
        return self

    @cB.cancel_on(TypeError)
    def fromRGB(self, *col):
        """ RGB -> XYZ conversion
        :param col: either RGB tuple or ColRGB class
        :return: XYZ class """
        self.X, self.Y, self.Z = RGBtoXYZ(*self._parse_input(cRGB.ColRGB, *col), rgb_space=self.RGBSpace)
        return self

    @cB.cancel_on(TypeError)
    def fromYxy(self, *col):
        """ Yxy -> XYZ conversion
        :param col: either Yxy tuple or ColYxy class
        :return: XYZ class """
        self.X, self.Y, self.Z = YxytoXYZ(*self._parse_input(cYxy.ColYxy, *col))
        return self

    @cB.cancel_on(TypeError)
    def fromHunterLab(self, *col):
        """ Hunter-L*ab -> XYZ conversion
        :param col: either Hunter-L*ab tuple or ColHunterLab class
        :return: XYZ class """
        tmp = self._parse_input(cHunterLab.ColHunterLab, *col)
        tmp.append(self)    # append self for observer reference
        self.X, self.Y, self.Z = HunterLabtoXYZ(*tmp)
        return self

    @cB.cancel_on(TypeError)
    def fromCIELab(self, *col):
        """ CIE-L*ab -> XYZ conversion
        :param col: either CIE-L*ab tuple or ColCIELab class
        :return: XYZ class """
        tmp = self._parse_input(cCIELab.ColCIELab, *col)
        tmp.append(self)    # append self for observer reference
        self.X, self.Y, self.Z = CIELabtoXYZ(*tmp)
        return self

    @cB.cancel_on(TypeError)
    def fromCIELuv(self, *col):
        """ CIE-L*uv -> XYZ conversion
        :param col: either CIE-L*uv tuple or ColCIELuv class
        :return: XYZ class """
        tmp = self._parse_input(cCIELuv.ColCIELuv, *col)
        tmp.append(self)    # append self for observer reference
        self.X, self.Y, self.Z = CIELuvtoXYZ(*tmp)
        return self


if __name__ == "__main__":
    col_XYZ = ColXYZ(0.1, 0.415, 0.2)
    print(col_XYZ)
    print(str(col_XYZ))

    print(col_XYZ.colorTemp)
    print(col_XYZ.colorLum)
