colormath-3.0.0/0000755000175000017500000000000013220777516016230 5ustar greg.taylorgreg.taylor00000000000000colormath-3.0.0/LICENSE.txt0000644000175000017500000000275213162226447020056 0ustar greg.taylorgreg.taylor00000000000000Copyright (c) 2014, Greg Taylor All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the DUO Interactive, LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. colormath-3.0.0/setup.cfg0000644000175000017500000000004613220777516020051 0ustar greg.taylorgreg.taylor00000000000000[egg_info] tag_build = tag_date = 0 colormath-3.0.0/PKG-INFO0000644000175000017500000000472613220777516017336 0ustar greg.taylorgreg.taylor00000000000000Metadata-Version: 1.1 Name: colormath Version: 3.0.0 Summary: Color math and conversion library. Home-page: https://github.com/gtaylor/python-colormath Author: Gregory Taylor Author-email: gtaylor@gc-taylor.com License: BSD Download-URL: http://pypi.python.org/pypi/colormath/ Description-Content-Type: UNKNOWN Description: Python Color Math Module (colormath) ==================================== .. image:: https://travis-ci.org/gtaylor/python-colormath.png?branch=master :target: https://travis-ci.org/gtaylor/python-colormath .. image:: https://pypip.in/d/colormath/badge.png :target: https://crate.io/packages/colormath/ This module implements a large number of different color operations such as color space conversions, Delta E, and density to spectral. Requirements ------------ * numpy * NetworkX 2.0+ * Python 2.7 or Python 3.5+ Installation ------------ The easiest way to install colormath is via pip/easy_install:: pip install colormath Documentation ------------- For documentation, see the project webpage at: http://python-colormath.readthedocs.org/ There are also a lot of useful examples under the examples directory within this directory. Support ------- Head over to https://github.com/gtaylor/python-colormath and submit an issue if you have any problems or questions. Legal Mumbo Jumbo ----------------- Copyright (C) 2018 `Gregory Taylor`_ This software is licensed under the BSD License. .. _Gregory Taylor: http://gc-taylor.com Keywords: color math conversions Platform: Platform Independent Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Natural Language :: English Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Topic :: Scientific/Engineering :: Mathematics Classifier: Topic :: Software Development :: Libraries :: Python Modules Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 colormath-3.0.0/colormath.egg-info/0000755000175000017500000000000013220777516021712 5ustar greg.taylorgreg.taylor00000000000000colormath-3.0.0/colormath.egg-info/top_level.txt0000644000175000017500000000001213220777516024435 0ustar greg.taylorgreg.taylor00000000000000colormath colormath-3.0.0/colormath.egg-info/requires.txt0000644000175000017500000000002413220777516024306 0ustar greg.taylorgreg.taylor00000000000000numpy networkx>=2.0 colormath-3.0.0/colormath.egg-info/PKG-INFO0000644000175000017500000000472613220777516023020 0ustar greg.taylorgreg.taylor00000000000000Metadata-Version: 1.1 Name: colormath Version: 3.0.0 Summary: Color math and conversion library. Home-page: https://github.com/gtaylor/python-colormath Author: Gregory Taylor Author-email: gtaylor@gc-taylor.com License: BSD Download-URL: http://pypi.python.org/pypi/colormath/ Description-Content-Type: UNKNOWN Description: Python Color Math Module (colormath) ==================================== .. image:: https://travis-ci.org/gtaylor/python-colormath.png?branch=master :target: https://travis-ci.org/gtaylor/python-colormath .. image:: https://pypip.in/d/colormath/badge.png :target: https://crate.io/packages/colormath/ This module implements a large number of different color operations such as color space conversions, Delta E, and density to spectral. Requirements ------------ * numpy * NetworkX 2.0+ * Python 2.7 or Python 3.5+ Installation ------------ The easiest way to install colormath is via pip/easy_install:: pip install colormath Documentation ------------- For documentation, see the project webpage at: http://python-colormath.readthedocs.org/ There are also a lot of useful examples under the examples directory within this directory. Support ------- Head over to https://github.com/gtaylor/python-colormath and submit an issue if you have any problems or questions. Legal Mumbo Jumbo ----------------- Copyright (C) 2018 `Gregory Taylor`_ This software is licensed under the BSD License. .. _Gregory Taylor: http://gc-taylor.com Keywords: color math conversions Platform: Platform Independent Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Natural Language :: English Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Topic :: Scientific/Engineering :: Mathematics Classifier: Topic :: Software Development :: Libraries :: Python Modules Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 colormath-3.0.0/colormath.egg-info/SOURCES.txt0000644000175000017500000000130513220777516023575 0ustar greg.taylorgreg.taylor00000000000000LICENSE.txt MANIFEST.in README.rst setup.py colormath/__init__.py colormath/chromatic_adaptation.py colormath/color_appearance_models.py colormath/color_constants.py colormath/color_conversions.py colormath/color_diff.py colormath/color_diff_matrix.py colormath/color_exceptions.py colormath/color_objects.py colormath/density.py colormath/density_standards.py colormath/spectral_constants.py colormath.egg-info/PKG-INFO colormath.egg-info/SOURCES.txt colormath.egg-info/dependency_links.txt colormath.egg-info/requires.txt colormath.egg-info/top_level.txt examples/color_appearance_model.py examples/conversions.py examples/delta_e.py examples/delta_e_matrix.py examples/density.py examples/example_config.pycolormath-3.0.0/colormath.egg-info/dependency_links.txt0000644000175000017500000000000113220777516025760 0ustar greg.taylorgreg.taylor00000000000000 colormath-3.0.0/colormath/0000755000175000017500000000000013220777516020220 5ustar greg.taylorgreg.taylor00000000000000colormath-3.0.0/colormath/color_constants.py0000644000175000017500000000311313162226447023777 0ustar greg.taylorgreg.taylor00000000000000""" Contains lookup tables, constants, and things that are generally static and useful throughout the library. """ import numpy # Not sure what these are, they are used in Lab and Luv calculations. CIE_E = 216.0 / 24389.0 CIE_K = 24389.0 / 27.0 # Observer Function and Illuminant Data ILLUMINANTS = { # 2 Degree Functions '2': { 'a': (1.09850, 1.00000, 0.35585), 'b': (0.99072, 1.00000, 0.85223), 'c': (0.98074, 1.00000, 1.18232), 'd50': (0.96422, 1.00000, 0.82521), 'd55': (0.95682, 1.00000, 0.92149), 'd65': (0.95047, 1.00000, 1.08883), 'd75': (0.94972, 1.00000, 1.22638), 'e': (1.00000, 1.00000, 1.00000), 'f2': (0.99186, 1.00000, 0.67393), 'f7': (0.95041, 1.00000, 1.08747), 'f11': (1.00962, 1.00000, 0.64350) }, # 10 Degree Functions '10': { 'd50': (0.9672, 1.000, 0.8143), 'd55': (0.958, 1.000, 0.9093), 'd65': (0.9481, 1.000, 1.073), 'd75': (0.94416, 1.000, 1.2064), } } OBSERVERS = ILLUMINANTS.keys() # Chromatic Adaptation Matrices # http://brucelindbloom.com/Eqn_ChromAdapt.html ADAPTATION_MATRICES = { 'xyz_scaling': numpy.array(( (1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0))), 'bradford': numpy.array(( (0.8951, 0.2664, -0.1614), (-0.7502, 1.7135, 0.0367), (0.0389, -0.0685, 1.0296))), 'von_kries': numpy.array(( (0.40024, 0.70760, -0.08081), (-0.22630, 1.16532, 0.04570), (0.00000, 0.00000, 0.91822))), } colormath-3.0.0/colormath/__init__.py0000644000175000017500000000002213220777307022321 0ustar greg.taylorgreg.taylor00000000000000VERSION = '3.0.0' colormath-3.0.0/colormath/color_diff_matrix.py0000644000175000017500000001262013220777307024263 0ustar greg.taylorgreg.taylor00000000000000""" This module contains the formulas for comparing Lab values with matrices and vectors. The benefit of using NumPy's matrix capabilities is speed. These calls can be used to efficiently compare large volumes of Lab colors. """ import numpy def delta_e_cie1976(lab_color_vector, lab_color_matrix): """ Calculates the Delta E (CIE1976) between `lab_color_vector` and all colors in `lab_color_matrix`. """ return numpy.sqrt( numpy.sum(numpy.power(lab_color_vector - lab_color_matrix, 2), axis=1)) # noinspection PyPep8Naming def delta_e_cie1994(lab_color_vector, lab_color_matrix, K_L=1, K_C=1, K_H=1, K_1=0.045, K_2=0.015): """ Calculates the Delta E (CIE1994) of two colors. K_l: 0.045 graphic arts 0.048 textiles K_2: 0.015 graphic arts 0.014 textiles K_L: 1 default 2 textiles """ C_1 = numpy.sqrt(numpy.sum(numpy.power(lab_color_vector[1:], 2))) C_2 = numpy.sqrt(numpy.sum(numpy.power(lab_color_matrix[:, 1:], 2), axis=1)) delta_lab = lab_color_vector - lab_color_matrix delta_L = delta_lab[:, 0].copy() delta_C = C_1 - C_2 delta_lab[:, 0] = delta_C delta_H_sq = numpy.sum(numpy.power(delta_lab, 2) * numpy.array([-1, 1, 1]), axis=1) # noinspection PyArgumentList delta_H = numpy.sqrt(delta_H_sq.clip(min=0)) S_L = 1 S_C = 1 + K_1 * C_1 S_H = 1 + K_2 * C_1 LCH = numpy.vstack([delta_L, delta_C, delta_H]) params = numpy.array([[K_L * S_L], [K_C * S_C], [K_H * S_H]]) return numpy.sqrt(numpy.sum(numpy.power(LCH / params, 2), axis=0)) # noinspection PyPep8Naming def delta_e_cmc(lab_color_vector, lab_color_matrix, pl=2, pc=1): """ Calculates the Delta E (CIE1994) of two colors. CMC values Acceptability: pl=2, pc=1 Perceptability: pl=1, pc=1 """ L, a, b = lab_color_vector C_1 = numpy.sqrt(numpy.sum(numpy.power(lab_color_vector[1:], 2))) C_2 = numpy.sqrt(numpy.sum(numpy.power(lab_color_matrix[:, 1:], 2), axis=1)) delta_lab = lab_color_vector - lab_color_matrix delta_L = delta_lab[:, 0].copy() delta_C = C_1 - C_2 delta_lab[:, 0] = delta_C H_1 = numpy.degrees(numpy.arctan2(b, a)) if H_1 < 0: H_1 += 360 F = numpy.sqrt(numpy.power(C_1, 4) / (numpy.power(C_1, 4) + 1900.0)) # noinspection PyChainedComparisons if 164 <= H_1 and H_1 <= 345: T = 0.56 + abs(0.2 * numpy.cos(numpy.radians(H_1 + 168))) else: T = 0.36 + abs(0.4 * numpy.cos(numpy.radians(H_1 + 35))) if L < 16: S_L = 0.511 else: S_L = (0.040975 * L) / (1 + 0.01765 * L) S_C = ((0.0638 * C_1) / (1 + 0.0131 * C_1)) + 0.638 S_H = S_C * (F * T + 1 - F) delta_C = C_1 - C_2 delta_H_sq = numpy.sum(numpy.power(delta_lab, 2) * numpy.array([-1, 1, 1]), axis=1) # noinspection PyArgumentList delta_H = numpy.sqrt(delta_H_sq.clip(min=0)) LCH = numpy.vstack([delta_L, delta_C, delta_H]) params = numpy.array([[pl * S_L], [pc * S_C], [S_H]]) return numpy.sqrt(numpy.sum(numpy.power(LCH / params, 2), axis=0)) # noinspection PyPep8Naming def delta_e_cie2000(lab_color_vector, lab_color_matrix, Kl=1, Kc=1, Kh=1): """ Calculates the Delta E (CIE2000) of two colors. """ L, a, b = lab_color_vector avg_Lp = (L + lab_color_matrix[:, 0]) / 2.0 C1 = numpy.sqrt(numpy.sum(numpy.power(lab_color_vector[1:], 2))) C2 = numpy.sqrt(numpy.sum(numpy.power(lab_color_matrix[:, 1:], 2), axis=1)) avg_C1_C2 = (C1 + C2) / 2.0 G = 0.5 * (1 - numpy.sqrt(numpy.power(avg_C1_C2, 7.0) / (numpy.power(avg_C1_C2, 7.0) + numpy.power(25.0, 7.0)))) a1p = (1.0 + G) * a a2p = (1.0 + G) * lab_color_matrix[:, 1] C1p = numpy.sqrt(numpy.power(a1p, 2) + numpy.power(b, 2)) C2p = numpy.sqrt(numpy.power(a2p, 2) + numpy.power(lab_color_matrix[:, 2], 2)) avg_C1p_C2p = (C1p + C2p) / 2.0 h1p = numpy.degrees(numpy.arctan2(b, a1p)) h1p += (h1p < 0) * 360 h2p = numpy.degrees(numpy.arctan2(lab_color_matrix[:, 2], a2p)) h2p += (h2p < 0) * 360 avg_Hp = (((numpy.fabs(h1p - h2p) > 180) * 360) + h1p + h2p) / 2.0 T = 1 - 0.17 * numpy.cos(numpy.radians(avg_Hp - 30)) + \ 0.24 * numpy.cos(numpy.radians(2 * avg_Hp)) + \ 0.32 * numpy.cos(numpy.radians(3 * avg_Hp + 6)) - \ 0.2 * numpy.cos(numpy.radians(4 * avg_Hp - 63)) diff_h2p_h1p = h2p - h1p delta_hp = diff_h2p_h1p + (numpy.fabs(diff_h2p_h1p) > 180) * 360 delta_hp -= (h2p > h1p) * 720 delta_Lp = lab_color_matrix[:, 0] - L delta_Cp = C2p - C1p delta_Hp = 2 * numpy.sqrt(C2p * C1p) * numpy.sin(numpy.radians(delta_hp) / 2.0) S_L = 1 + ((0.015 * numpy.power(avg_Lp - 50, 2)) / numpy.sqrt(20 + numpy.power(avg_Lp - 50, 2.0))) S_C = 1 + 0.045 * avg_C1p_C2p S_H = 1 + 0.015 * avg_C1p_C2p * T delta_ro = 30 * numpy.exp(-(numpy.power(((avg_Hp - 275) / 25), 2.0))) R_C = numpy.sqrt((numpy.power(avg_C1p_C2p, 7.0)) / (numpy.power(avg_C1p_C2p, 7.0) + numpy.power(25.0, 7.0))) R_T = -2 * R_C * numpy.sin(2 * numpy.radians(delta_ro)) return numpy.sqrt( numpy.power(delta_Lp / (S_L * Kl), 2) + numpy.power(delta_Cp / (S_C * Kc), 2) + numpy.power(delta_Hp / (S_H * Kh), 2) + R_T * (delta_Cp / (S_C * Kc)) * (delta_Hp / (S_H * Kh))) colormath-3.0.0/colormath/density.py0000644000175000017500000000467613220777307022264 0ustar greg.taylorgreg.taylor00000000000000""" Formulas for density calculation. """ from math import log10 from colormath.density_standards import ANSI_STATUS_T_BLUE, ANSI_STATUS_T_GREEN, \ ANSI_STATUS_T_RED, VISUAL_DENSITY_THRESH, ISO_VISUAL def ansi_density(color, density_standard): """ Calculates density for the given SpectralColor using the spectral weighting function provided. For example, ANSI_STATUS_T_RED. These may be found in :py:mod:`colormath.density_standards`. :param SpectralColor color: The SpectralColor object to calculate density for. :param numpy.ndarray std_array: NumPy array of filter of choice from :py:mod:`colormath.density_standards`. :rtype: float :returns: The density value for the given color and density standard. """ # Load the spec_XXXnm attributes into a Numpy array. sample = color.get_numpy_array() # Matrix multiplication intermediate = sample * density_standard # Sum the products. numerator = intermediate.sum() # This is the denominator in the density equation. sum_of_standard_wavelengths = density_standard.sum() # This is the top level of the density formula. return -1.0 * log10(numerator / sum_of_standard_wavelengths) def auto_density(color): """ Given a SpectralColor, automatically choose the correct ANSI T filter. Returns a tuple with a string representation of the filter the calculated density. :param SpectralColor color: The SpectralColor object to calculate density for. :rtype: float :returns: The density value, with the filter selected automatically. """ blue_density = ansi_density(color, ANSI_STATUS_T_BLUE) green_density = ansi_density(color, ANSI_STATUS_T_GREEN) red_density = ansi_density(color, ANSI_STATUS_T_RED) densities = [blue_density, green_density, red_density] min_density = min(densities) max_density = max(densities) density_range = max_density - min_density # See comments in density_standards.py for VISUAL_DENSITY_THRESH to # understand what this is doing. if density_range <= VISUAL_DENSITY_THRESH: return ansi_density(color, ISO_VISUAL) elif blue_density > green_density and blue_density > red_density: return blue_density elif green_density > blue_density and green_density > red_density: return green_density else: return red_density colormath-3.0.0/colormath/spectral_constants.py0000644000175000017500000003235113162226447024504 0ustar greg.taylorgreg.taylor00000000000000""" Contains lookup tables, constants, and things that are generally static and useful throughout the library. """ import numpy STDOBSERV_X2 = numpy.array(( 0.000000000000, 0.000000000000, 0.000129900000, 0.000414900000, 0.001368000000, 0.004243000000, 0.014310000000, 0.043510000000, 0.134380000000, 0.283900000000, 0.348280000000, 0.336200000000, 0.290800000000, 0.195360000000, 0.095640000000, 0.032010000000, 0.004900000000, 0.009300000000, 0.063270000000, 0.165500000000, 0.290400000000, 0.433449900000, 0.594500000000, 0.762100000000, 0.916300000000, 1.026300000000, 1.062200000000, 1.002600000000, 0.854449900000, 0.642400000000, 0.447900000000, 0.283500000000, 0.164900000000, 0.087400000000, 0.046770000000, 0.022700000000, 0.011359160000, 0.005790346000, 0.002899327000, 0.001439971000, 0.000690078600, 0.000332301100, 0.000166150500, 0.000083075270, 0.000041509940, 0.000020673830, 0.000010253980, 0.000005085868, 0.000002522525, 0.000001251141 )) STDOBSERV_Y2 = numpy.array(( 0.000000000000, 0.000000000000, 0.000003917000, 0.000012390000, 0.000039000000, 0.000120000000, 0.000396000000, 0.001210000000, 0.004000000000, 0.011600000000, 0.023000000000, 0.038000000000, 0.060000000000, 0.090980000000, 0.139020000000, 0.208020000000, 0.323000000000, 0.503000000000, 0.710000000000, 0.862000000000, 0.954000000000, 0.994950100000, 0.995000000000, 0.952000000000, 0.870000000000, 0.757000000000, 0.631000000000, 0.503000000000, 0.381000000000, 0.265000000000, 0.175000000000, 0.107000000000, 0.061000000000, 0.032000000000, 0.017000000000, 0.008210000000, 0.004102000000, 0.002091000000, 0.001047000000, 0.000520000000, 0.000249200000, 0.000120000000, 0.000060000000, 0.000030000000, 0.000014990000, 0.000007465700, 0.000003702900, 0.000001836600, 0.000000910930, 0.000000451810 )) STDOBSERV_Z2 = numpy.array(( 0.000000000000, 0.000000000000, 0.000606100000, 0.001946000000, 0.006450001000, 0.020050010000, 0.067850010000, 0.207400000000, 0.645600000000, 1.385600000000, 1.747060000000, 1.772110000000, 1.669200000000, 1.287640000000, 0.812950100000, 0.465180000000, 0.272000000000, 0.158200000000, 0.078249990000, 0.042160000000, 0.020300000000, 0.008749999000, 0.003900000000, 0.002100000000, 0.001650001000, 0.001100000000, 0.000800000000, 0.000340000000, 0.000190000000, 0.000049999990, 0.000020000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000 )) STDOBSERV_X10 = numpy.array(( 0.000000000000, 0.000000000000, 0.000000122200, 0.000005958600, 0.000159952000, 0.002361600000, 0.019109700000, 0.084736000000, 0.204492000000, 0.314679000000, 0.383734000000, 0.370702000000, 0.302273000000, 0.195618000000, 0.080507000000, 0.016172000000, 0.003816000000, 0.037465000000, 0.117749000000, 0.236491000000, 0.376772000000, 0.529826000000, 0.705224000000, 0.878655000000, 1.014160000000, 1.118520000000, 1.123990000000, 1.030480000000, 0.856297000000, 0.647467000000, 0.431567000000, 0.268329000000, 0.152568000000, 0.081260600000, 0.040850800000, 0.019941300000, 0.009576880000, 0.004552630000, 0.002174960000, 0.001044760000, 0.000508258000, 0.000250969000, 0.000126390000, 0.000064525800, 0.000033411700, 0.000017611500, 0.000009413630, 0.000005093470, 0.000002795310, 0.000001553140 )) STDOBSERV_Y10 = numpy.array(( 0.000000000000, 0.000000000000, 0.000000013398, 0.000000651100, 0.000017364000, 0.000253400000, 0.002004400000, 0.008756000000, 0.021391000000, 0.038676000000, 0.062077000000, 0.089456000000, 0.128201000000, 0.185190000000, 0.253589000000, 0.339133000000, 0.460777000000, 0.606741000000, 0.761757000000, 0.875211000000, 0.961988000000, 0.991761000000, 0.997340000000, 0.955552000000, 0.868934000000, 0.777405000000, 0.658341000000, 0.527963000000, 0.398057000000, 0.283493000000, 0.179828000000, 0.107633000000, 0.060281000000, 0.031800400000, 0.015905100000, 0.007748800000, 0.003717740000, 0.001768470000, 0.000846190000, 0.000407410000, 0.000198730000, 0.000098428000, 0.000049737000, 0.000025486000, 0.000013249000, 0.000007012800, 0.000003764730, 0.000002046130, 0.000001128090, 0.000000629700 )) STDOBSERV_Z10 = numpy.array(( 0.000000000000, 0.000000000000, 0.000000535027, 0.000026143700, 0.000704776000, 0.010482200000, 0.086010900000, 0.389366000000, 0.972542000000, 1.553480000000, 1.967280000000, 1.994800000000, 1.745370000000, 1.317560000000, 0.772125000000, 0.415254000000, 0.218502000000, 0.112044000000, 0.060709000000, 0.030451000000, 0.013676000000, 0.003988000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000, 0.000000000000 )) REFERENCE_ILLUM_A = numpy.array(( 3.59, 4.75, 6.15, 7.83, 9.80, 12.09, 14.72, 17.69, 21.01, 24.68, 28.71, 33.10, 37.82, 42.88, 48.25, 53.92, 59.87, 66.07, 72.50, 79.14, 85.95, 92.91, 100.00, 107.18, 114.43, 121.72, 129.03, 136.33, 143.60, 150.81, 157.95, 164.99, 171.92, 178.72, 185.38, 191.88, 198.20, 204.34, 210.29, 216.04, 221.58, 226.91, 232.02, 236.91, 241.57, 246.01, 250.21, 254.19, 257.95, 261.47 )) REFERENCE_ILLUM_B = numpy.array(( 2.40, 5.60, 9.60, 15.20, 22.40, 31.30, 41.30, 52.10, 63.20, 73.10, 80.80, 85.40, 88.30, 92.00, 95.20, 96.50, 94.20, 90.70, 89.50, 92.20, 96.90, 101.00, 102.80, 102.60, 101.00, 99.20, 98.00, 98.50, 99.70, 101.00, 102.20, 103.90, 105.00, 104.90, 103.90, 101.60, 99.10, 96.20, 92.90, 89.40, 86.90, 85.20, 84.70, 85.40, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) REFERENCE_ILLUM_C = numpy.array(( 2.70, 7.00, 12.90, 21.40, 33.00, 47.40, 63.30, 80.60, 98.10, 112.40, 121.50, 124.00, 123.10, 123.80, 123.90, 120.70, 112.10, 102.30, 96.90, 98.00, 102.10, 105.20, 105.30, 102.30, 97.80, 93.20, 89.70, 88.40, 88.10, 88.00, 87.80, 88.20, 87.90, 86.30, 84.00, 80.20, 76.30, 72.40, 68.30, 64.40, 61.50, 59.20, 58.10, 58.20, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) REFERENCE_ILLUM_D50 = numpy.array(( 17.92, 20.98, 23.91, 25.89, 24.45, 29.83, 49.25, 56.45, 59.97, 57.76, 74.77, 87.19, 90.56, 91.32, 95.07, 91.93, 95.70, 96.59, 97.11, 102.09, 100.75, 102.31, 100.00, 97.74, 98.92, 93.51, 97.71, 99.29, 99.07, 95.75, 98.90, 95.71, 98.24, 103.06, 99.19, 87.43, 91.66, 92.94, 76.89, 86.56, 92.63, 78.27, 57.72, 82.97, 78.31, 79.59, 73.44, 63.95, 70.81, 74.48 )) REFERENCE_ILLUM_D65 = numpy.array(( 39.90, 44.86, 46.59, 51.74, 49.92, 54.60, 82.69, 91.42, 93.37, 86.63, 104.81, 116.96, 117.76, 114.82, 115.89, 108.78, 109.33, 107.78, 104.78, 107.68, 104.40, 104.04, 100.00, 96.34, 95.79, 88.69, 90.02, 89.61, 87.71, 83.30, 83.72, 80.05, 80.24, 82.30, 78.31, 69.74, 71.63, 74.37, 61.62, 69.91, 75.11, 63.61, 46.43, 66.83, 63.40, 64.32, 59.47, 51.97, 57.46, 60.33 )) REFERENCE_ILLUM_E = numpy.array(( 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, )) REFERENCE_ILLUM_F2 = numpy.array(( 0.00, 0.00, 0.00, 0.00, 1.18, 1.84, 3.44, 3.85, 4.19, 5.06, 11.81, 6.63, 7.19, 7.54, 7.65, 7.62, 7.28, 7.05, 7.16, 8.04, 10.01, 16.64, 16.16, 18.62, 22.79, 18.66, 16.54, 13.80, 10.95, 8.40, 6.31, 4.68, 3.45, 2.55, 1.89, 1.53, 1.10, 0.88, 0.68, 0.56, 0.51, 0.47, 0.46, 0.40, 0.27, 0.00, 0.00, 0.00, 0.00, 0.00 )) REFERENCE_ILLUM_F7 = numpy.array(( 0.00, 0.00, 0.00, 0.00, 2.56, 3.84, 6.15, 7.37, 7.71, 9.15, 17.52, 12.00, 13.08, 13.71, 13.95, 13.82, 13.43, 13.08, 12.78, 12.44, 12.26, 17.05, 12.58, 12.83, 16.75, 12.67, 12.19, 11.60, 11.12, 10.76, 10.11, 10.02, 9.87, 7.27, 5.83, 5.04, 4.12, 3.46, 2.73, 2.25, 1.90, 1.62, 1.45, 1.17, 0.81, 0.00, 0.00, 0.00, 0.00, 0.00 )) REFERENCE_ILLUM_F11 = numpy.array(( 0.00, 0.00, 0.00, 0.00, 0.91, 0.46, 1.29, 1.59, 2.46, 4.49, 12.13, 7.19, 6.72, 5.46, 5.66, 14.96, 4.72, 1.47, 0.89, 1.18, 39.59, 32.61, 2.83, 1.67, 11.28, 12.73, 7.33, 55.27, 13.18, 12.26, 2.07, 3.58, 2.48, 1.54, 1.46, 2.00, 1.35, 5.58, 0.57, 0.23, 0.24, 0.20, 0.32, 0.16, 0.09, 0.00, 0.00, 0.00, 0.00, 0.00 )) REFERENCE_ILLUM_BLACKBODY = numpy.array(( 43.36, 47.77, 52.15, 56.44, 60.62, 64.65, 68.51, 72.18, 75.63, 78.87, 81.87, 84.63, 87.16, 89.44, 91.48, 93.29, 94.87, 96.23, 97.37, 98.31, 99.05, 99.61, 100.00, 100.22, 100.29, 100.21, 100.00, 99.67, 99.22, 98.67, 98.02, 97.28, 96.47, 95.58, 94.63, 93.62, 92.56, 91.45, 90.30, 89.12, 87.91, 86.67, 85.42, 84.15, 82.86, 81.56, 80.26, 78.95, 77.64, 76.33 )) # This table is used to match up illuminants to spectral distributions above. # It should correspond to a ColorObject.illuminant attribute. REF_ILLUM_TABLE = { 'a': REFERENCE_ILLUM_A, 'b': REFERENCE_ILLUM_B, 'c': REFERENCE_ILLUM_C, 'd50': REFERENCE_ILLUM_D50, 'd65': REFERENCE_ILLUM_D65, 'e': REFERENCE_ILLUM_E, 'f2': REFERENCE_ILLUM_F2, 'f7': REFERENCE_ILLUM_F7, 'f11': REFERENCE_ILLUM_F11, 'blackbody': REFERENCE_ILLUM_BLACKBODY, }colormath-3.0.0/colormath/color_exceptions.py0000644000175000017500000000214013162226447024143 0ustar greg.taylorgreg.taylor00000000000000""" This module contains exceptions for use throughout the L11 Colorlib. """ class ColorMathException(Exception): """ Base exception for all colormath exceptions. """ pass class UndefinedConversionError(ColorMathException): """ Raised when the user asks for a color space conversion that does not exist. """ def __init__(self, cobj, cs_to): super(UndefinedConversionError, self).__init__(cobj, cs_to) self.message = "Conversion from %s to %s is not defined." % (cobj, cs_to) class InvalidIlluminantError(ColorMathException): """ Raised when an invalid illuminant is set on a ColorObj. """ def __init__(self, illuminant): super(InvalidIlluminantError, self).__init__(illuminant) self.message = "Invalid illuminant specified: %s" % illuminant class InvalidObserverError(ColorMathException): """ Raised when an invalid observer is set on a ColorObj. """ def __init__(self, cobj): super(InvalidObserverError, self).__init__(cobj) self.message = "Invalid observer angle specified: %s" % cobj.observer colormath-3.0.0/colormath/color_appearance_models.py0000644000175000017500000013444213220777307025440 0ustar greg.taylorgreg.taylor00000000000000""" Copyright (c) 2014, Michael Mauderer, University of St Andrews All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the University of St Andrews nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ from __future__ import division import logging import numpy logger = logging.getLogger(__name__) class Nayatani95(object): """ **References** * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons. * Nayatani, Y., Sobagaki, H., & Yano, K. H. T. (1995). Lightness dependency of chroma scales of a nonlinear color-appearance model and its latest formulation. *Color Research & Application*, 20(3), 156-167. """ @property def hue_angle(self): """ Predicted hue angle :math:`\\theta`. """ return self._hue_angle @property def chroma(self): """ Predicted chroma :math:`C`. """ return self._chroma @property def saturation(self): """ Predicted saturation :math:`S`. """ return self._saturation @property def brightness(self): """ Predicted brightness :math:`B_r`. """ return self._brightness @property def colorfulness(self): """ Predicted colorfulness :math:`M`. """ return self._colorfulness def __init__(self, x, y, z, x_n, y_n, z_n, y_ob, e_o, e_or, n=1): """ :param x: X value of test sample :math:`X`. :param y: Y value of test sample :math:`Y`. :param z: Z value of test sample :math:`Z`. :param x_n: X value of reference white :math:`X_n`. :param y_n: Y value of reference white :math:`Y_n`. :param z_n: Z value of reference white :math:`Z_n`. :param y_ob: Luminance factor of achromatic background as percentage :math:`Y_o`. Required to be larger than 0.18. :param e_o: Illuminance of the viewing field :math:`E_o` in lux. :param e_or: Normalising illuminance :math:`E_or` in lux. :param n: Noise term :math:`n`. """ if numpy.any(y_ob <= 0.18): raise ValueError('y_ob hast be greater than 0.18.') l_o = y_ob * e_o / (100 * numpy.pi) l_or = y_ob * e_or / (100 * numpy.pi) logger.debug('L_o: {}'.format(l_o)) logger.debug('L_or: {}'.format(l_or)) x_o = x_n / (x_n + y_n + z_n) y_o = y_n / (x_n + y_n + z_n) logger.debug('x_o: {}'.format(x_o)) logger.debug('y_o: {}'.format(y_o)) xi = (0.48105 * x_o + 0.78841 * y_o - 0.08081) / y_o eta = (-0.27200 * x_o + 1.11962 * y_o + 0.04570) / y_o zeta = (0.91822 * (1 - x_o - y_o)) / y_o logger.debug('xi: {}'.format(xi)) logger.debug('eta: {}'.format(eta)) logger.debug('zeta: {}'.format(zeta)) r_0, g_0, b_0 = rgb_0 = ((y_ob * e_o) / (100 * numpy.pi)) * numpy.array([xi, eta, zeta]) logger.debug('rgb_0: {}'.format(rgb_0)) r, g, b, = rgb = self.xyz_to_rgb(numpy.array([x, y, z])) logger.debug('rgb: {}'.format(rgb)) e_r = self._compute_scaling_coefficient(r, xi) logger.debug('e(R): {}'.format(e_r)) e_g = self._compute_scaling_coefficient(g, eta) logger.debug('e(G): {}'.format(e_g)) beta_r = self._beta_1(r_0) logger.debug('beta1(rho): {}'.format(beta_r)) beta_g = self._beta_1(g_0) logger.debug('beta1(eta): {}'.format(beta_g)) beta_b = self._beta_2(b_0) logger.debug('beta2(zeta): {}'.format(beta_b)) beta_l = self._beta_1(l_or) logger.debug('beta1(L_or): {}'.format(beta_l)) # Opponent Color Dimension self._achromatic_response = (2 / 3) * beta_r * e_r * numpy.log10((r + n) / (20 * xi + n)) self._achromatic_response += (1 / 3) * beta_g * e_g * numpy.log10((g + n) / (20 * eta + n)) self._achromatic_response *= 41.69 / beta_l logger.debug('Q: {}'.format(self._achromatic_response)) self._tritanopic_response = (1 / 1) * beta_r * numpy.log10((r + n) / (20 * xi + n)) self._tritanopic_response += - (12 / 11) * beta_g * numpy.log10((g + n) / (20 * eta + n)) self._tritanopic_response += (1 / 11) * beta_b * numpy.log10((b + n) / (20 * zeta + n)) logger.debug('t: {}'.format(self._tritanopic_response)) self._protanopic_response = (1 / 9) * beta_r * numpy.log10((r + n) / (20 * xi + n)) self._protanopic_response += (1 / 9) * beta_g * numpy.log10((g + n) / (20 * eta + n)) self._protanopic_response += - (2 / 9) * beta_b * numpy.log10((b + n) / (20 * zeta + n)) logger.debug('p: {}'.format(self._protanopic_response)) # Brightness self._brightness = (50 / beta_l) * ((2 / 3) * beta_r + (1 / 3) * beta_g) + self._achromatic_response self._brightness_ideal_white = (2 / 3) * beta_r * 1.758 * numpy.log10((100 * xi + n) / (20 * xi + n)) self._brightness_ideal_white += (1 / 3) * beta_g * 1.758 * numpy.log10((100 * eta + n) / (20 * eta + n)) self._brightness_ideal_white *= 41.69 / beta_l self._brightness_ideal_white += (50 / beta_l) * (2 / 3) * beta_r self._brightness_ideal_white += (50 / beta_l) * (1 / 3) * beta_g # Lightness self._lightness_achromatic = self._achromatic_response + 50 self._lightness_achromatic_normalized = 100 * (self._brightness / self._brightness_ideal_white) # Hue hue_angle_rad = numpy.arctan2(self._protanopic_response, self._tritanopic_response) self._hue_angle = ((360 * hue_angle_rad / (2 * numpy.pi)) + 360) % 360 logger.debug('theta: {}'.format(self._hue_angle)) e_s_theta = self.chromatic_strength(hue_angle_rad) logger.debug('E_s(theta): {}'.format(e_s_theta)) # Saturation self._saturation_rg = (488.93 / beta_l) * e_s_theta * self._tritanopic_response self._saturation_yb = (488.93 / beta_l) * e_s_theta * self._protanopic_response logger.debug('S_RG: {}'.format(self._saturation_rg)) logger.debug('S_YB: {}'.format(self._saturation_yb)) self._saturation = numpy.sqrt((self._saturation_rg ** 2) + (self._saturation_yb ** 2)) logger.debug('S: {}'.format(self._saturation)) # Chroma self._chroma_rg = ((self._lightness_achromatic / 50) ** 0.7) * self._saturation_rg self._chroma_yb = ((self._lightness_achromatic / 50) ** 0.7) * self._saturation_yb self._chroma = ((self._lightness_achromatic / 50) ** 0.7) * self._saturation logger.debug('C: {}'.format(self._chroma)) # Colorfulness self._colorfulness_rg = self._chroma_rg * self._brightness_ideal_white / 100 self._colorfulness_yb = self._chroma_yb * self._brightness_ideal_white / 100 self._colorfulness = self._chroma * self._brightness_ideal_white / 100 @staticmethod def chromatic_strength(angle): result = 0.9394 result += - 0.2478 * numpy.sin(1 * angle) result += - 0.0743 * numpy.sin(2 * angle) result += + 0.0666 * numpy.sin(3 * angle) result += - 0.0186 * numpy.sin(4 * angle) result += - 0.0055 * numpy.cos(1 * angle) result += - 0.0521 * numpy.cos(2 * angle) result += - 0.0573 * numpy.cos(3 * angle) result += - 0.0061 * numpy.cos(4 * angle) return result @staticmethod def _compute_scaling_coefficient(a, b): return numpy.where(a >= (20 * b), 1.758, 1) @staticmethod def _beta_1(x): return (6.469 + 6.362 * (x ** 0.4495)) / (6.469 + (x ** 0.4495)) @staticmethod def _beta_2(x): return 0.7844 * (8.414 + 8.091 * (x ** 0.5128)) / (8.414 + (x ** 0.5128)) xyz_to_rgb_m = numpy.array([[0.40024, 0.70760, -0.08081], [-0.22630, 1.16532, 0.04570], [0, 0, 0.91822]]) @classmethod def xyz_to_rgb(cls, xyz): return cls.xyz_to_rgb_m.dot(xyz) class Hunt(object): """ **References** * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons. * Hunt, R. W. G. (2005). *The reproduction of colour*. 5th Ed., John Wiley & Sons. """ @property def hue_angle(self): """ Predicted hue angle :math:`h_s`. """ return self._hue_angle @property def chroma(self): """ Predicted chroma :math:`C_{94}`. """ return self._chroma @property def saturation(self): """ Predicted saturation :math:`s`. """ return self._saturation @property def brightness(self): """ Predicted brightness :math:`Q`. """ return self._brightness @property def colorfulness(self): """ Predicted colorfulness :math:`M_{94}`. """ return self._colorfulness @property def lightness(self): """ Predicted colorfulness :math:`J`. """ return self._lightness def __init__(self, x, y, z, x_b, y_b, z_b, x_w, y_w, z_w, l_a, n_c, n_b, l_as=None, cct_w=None, n_cb=None, n_bb=None, x_p=None, y_p=None, z_p=None, p=None, helson_judd=False, discount_illuminant=True, s=None, s_w=None): """ :param x: X value of test sample :math:`X`. :param y: Y value of test sample :math:`Y`. :param z: Z value of test sample :math:`Z`. :param x_b: X value of background :math:`X_b`. :param y_b: Y value of background :math:`Y_b`. :param z_b: Z value of background :math:`Z_b`. :param x_w: X value of reference white :math:`X_W`. :param y_w: Y value of reference white :math:`Y_W`. :param z_w: Z value of reference white :math:`Z_W`. :param l_a: Adapting luminance :math:`L_A`. :param n_c: Chromatic surround induction_factor :math:`N_c`. :param n_b: Brightness surround induction factor :math:`N_b`. :param l_as: Scotopic luminance of the illuminant :math:`L_{AS}`. Will be approximated if not supplied. :param cct_w: Correlated color temperature of illuminant :math:`T`. Will be used to approximate l_as if not supplied. :param n_cb: Chromatic background induction factor :math:`N_{cb}`. Will be approximated using y_w and y_b if not supplied. :param n_bb: Brightness background induction factor :math:`N_{bb}`. Will be approximated using y_w and y_b if not supplied. :param x_p: X value of proxima field :math:`X_p`. If not supplied, will be assumed to equal background. :param y_p: Y value of proxima field :math:`Y_p`. If not supplied, will be assumed to equal background. :param z_p: Z value of proxima field :math:`Z_p`. If not supplied, will be assumed to equal background. :param p: Simultaneous contrast/assimilation parameter. :param helson_judd: Truth value indicating whether the Heslon-Judd effect should be accounted for. Default False. :param discount_illuminant: Truth value whether discount-the-illuminant should be applied. Default True. :param s: Scotopic response to the stimulus. :param s_w: Scotopic response for th reference white. :raises ValueError: if illegal parameter combination is supplied. """ if x_p is None: x_p = x_b logger.warn('Approximated x_p with x_b.') if y_p is None: y_p = y_b logger.warn('Approximated y_p with y_b.') if z_p is None: z_p = y_b logger.warn('Approximated z_p with z_b.') if n_cb is None: n_cb = 0.725 * (y_w / y_b) ** 0.2 logger.warn('Approximated n_cb.') logger.debug('N_cb: {}'.format(n_cb)) if n_bb is None: n_bb = 0.725 * (y_w / y_b) ** 0.2 logger.warn('Approximated n_bb.') logger.debug('N_bb: {}'.format(n_cb)) if l_as is None: logger.warn('Approximated scotopic luminance.') if cct_w is None: cct_w = self._get_cct(x_w, y_w, z_w) logger.warn('Approximated cct_w: {}'.format(cct_w)) l_as = 2.26 * l_a l_as *= ((cct_w / 4000) - 0.4) ** (1 / 3) logger.debug('LA_S: {}'.format(l_as)) if s is None != s_w is None: raise ValueError("Either both scotopic responses (s, s_w) need to be supplied or none.") elif s is None and s_w is None: s = y s_w = y_w logger.warn('Approximated scotopic response to stimulus and reference white.') if p is None: logger.warn('p not supplied. Model will not account for simultaneous chromatic contrast .') xyz = numpy.array([x, y, z]) logger.debug('XYZ: {}'.format(xyz)) xyz_w = numpy.array([x_w, y_w, z_w]) logger.debug('XYZ_W: {}'.format(xyz_w)) xyz_b = numpy.array([x_b, y_b, z_b]) xyz_p = numpy.array([x_p, y_p, z_p]) k = 1 / (5 * l_a + 1) logger.debug('k: {}'.format(k)) # luminance adaptation factor f_l = 0.2 * (k ** 4) * (5 * l_a) + 0.1 * ((1 - (k ** 4)) ** 2) * ((5 * l_a) ** (1 / 3)) logger.debug('F_L: {}'.format(f_l)) logger.debug('--- Stimulus RGB adaptation start ----') rgb_a = self._adaptation(f_l, l_a, xyz, xyz_w, xyz_b, xyz_p, p, helson_judd, discount_illuminant) logger.debug('--- Stimulus RGB adaptation end ----') r_a, g_a, b_a = rgb_a logger.debug('RGB_A: {}'.format(rgb_a)) logger.debug('--- White RGB adaptation start ----') rgb_aw = self._adaptation(f_l, l_a, xyz_w, xyz_w, xyz_b, xyz_p, p, helson_judd, discount_illuminant) logger.debug('--- White RGB adaptation end ----') r_aw, g_aw, b_aw = rgb_aw logger.debug('RGB_AW: {}'.format(rgb_aw)) # --------------------------- # Opponent Color Dimensions # --------------------------- # achromatic_cone_signal a_a = 2 * r_a + g_a + (1 / 20) * b_a - 3.05 + 1 logger.debug('A_A: {}'.format(a_a)) a_aw = 2 * r_aw + g_aw + (1 / 20) * b_aw - 3.05 + 1 logger.debug('A_AW: {}'.format(a_aw)) c1 = r_a - g_a logger.debug('C1: {}'.format(c1)) c2 = g_a - b_a logger.debug('C2: {}'.format(c2)) c3 = b_a - r_a logger.debug('C3: {}'.format(c3)) c1_w = r_aw - g_aw logger.debug('C1_W: {}'.format(c1_w)) c2_w = g_aw - b_aw logger.debug('C2_W: {}'.format(c2_w)) c3_w = b_aw - r_aw logger.debug('C3_W: {}'.format(c3_w)) # ----- # Hue # ----- self._hue_angle = (180 * numpy.arctan2(0.5 * (c2 - c3) / 4.5, c1 - (c2 / 11)) / numpy.pi) % 360 hue_angle_w = (180 * numpy.arctan2(0.5 * (c2_w - c3_w) / 4.5, c1_w - (c2_w / 11)) / numpy.pi) % 360 # ------------- # Saturation # ------------- e_s = self._calculate_eccentricity_factor(self.hue_angle) logger.debug('es: {}'.format(e_s)) e_s_w = self._calculate_eccentricity_factor(hue_angle_w) f_t = l_a / (l_a + 0.1) logger.debug('F_t: {}'.format(f_t)) m_yb = 100 * (0.5 * (c2 - c3) / 4.5) * (e_s * (10 / 13) * n_c * n_cb * f_t) logger.debug('m_yb: {}'.format(m_yb)) m_rg = 100 * (c1 - (c2 / 11)) * (e_s * (10 / 13) * n_c * n_cb) logger.debug('m_rg: {}'.format(m_rg)) m = ((m_rg ** 2) + (m_yb ** 2)) ** 0.5 logger.debug('m: {}'.format(m)) self._saturation = 50 * m / rgb_a.sum(axis=0) m_yb_w = 100 * (0.5 * (c2_w - c3_w) / 4.5) * (e_s_w * (10 / 13) * n_c * n_cb * f_t) m_rg_w = 100 * (c1_w - (c2_w / 11)) * (e_s_w * (10 / 13) * n_c * n_cb) m_w = ((m_rg_w ** 2) + (m_yb_w ** 2)) ** 0.5 # ------------ # Brightness # ------------ logger.debug('--- Stimulus achromatic signal START ----') a = self._calculate_achromatic_signal(l_as, s, s_w, n_bb, a_a) logger.debug('--- Stimulus achromatic signal END ----') logger.debug('A: {}'.format(a)) logger.debug('--- White achromatic signal START ----') a_w = self._calculate_achromatic_signal(l_as, s_w, s_w, n_bb, a_aw) logger.debug('--- White achromatic signal END ----') logger.debug('A_w: {}'.format(a_w)) n1 = ((7 * a_w) ** 0.5) / (5.33 * n_b ** 0.13) n2 = (7 * a_w * n_b ** 0.362) / 200 logger.debug('N1: {}'.format(n1)) logger.debug('N2: {}'.format(n2)) self._brightness = ((7 * (a + (m / 100))) ** 0.6) * n1 - n2 brightness_w = ((7 * (a_w + (m_w / 100))) ** 0.6) * n1 - n2 logger.debug('Q: {}'.format(self.brightness)) logger.debug('Q_W: {}'.format(brightness_w)) # ---------- # Lightness # ---------- z = 1 + (y_b / y_w) ** 0.5 logger.debug('z: {}'.format(z)) self._lightness = 100 * (self.brightness / brightness_w) ** z # ------- # Chroma # ------- self._chroma = 2.44 * (self.saturation ** 0.69) * ((self.brightness / brightness_w) ** (y_b / y_w)) * ( 1.64 - 0.29 ** (y_b / y_w)) # ------------- # Colorfulness # ------------- self._colorfulness = (f_l ** 0.15) * self.chroma xyz_to_rgb_m = numpy.array([[0.38971, 0.68898, -0.07868], [-0.22981, 1.18340, 0.04641], [0, 0, 1]]) @classmethod def xyz_to_rgb(cls, xyz): return cls.xyz_to_rgb_m.dot(xyz) def _adaptation(self, f_l, l_a, xyz, xyz_w, xyz_b, xyz_p=None, p=None, helson_judd=False, discount_illuminant=True): """ :param f_l: Luminance adaptation factor :param l_a: Adapting luminance :param xyz: Stimulus color in XYZ :param xyz_w: Reference white color in XYZ :param xyz_b: Background color in XYZ :param xyz_p: Proxima field color in XYZ :param p: Simultaneous contrast/assimilation parameter. """ rgb = self.xyz_to_rgb(xyz) logger.debug('RGB: {}'.format(rgb)) rgb_w = self.xyz_to_rgb(xyz_w) logger.debug('RGB_W: {}'.format(rgb_w)) y_w = xyz_w[1] y_b = xyz_b[1] h_rgb = 3 * rgb_w / (rgb_w.sum()) logger.debug('H_RGB: {}'.format(h_rgb)) # Chromatic adaptation factors if not discount_illuminant: f_rgb = (1 + (l_a ** (1 / 3)) + h_rgb) / (1 + (l_a ** (1 / 3)) + (1 / h_rgb)) else: f_rgb = numpy.ones(numpy.shape(h_rgb)) logger.debug('F_RGB: {}'.format(f_rgb)) # Adaptation factor if helson_judd: d_rgb = self._f_n((y_b / y_w) * f_l * f_rgb[1]) - self._f_n((y_b / y_w) * f_l * f_rgb) assert d_rgb[1] == 0 else: d_rgb = numpy.zeros(numpy.shape(f_rgb)) logger.debug('D_RGB: {}'.format(d_rgb)) # Cone bleaching factors rgb_b = (10 ** 7) / ((10 ** 7) + 5 * l_a * (rgb_w / 100)) logger.debug('B_RGB: {}'.format(rgb_b)) if xyz_p is not None and p is not None: logger.debug('Account for simultaneous chromatic contrast') rgb_p = self.xyz_to_rgb(xyz_p) rgb_w = self.adjust_white_for_scc(rgb_p, rgb_b, rgb_w, p) # Adapt rgb using modified rgb_a = 1 + rgb_b * (self._f_n(f_l * f_rgb * rgb / rgb_w) + d_rgb) logger.debug('RGB_A: {}'.format(rgb_a)) return rgb_a @classmethod def adjust_white_for_scc(cls, rgb_p, rgb_b, rgb_w, p): """ Adjust the white point for simultaneous chromatic contrast. :param rgb_p: Cone signals of proxima field. :param rgb_b: Cone signals of background. :param rgb_w: Cone signals of reference white. :param p: Simultaneous contrast/assimilation parameter. :return: Adjusted cone signals for reference white. """ p_rgb = rgb_p / rgb_b rgb_w = rgb_w * (((1 - p) * p_rgb + (1 + p) / p_rgb) ** 0.5) / (((1 + p) * p_rgb + (1 - p) / p_rgb) ** 0.5) return rgb_w @staticmethod def _get_cct(x, y, z): """ Reference Hernandez-Andres, J., Lee, R. L., & Romero, J. (1999). Calculating correlated color temperatures across the entire gamut of daylight and skylight chromaticities. Applied Optics, 38(27), 5703-5709. """ x_e = 0.3320 y_e = 0.1858 n = ((x / (x + z + z)) - x_e) / ((y / (x + z + z)) - y_e) a_0 = -949.86315 a_1 = 6253.80338 a_2 = 28.70599 a_3 = 0.00004 t_1 = 0.92159 t_2 = 0.20039 t_3 = 0.07125 cct = a_0 + a_1 * numpy.exp(-n / t_1) + a_2 * numpy.exp(-n / t_2) + a_3 * numpy.exp(-n / t_3) return cct @staticmethod def calculate_scotopic_luminance(photopic_luminance, color_temperature): return 2.26 * photopic_luminance * ((color_temperature / 4000) - 0.4) ** (1 / 3) @classmethod def _calculate_achromatic_signal(cls, l_as, s, s_w, n_bb, a_a): j = 0.00001 / ((5 * l_as / 2.26) + 0.00001) logger.debug('j: {}'.format(j)) f_ls = 3800 * (j ** 2) * (5 * l_as / 2.26) f_ls += 0.2 * ((1 - (j ** 2)) ** 0.4) * ((5 * l_as / 2.26) ** (1 / 6)) logger.debug('F_LS: {}'.format(f_ls)) b_s = 0.5 / (1 + 0.3 * ((5 * l_as / 2.26) * (s / s_w)) ** 0.3) b_s += 0.5 / (1 + 5 * (5 * l_as / 2.26)) logger.debug('B_S: {}'.format(b_s)) a_s = (cls._f_n(f_ls * s / s_w) * 3.05 * b_s) + 0.3 logger.debug('A_S: {}'.format(a_s)) return n_bb * (a_a - 1 + a_s - 0.3 + numpy.sqrt((1 + (0.3 ** 2)))) @staticmethod def _f_n(i): """ Nonlinear response function. """ return 40 * ((i ** 0.73) / (i ** 0.73 + 2)) @staticmethod def _calculate_eccentricity_factor(hue_angle): h = numpy.array([20.14, 90, 164.25, 237.53]) e = numpy.array([0.8, 0.7, 1.0, 1.2]) out = numpy.interp(hue_angle, h, e) out = numpy.where(hue_angle < 20.14, 0.856 - (hue_angle / 20.14) * 0.056, out) out = numpy.where(hue_angle > 237.53, 0.856 + 0.344 * (360 - hue_angle) / (360 - 237.53), out) return out class RLAB(object): """ **References** * Fairchild, M. D. (1996). Refinement of the RLAB color space. *Color Research & Application*, 21(5), 338-346. * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons. """ @property def hue_angle(self): """ Predicted hue angle :math:`h^R`. """ return self._hue_angle @property def chroma(self): """ Predicted chroma :math:`C^R`. """ return self._chroma @property def saturation(self): """ Predicted saturation :math:`s^R`. """ return self._saturation @property def lightness(self): """ Predicted colorfulness :math:`L^R`. """ return self._lightness @property def a(self): """ Predicted red-green chromatic response :math:`a^R`. """ return self._a @property def b(self): """ Predicted yellow-blue chromatic response :math:`b^R`. """ return self._b R = numpy.array([[1.9569, -1.1882, 0.2313], [0.3612, 0.6388, 0], [0, 0, 1]]) def __init__(self, x, y, z, x_n, y_n, z_n, y_n_abs, sigma, d): """ :param x: X value of test sample :math:`X`. :param y: Y value of test sample :math:`Y`. :param z: Z value of test sample :math:`Z`. :param x_n: X value of reference white :math:`X_n`. :param y_n: Y value of reference white :math:`Y_n`. :param z_n: Z value of reference white :math:`Z_n`. :param y_n_abs: Absolute luminance :math:`Y_n` of a white object in cd/m^2. :param sigma: Relative luminance parameter :math:`\sigma`. For average surround set :math:`\sigma=1/2.3`, for dim surround :math:`\sigma=1/2.9` and for dark surround :math:`\sigma=1/3.5`. :param d: Degree of adaptation :math:`D`. """ xyz = numpy.array([x, y, z]) xyz_n = numpy.array([x_n, y_n, z_n]) lms = Hunt.xyz_to_rgb(xyz) lms_n = Hunt.xyz_to_rgb(xyz_n) logger.debug('LMS: {}'.format(lms)) logger.debug('LMS_n: {}'.format(lms_n)) lms_e = (3 * lms_n) / (lms_n[0] + lms_n[1] + lms_n[2]) lms_p = (1 + (y_n_abs ** (1 / 3)) + lms_e) / ( 1 + (y_n_abs ** (1 / 3)) + (1 / lms_e)) logger.debug('LMS_e: {}'.format(lms_e)) logger.debug('LMS_p: {}'.format(lms_p)) lms_a = (lms_p + d * (1 - lms_p)) / lms_n logger.debug('LMS_a: {}'.format(lms_a)) # If we want to allow arrays as input we need special handling here. if len(numpy.shape(x)) == 0: # Okay so just a number, we can do things by the book. a = numpy.diag(lms_a) logger.debug('A: {}'.format(a)) xyz_ref = self.R.dot(a).dot(Hunt.xyz_to_rgb_m).dot(xyz) else: # So we have an array. Since constructing huge multidimensional # arrays might not bee the best idea, we will handle each input # dimension separately. First figure out how many values we have to # deal with. input_dim = len(x) # No create the ouput array that we will fill layer by layer xyz_ref = numpy.zeros((3, input_dim)) for layer in range(input_dim): a = numpy.diag(lms_a[..., layer]) logger.debug('A layer {}: {}'.format(layer, a)) xyz_ref[..., layer] = self.R.dot(a).dot(Hunt.xyz_to_rgb_m).dot(xyz[..., layer]) logger.debug('XYZ_ref: {}'.format(xyz_ref)) x_ref, y_ref, z_ref = xyz_ref # Lightness self._lightness = 100 * (y_ref ** sigma) logger.debug('lightness: {}'.format(self.lightness)) # Opponent Color Dimensions self._a = 430 * ((x_ref ** sigma) - (y_ref ** sigma)) self._b = 170 * ((y_ref ** sigma) - (z_ref ** sigma)) logger.debug('a: {}'.format(self._a)) logger.debug('b: {}'.format(self._b)) # Hue self._hue_angle = (360 * numpy.arctan2(self._b, self._a) / (2 * numpy.pi) + 360) % 360 # Chroma self._chroma = numpy.sqrt((self._a ** 2) + (self._b ** 2)) # Saturation self._saturation = self.chroma / self.lightness class ATD95(object): """ **References** * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons. * Guth, S. L. (1995, April). Further applications of the ATD model for color vision. In *IS&T/SPIE's Symposium on Electronic Imaging: Science & Technology* (pp. 12-26). International Society for Optics and Photonics. """ @property def hue(self): """ Predicted hue :math:`H`. """ return self._hue @property def brightness(self): """ Predicted brightness :math:`Br`. """ return self._brightness @property def saturation(self): """ Predicted saturation :math:`C`. """ return self._saturation def __init__(self, x, y, z, x_0, y_0, z_0, y_0_abs, k_1, k_2, sigma=300): """ :param x: X value of test sample :math:`X`. :param y: Y value of test sample :math:`Y`. :param z: Z value of test sample :math:`Z`. :param x_0: X value of reference white :math:`X_0`. :param y_0: Y value of reference white :math:`Y_0`. :param z_0: Z value of reference white :math:`Z_0`. :param y_0_abs: Absolute adapting luminance :math:`Y_0` in cd/m^2. :param k_1: :math:`k_1` :param k_2: :math:`k_2` :param sigma: :math:`\sigma` """ xyz = self._scale_to_luminance(numpy.array([x, y, z]), y_0_abs) xyz_0 = self._scale_to_luminance(numpy.array([x_0, y_0, z_0]), y_0_abs) logger.debug('Scaled XYZ: {}'.format(xyz)) logger.debug('Scaled XYZ_0: {}'.format(xyz)) # Adaptation Model lms = self._xyz_to_lms(xyz) logger.debug('LMS: {}'.format(lms)) xyz_a = k_1 * xyz + k_2 * xyz_0 logger.debug('XYZ_a: {}'.format(xyz_a)) lms_a = self._xyz_to_lms(xyz_a) logger.debug('LMS_a: {}'.format(lms_a)) l_g, m_g, s_g = lms * (sigma / (sigma + lms_a)) # Opponent Color Dimensions a_1i = 3.57 * l_g + 2.64 * m_g t_1i = 7.18 * l_g - 6.21 * m_g d_1i = -0.7 * l_g + 0.085 * m_g + s_g a_2i = 0.09 * a_1i t_2i = 0.43 * t_1i + 0.76 * d_1i d_2i = d_1i self._a_1 = self._calculate_final_response(a_1i) self._t_1 = self._calculate_final_response(t_1i) self._d_1 = self._calculate_final_response(d_1i) self._a_2 = self._calculate_final_response(a_2i) self._t_2 = self._calculate_final_response(t_2i) self._d_2 = self._calculate_final_response(d_2i) # Perceptual Correlates self._brightness = (self._a_1 ** 2 + self._t_1 ** 2 + self._d_1 ** 2) ** 0.5 self._saturation = (self._t_2 ** 2 + self._d_2 ** 2) ** 0.5 / self._a_2 self._hue = self._t_2 / self._d_2 @staticmethod def _calculate_final_response(value): return value / (200 + abs(value)) @staticmethod def _scale_to_luminance(xyz, absolute_adapting_luminance): return 18 * (absolute_adapting_luminance * xyz / 100) ** 0.8 @staticmethod def _xyz_to_lms(xyz): x, y, z = xyz l = ((0.66 * (0.2435 * x + 0.8524 * y - 0.0516 * z)) ** 0.7) + 0.024 m = ((-0.3954 * x + 1.1642 * y + 0.0837 * z) ** 0.7) + 0.036 s = ((0.43 * (0.04 * y + 0.6225 * z)) ** 0.7) + 0.31 return numpy.array([l, m, s]) class LLAB(object): """ **References** * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons. * Luo, M. R., & Morovic, J. (1996, September). Two unsolved issues in colour management-colour appearance and gamut mapping. In *5th International Conference on High Technology* (pp. 136-147). * Luo, M. R., Lo, M. C., & Kuo, W. G. (1996). The LLAB (l: c) colour model. *Color Research & Application*, 21(6), 412-429. """ @property def hue_angle(self): """ Predicted hue angle :math:`h_L`. """ return self._hue_angle @property def chroma(self): """ Predicted chroma :math:`Ch_L`. """ return self._chroma @property def saturation(self): """ Predicted saturation :math:`s_L`. """ return self._saturation @property def lightness(self): """ Predicted colorfulness :math:`L_L`. """ return self._lightness @property def a_l(self): """ Predicted red-green chromatic response :math:`A_L`. """ return self._a_l @property def b_l(self): """ Predicted yellow-blue chromatic response :math:`B_L`. """ return self._b_l def __init__(self, x, y, z, x_0, y_0, z_0, y_b, f_s, f_l, f_c, l, d=1): """ :param x: X value of test sample :math:`X`. :param y: Y value of test sample :math:`Y`. :param z: Z value of test sample :math:`Z`. :param x_0: X value of reference white :math:`X_0`. :param y_0: Y value of reference white :math:`Y_0`. :param z_0: Z value of reference white :math:`Z_0`. :param y_b: Luminance factor of the background :math:`Y_b` in cd/m^2. :param f_s: Surround induction factor :math:`F_S`. :param f_l: Lightness induction factor :math:`F_L`. :param f_c: Chroma induction factor :math:`F_C`. :param l: Absolute luminance of reference white :math:`L` in cd/m^2. :param d: Discounting-the-Illuminant factor :math:`D`. """ xyz = numpy.array([x, y, z]) logger.debug('XYZ: {}'.format([x, y, z])) xyz_0 = numpy.array([x_0, y_0, z_0]) r, g, b = self.xyz_to_rgb(xyz) logger.debug('RGB: {}'.format([r, g, b])) r_0, g_0, b_0 = self.xyz_to_rgb(xyz_0) logger.debug('RGB_0: {}'.format([r_0, g_0, b_0])) xyz_0r = numpy.array([95.05, 100, 108.88]) r_0r, g_0r, b_0r = self.xyz_to_rgb(xyz_0r) logger.debug('RGB_0r: {}'.format([r_0r, g_0r, b_0r])) beta = (b_0 / b_0r) ** 0.0834 logger.debug('beta: {}'.format(beta)) r_r = (d * (r_0r / r_0) + 1 - d) * r g_r = (d * (g_0r / g_0) + 1 - d) * g b_r = (d * (b_0r / (b_0 ** beta)) + 1 - d) * (abs(b) ** beta) logger.debug('RGB_r: {}'.format([r_r, g_r, b_r])) rgb_r = numpy.array([r_r, g_r, b_r]) # m_inv = numpy.linalg.inv(self.xyz_to_rgb_m) m_inv = numpy.array([[0.987, -0.1471, 0.16], [0.4323, 0.5184, 0.0493], [-0.0085, 0.04, 0.9685]]) x_r, y_r, z_r = m_inv.dot(rgb_r * y) logger.debug('XYZ_r: {}'.format([x_r, y_r, z_r])) # Opponent Color Dimension def f(w): return numpy.where(w > 0.008856, w ** (1 / f_s), (((0.008856 ** (1 / f_s)) - (16 / 116)) / 0.008856) * w + (16 / 116)) # lightness_contrast_exponent z = 1 + f_l * ((y_b / 100) ** 0.5) logger.debug('z: {}'.format(z)) self._lightness = 116 * (f(y_r / 100) ** z) - 16 a = 500 * (f(x_r / 95.05) - f(y_r / 100)) b = 200 * (f(y_r / 100) - f(z_r / 108.88)) logger.debug('A: {}'.format(a)) logger.debug('B: {}'.format(b)) logger.debug('f(Xr): {}'.format(f(x_r / 95.05))) logger.debug('f(Yr): {}'.format(f(y_r / 100))) logger.debug('f(Zr): {}'.format(f(z_r / 108.88))) # Perceptual Correlates c = (a ** 2 + b ** 2) ** 0.5 self._chroma = 25 * numpy.log(1 + 0.05 * c) s_c = 1 + 0.47 * numpy.log10(l) - 0.057 * numpy.log10(l) ** 2 s_m = 0.7 + 0.02 * self._lightness - 0.0002 * self._lightness ** 2 c_l = self._chroma * s_m * s_c * f_c self._saturation = self._chroma / self._lightness hue_angle_rad = numpy.arctan2(b, a) self._hue_angle = hue_angle_rad * 360 / (2 * numpy.pi) % 360 self._a_l = c_l * numpy.cos(hue_angle_rad) self._b_l = c_l * numpy.sin(hue_angle_rad) xyz_to_rgb_m = numpy.array([[0.8951, 0.2664, -0.1614], [-0.7502, 1.7135, 0.0367], [0.0389, -0.0685, 1.0296]]) @classmethod def xyz_to_rgb(cls, xyz): return cls.xyz_to_rgb_m.dot(xyz / xyz[1]) class CIECAM02(object): """ **References** * CIE TC 8-01 (2004). A Color appearance model for color management systems. Publication 159. Vienna: CIE Central Bureau. ISBN 3-901906-29-0. * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons. """ @property def hue_angle(self): """ Predicted hue angle :math:`h`. """ return self._h @property def chroma(self): """ Predicted chroma :math:`C`. """ return self._chroma @property def saturation(self): """ Predicted saturation :math:`s_L`. """ return self._saturation @property def lightness(self): """ Predicted colorfulness :math:`J`. """ return self._lightness @property def brightness(self): """ Predicted colorfulness :math:`Q`. """ return self._brightness @property def colorfulness(self): """ Predicted colorfulness :math:`M`. """ return self._colorfulness @property def a(self): """ Predicted red-green chromatic response :math:`a`. """ return self._a @property def b(self): """ Predicted yellow-blue chromatic response :math:`b`. """ return self._b M_CAT02 = numpy.array([[0.7328, 0.4296, -0.1624], [-0.7036, 1.6975, 0.0061], [0.0030, 0.0136, 0.9834]]) M_CAT02_inv = numpy.linalg.inv(M_CAT02) M_HPE = numpy.array([[0.38971, 0.68898, -0.07868], [-0.22981, 1.18340, 0.04641], [0, 0, 1]]) def __init__(self, x, y, z, x_w, y_w, z_w, y_b, l_a, c, n_c, f, d=False): """ :param x: X value of test sample :math:`X`. :param y: Y value of test sample :math:`Y`. :param z: Z value of test sample :math:`Z`. :param x_w: X value of reference white :math:`X_W`. :param y_w: Y value of reference white :math:`Y_W`. :param z_w: Z value of reference white :math:`Z_W`. :param y_b: Background relative luminance :math:`Y_b`. :param l_a: Adapting luminance :math:`L_A` in cd/m^2. :param c: Exponential nonlinearity :math:`c`. (Average/Dim/Dark) (0.69/0.59/0.525). :param n_c: Chromatic induction factor :math:`N_c`. (Average/Dim/Dark) (1.0,0.9,0.8). :param f: Maximum degree of adaptation :math:`F`. (Average/Dim/Dark) (1.0/0.9/0.8). :param d: Discount-the-Illuminant factor :math:`D`. """ xyz = numpy.array([x, y, z]) xyz_w = numpy.array([x_w, y_w, z_w]) # Determine the degree of adaptation if not d: d = self._compute_degree_of_adaptation(f, l_a) else: d = 1 logger.debug("D: {}".format(d)) # Compute viewing condition dependant components k = 1 / (5 * l_a + 1) logger.debug("k: {}".format(k)) f_l = 0.2 * (k ** 4) * 5 * l_a + 0.1 * (1 - k ** 4) ** 2 * (5 * l_a) ** (1 / 3) logger.debug("F_L: {}".format(f_l)) n = y_b / y_w logger.debug("n: {}".format(n)) self.n_bb = self.n_cb = 0.725 * n ** -0.2 z = 1.48 + numpy.sqrt(n) logger.debug("z".format(z)) rgb_a, rgb_aw = self._compute_adaptation(xyz, xyz_w, f_l, d) logger.debug("RGB'a: {}".format(rgb_a)) logger.debug("RGB'aw: {}".format(rgb_aw)) r_a, g_a, b_a = rgb_a r_aw, g_aw, b_aw = rgb_aw # Opponent Color Dimensions self._a = r_a - 12 * g_a / 11 + b_a / 11 self._b = (1 / 9) * (r_a + g_a - 2 * b_a) # Hue self._h = 360 * numpy.arctan2(self._b, self._a) / (2 * numpy.pi) e_t = (1 / 4) * (numpy.cos(2 + self._h * numpy.pi / 180) + 3.8) # Lightness a = self._compute_achromatic_response(r_a, g_a, b_a, self.n_bb) logger.debug('A: {}'.format(a)) a_w = self._compute_achromatic_response(r_aw, g_aw, b_aw, self.n_bb) logger.debug('A_W: {}'.format(a_w)) self._lightness = 100 * (a / a_w) ** (c * z) # 16.24 # Brightness # self._brightness = self.compute_brightness(self.lightness, surround, a_w, f_l) self._brightness = (4 / c) * numpy.sqrt(self._lightness / 100) * (a_w + 4) * f_l ** 0.25 # Chroma # self.chroma = self.compute_chroma(rgb_a, self.lightness, surround, self.N_cb, e_t, self.a, self.b, n) t = ((50000 / 13) * n_c * self.n_cb * e_t * numpy.sqrt((self._a ** 2) + (self._b ** 2))) / ( rgb_a[0] + rgb_a[1] + (21 / 20) * rgb_a[2]) self._chroma = (t ** 0.9) * numpy.sqrt(self._lightness / 100) * ((1.64 - 0.29 ** n) ** 0.73) # Colorfulness self._colorfulness = self.chroma * f_l ** 0.25 # Saturation self._saturation = 100 * numpy.sqrt(self._colorfulness / self._brightness) # Cartesian coordinates self.a_c, self.b_c = self._compute_cartesian_coordinates(self.chroma, self._h) self.a_m, self.b_m = self._compute_cartesian_coordinates(self._colorfulness, self._h) self.a_s, self.b_s = self._compute_cartesian_coordinates(self.saturation, self._h) @classmethod def _compute_adaptation(cls, xyz, xyz_w, f_l, d): # Transform input colors to cone responses rgb = cls._xyz_to_rgb(xyz) logger.debug("RGB: {}".format(rgb)) rgb_w = cls._xyz_to_rgb(xyz_w) logger.debug("RGB_W: {}".format(rgb_w)) # Compute adapted tristimulus-responses rgb_c = cls._white_adaption(rgb, rgb_w, d) logger.debug("RGB_C: {}".format(rgb_c)) rgb_cw = cls._white_adaption(rgb_w, rgb_w, d) logger.debug("RGB_CW: {}".format(rgb_cw)) # Convert adapted tristimulus-responses to Hunt-Pointer-Estevez fundamentals rgb_p = cls._compute_hunt_pointer_estevez_fundamentals(rgb_c) logger.debug("RGB': {}".format(rgb_p)) rgb_wp = cls._compute_hunt_pointer_estevez_fundamentals(rgb_cw) logger.debug("RGB'_W: {}".format(rgb_wp)) # Compute post-adaptation non-linearities rgb_ap = cls._compute_nonlinearities(f_l, rgb_p) rgb_awp = cls._compute_nonlinearities(f_l, rgb_wp) return rgb_ap, rgb_awp @staticmethod def _xyz_to_rgb(xyz): return numpy.dot(CIECAM02.M_CAT02, xyz) @staticmethod def _rgb_to_xyz(rgb): return numpy.dot(CIECAM02.M_CAT02_inv, rgb) @staticmethod def _white_adaption(rgb, rgb_w, d=1): return ((100 * d / rgb_w) + (1 - d)) * rgb @staticmethod def _compute_degree_of_adaptation(surround_conditions, adapting_luminance): return surround_conditions * (1 - (1 / 3.6) * numpy.exp((-adapting_luminance - 42) / 92)) @staticmethod def _compute_hunt_pointer_estevez_fundamentals(rgb): return numpy.dot(numpy.dot(CIECAM02.M_HPE, CIECAM02.M_CAT02_inv), rgb) @staticmethod def _compute_nonlinearities(f_l, rgb): return 0.1 + (400 * (f_l * rgb / 100) ** 0.42) / (27.13 + (f_l * rgb / 100) ** 0.42) @staticmethod def _compute_achromatic_response(r, g, b, n_bb): return (2 * r + g + (1 / 20) * b - 0.305) * n_bb @staticmethod def _compute_cartesian_coordinates(value, hue): a = value * numpy.cos(hue * numpy.pi / 180) # 16.30 b = value * numpy.sin(hue * numpy.pi / 180) # 16.31 return a, b class CIECAM02m1(CIECAM02): """ **References** * Wu, R. C., & Wardman, R. H. (2007). Proposed modification to the CIECAM02 colour appearance model to include the simultaneous contrast effects. *Color Research & Application*, 32(2), 121-129. """ def __init__(self, x, y, z, x_w, y_w, z_w, x_b, y_b, z_b, l_a, c, n_c, f, p, d=False): """ :param x: X value of test sample :math:`X`. :param y: Y value of test sample :math:`Y`. :param z: Z value of test sample :math:`Z`. :param x_w: X value of reference white :math:`X_W`. :param y_w: Y value of reference white :math:`Y_W`. :param z_w: Z value of reference white :math:`Z_W`. :param x_b: X value of background :math:`X_b`. :param y_b: Y value of background :math:`Y_b`. :param z_b: Z value of background :math:`Z_b`. :param l_a: Adapting luminance :math:`L_A` in cd/m^2. :param c: Exponential nonlinearity :math:`c`. (Average/Dim/Dark) (0.69/0.59/5.25). :param n_c: Chromatic induction factor :math:`N_c`. (Average/Dim/Dark) (1.0,0.9,0.8). :param f: Maximum degree of adaptation :math:`F`. (Average/Dim/Dark) (1.0/0.9/0.8). :param p: Simultaneous contrast/assimilation parameter. :param d: Discount-the-Illuminant factor :math:`D`. """ self._p = p self._xyz_b = numpy.array([x_b, y_b, z_b]) super(CIECAM02m1, self).__init__(x, y, z, x_w, y_w, z_w, y_b, l_a, c, n_c, f, d) def _compute_adaptation(self, xyz, xyz_w, f_l, d): """ Modified adaptation procedure incorporating simultaneous chromatic contrast from Hunt model. :param xyz: Stimulus XYZ. :param xyz_w: Reference white XYZ. :param f_l: Luminance adaptation factor :param d: Degree of adaptation. :return: Tuple of adapted rgb and rgb_w arrays. """ # Transform input colors to cone responses rgb = self._xyz_to_rgb(xyz) logger.debug("RGB: {}".format(rgb)) rgb_b = self._xyz_to_rgb(self._xyz_b) rgb_w = self._xyz_to_rgb(xyz_w) rgb_w = Hunt.adjust_white_for_scc(rgb, rgb_b, rgb_w, self._p) logger.debug("RGB_W: {}".format(rgb_w)) # Compute adapted tristimulus-responses rgb_c = self._white_adaption(rgb, rgb_w, d) logger.debug("RGB_C: {}".format(rgb_c)) rgb_cw = self._white_adaption(rgb_w, rgb_w, d) logger.debug("RGB_CW: {}".format(rgb_cw)) # Convert adapted tristimulus-responses to Hunt-Pointer-Estevez fundamentals rgb_p = self._compute_hunt_pointer_estevez_fundamentals(rgb_c) logger.debug("RGB': {}".format(rgb_p)) rgb_wp = self._compute_hunt_pointer_estevez_fundamentals(rgb_cw) logger.debug("RGB'_W: {}".format(rgb_wp)) # Compute post-adaptation non-linearities rgb_ap = self._compute_nonlinearities(f_l, rgb_p) rgb_awp = self._compute_nonlinearities(f_l, rgb_wp) return rgb_ap, rgb_awp colormath-3.0.0/colormath/color_objects.py0000644000175000017500000006746713220777307023443 0ustar greg.taylorgreg.taylor00000000000000""" This module contains classes to represent various color spaces. """ import logging import math import numpy from colormath import color_constants from colormath import density from colormath.chromatic_adaptation import apply_chromatic_adaptation_on_color from colormath.color_exceptions import InvalidObserverError, InvalidIlluminantError logger = logging.getLogger(__name__) class ColorBase(object): """ A base class holding some common methods and values. """ # Attribute names containing color data on the sub-class. For example, # sRGBColor would be ['rgb_r', 'rgb_g', 'rgb_b'] VALUES = [] # If this object as converted such that its values passed through an # RGB colorspace, this is set to the class for said RGB color space. # Allows reversing conversions automatically and accurately. _through_rgb_type = None def get_value_tuple(self): """ Returns a tuple of the color's values (in order). For example, an LabColor object will return (lab_l, lab_a, lab_b), where each member of the tuple is the float value for said variable. """ retval = tuple() for val in self.VALUES: retval += (getattr(self, val),) return retval def __str__(self): """ String representation of the color. """ retval = self.__class__.__name__ + ' (' for val in self.VALUES: value = getattr(self, val, None) if value is not None: retval += '%s:%.4f ' % (val, getattr(self, val)) return retval.strip() + ')' def __repr__(self): """ String representation of the object. """ retval = self.__class__.__name__ + '(' attributes = [(attr, getattr(self, attr)) for attr in self.VALUES] values = [x + "=" + repr(y) for x, y in attributes] retval += ','.join(values) return retval + ')' class IlluminantMixin(object): """ Color spaces that have a notion of an illuminant should inherit this. """ # noinspection PyAttributeOutsideInit def set_observer(self, observer): """ Validates and sets the color's observer angle. .. note:: This only changes the observer angle value. It does no conversion of the color's coordinates. :param str observer: One of '2' or '10'. """ observer = str(observer) if observer not in color_constants.OBSERVERS: raise InvalidObserverError(self) self.observer = observer # noinspection PyAttributeOutsideInit def set_illuminant(self, illuminant): """ Validates and sets the color's illuminant. .. note:: This only changes the illuminant. It does no conversion of the color's coordinates. For this, you'll want to refer to :py:meth:`XYZColor.apply_adaptation `. .. tip:: Call this after setting your observer. :param str illuminant: One of the various illuminants. """ illuminant = illuminant.lower() if illuminant not in color_constants.ILLUMINANTS[self.observer]: raise InvalidIlluminantError(illuminant) self.illuminant = illuminant def get_illuminant_xyz(self, observer=None, illuminant=None): """ :param str observer: Get the XYZ values for another observer angle. Must be either '2' or '10'. :param str illuminant: Get the XYZ values for another illuminant. :returns: the color's illuminant's XYZ values. """ try: if observer is None: observer = self.observer illums_observer = color_constants.ILLUMINANTS[observer] except KeyError: raise InvalidObserverError(self) try: if illuminant is None: illuminant = self.illuminant illum_xyz = illums_observer[illuminant] except (KeyError, AttributeError): raise InvalidIlluminantError(illuminant) return {'X': illum_xyz[0], 'Y': illum_xyz[1], 'Z': illum_xyz[2]} class SpectralColor(IlluminantMixin, ColorBase): """ A SpectralColor represents a spectral power distribution, as read by a spectrophotometer. Our current implementation has wavelength intervals of 10nm, starting at 340nm and ending at 830nm. Spectral colors are the lowest level, most "raw" measurement of color. You may convert spectral colors to any other color space, but you can't convert any other color space back to spectral. See `Spectral power distribution `_ on Wikipedia for some higher level details on how these work. """ VALUES = [ 'spec_340nm', 'spec_350nm', 'spec_360nm', 'spec_370nm', 'spec_380nm', 'spec_390nm', 'spec_400nm', 'spec_410nm', 'spec_420nm', 'spec_430nm', 'spec_440nm', 'spec_450nm', 'spec_460nm', 'spec_470nm', 'spec_480nm', 'spec_490nm', 'spec_500nm', 'spec_510nm', 'spec_520nm', 'spec_530nm', 'spec_540nm', 'spec_550nm', 'spec_560nm', 'spec_570nm', 'spec_580nm', 'spec_590nm', 'spec_600nm', 'spec_610nm', 'spec_620nm', 'spec_630nm', 'spec_640nm', 'spec_650nm', 'spec_660nm', 'spec_670nm', 'spec_680nm', 'spec_690nm', 'spec_700nm', 'spec_710nm', 'spec_720nm', 'spec_730nm', 'spec_740nm', 'spec_750nm', 'spec_760nm', 'spec_770nm', 'spec_780nm', 'spec_790nm', 'spec_800nm', 'spec_810nm', 'spec_820nm', 'spec_830nm' ] def __init__(self, spec_340nm=0.0, spec_350nm=0.0, spec_360nm=0.0, spec_370nm=0.0, spec_380nm=0.0, spec_390nm=0.0, spec_400nm=0.0, spec_410nm=0.0, spec_420nm=0.0, spec_430nm=0.0, spec_440nm=0.0, spec_450nm=0.0, spec_460nm=0.0, spec_470nm=0.0, spec_480nm=0.0, spec_490nm=0.0, spec_500nm=0.0, spec_510nm=0.0, spec_520nm=0.0, spec_530nm=0.0, spec_540nm=0.0, spec_550nm=0.0, spec_560nm=0.0, spec_570nm=0.0, spec_580nm=0.0, spec_590nm=0.0, spec_600nm=0.0, spec_610nm=0.0, spec_620nm=0.0, spec_630nm=0.0, spec_640nm=0.0, spec_650nm=0.0, spec_660nm=0.0, spec_670nm=0.0, spec_680nm=0.0, spec_690nm=0.0, spec_700nm=0.0, spec_710nm=0.0, spec_720nm=0.0, spec_730nm=0.0, spec_740nm=0.0, spec_750nm=0.0, spec_760nm=0.0, spec_770nm=0.0, spec_780nm=0.0, spec_790nm=0.0, spec_800nm=0.0, spec_810nm=0.0, spec_820nm=0.0, spec_830nm=0.0, observer='2', illuminant='d50'): """ :keyword str observer: Observer angle. Either ``'2'`` or ``'10'`` degrees. :keyword str illuminant: See :doc:`illuminants` for valid values. """ super(SpectralColor, self).__init__() # Spectral fields self.spec_340nm = float(spec_340nm) self.spec_350nm = float(spec_350nm) self.spec_360nm = float(spec_360nm) self.spec_370nm = float(spec_370nm) # begin Blue wavelengths self.spec_380nm = float(spec_380nm) self.spec_390nm = float(spec_390nm) self.spec_400nm = float(spec_400nm) self.spec_410nm = float(spec_410nm) self.spec_420nm = float(spec_420nm) self.spec_430nm = float(spec_430nm) self.spec_440nm = float(spec_440nm) self.spec_450nm = float(spec_450nm) self.spec_460nm = float(spec_460nm) self.spec_470nm = float(spec_470nm) self.spec_480nm = float(spec_480nm) self.spec_490nm = float(spec_490nm) # end Blue wavelengths # start Green wavelengths self.spec_500nm = float(spec_500nm) self.spec_510nm = float(spec_510nm) self.spec_520nm = float(spec_520nm) self.spec_530nm = float(spec_530nm) self.spec_540nm = float(spec_540nm) self.spec_550nm = float(spec_550nm) self.spec_560nm = float(spec_560nm) self.spec_570nm = float(spec_570nm) self.spec_580nm = float(spec_580nm) self.spec_590nm = float(spec_590nm) self.spec_600nm = float(spec_600nm) self.spec_610nm = float(spec_610nm) # end Green wavelengths # start Red wavelengths self.spec_620nm = float(spec_620nm) self.spec_630nm = float(spec_630nm) self.spec_640nm = float(spec_640nm) self.spec_650nm = float(spec_650nm) self.spec_660nm = float(spec_660nm) self.spec_670nm = float(spec_670nm) self.spec_680nm = float(spec_680nm) self.spec_690nm = float(spec_690nm) self.spec_700nm = float(spec_700nm) self.spec_710nm = float(spec_710nm) self.spec_720nm = float(spec_720nm) # end Red wavelengths self.spec_730nm = float(spec_730nm) self.spec_740nm = float(spec_740nm) self.spec_750nm = float(spec_750nm) self.spec_760nm = float(spec_760nm) self.spec_770nm = float(spec_770nm) self.spec_780nm = float(spec_780nm) self.spec_790nm = float(spec_790nm) self.spec_800nm = float(spec_800nm) self.spec_810nm = float(spec_810nm) self.spec_820nm = float(spec_820nm) self.spec_830nm = float(spec_830nm) #: The color's observer angle. Set with :py:meth:`set_observer`. self.observer = None #: The color's illuminant. Set with :py:meth:`set_illuminant`. self.illuminant = None self.set_observer(observer) self.set_illuminant(illuminant) def get_numpy_array(self): """ Dump this color into NumPy array. """ # This holds the obect's spectral data, and will be passed to # numpy.array() to create a numpy array (matrix) for the matrix math # that will be done during the conversion to XYZ. values = [] # Use the required value list to build this dynamically. Default to # 0.0, since that ultimately won't affect the outcome due to the math # involved. for val in self.VALUES: values.append(getattr(self, val, 0.0)) # Create and the actual numpy array/matrix from the spectral list. color_array = numpy.array([values]) return color_array def calc_density(self, density_standard=None): """ Calculates the density of the SpectralColor. By default, Status T density is used, and the correct density distribution (Red, Green, or Blue) is chosen by comparing the Red, Green, and Blue components of the spectral sample (the values being red in via "filters"). """ if density_standard is not None: return density.ansi_density(self, density_standard) else: return density.auto_density(self) class LabColor(IlluminantMixin, ColorBase): """ Represents a CIE Lab color. For more information on CIE Lab, see `Lab color space `_ on Wikipedia. """ VALUES = ['lab_l', 'lab_a', 'lab_b'] def __init__(self, lab_l, lab_a, lab_b, observer='2', illuminant='d50'): """ :param float lab_l: L coordinate. :param float lab_a: a coordinate. :param float lab_b: b coordinate. :keyword str observer: Observer angle. Either ``'2'`` or ``'10'`` degrees. :keyword str illuminant: See :doc:`illuminants` for valid values. """ super(LabColor, self).__init__() #: L coordinate self.lab_l = float(lab_l) #: a coordinate self.lab_a = float(lab_a) #: b coordinate self.lab_b = float(lab_b) #: The color's observer angle. Set with :py:meth:`set_observer`. self.observer = None #: The color's illuminant. Set with :py:meth:`set_illuminant`. self.illuminant = None self.set_observer(observer) self.set_illuminant(illuminant) class LCHabColor(IlluminantMixin, ColorBase): """ Represents an CIE LCH color that was converted to LCH by passing through CIE Lab. This differs from :py:class:`LCHuvColor`, which was converted to LCH through CIE Luv. See `Introduction to Colour Spaces `_ by Phil Cruse for an illustration of how CIE LCH differs from CIE Lab. """ VALUES = ['lch_l', 'lch_c', 'lch_h'] def __init__(self, lch_l, lch_c, lch_h, observer='2', illuminant='d50'): """ :param float lch_l: L coordinate. :param float lch_c: C coordinate. :param float lch_h: H coordinate. :keyword str observer: Observer angle. Either ``'2'`` or ``'10'`` degrees. :keyword str illuminant: See :doc:`illuminants` for valid values. """ super(LCHabColor, self).__init__() #: L coordinate self.lch_l = float(lch_l) #: C coordinate self.lch_c = float(lch_c) #: H coordinate self.lch_h = float(lch_h) #: The color's observer angle. Set with :py:meth:`set_observer`. self.observer = None #: The color's illuminant. Set with :py:meth:`set_illuminant`. self.illuminant = None self.set_observer(observer) self.set_illuminant(illuminant) class LCHuvColor(IlluminantMixin, ColorBase): """ Represents an CIE LCH color that was converted to LCH by passing through CIE Luv. This differs from :py:class:`LCHabColor`, which was converted to LCH through CIE Lab. See `Introduction to Colour Spaces `_ by Phil Cruse for an illustration of how CIE LCH differs from CIE Lab. """ VALUES = ['lch_l', 'lch_c', 'lch_h'] def __init__(self, lch_l, lch_c, lch_h, observer='2', illuminant='d50'): """ :param float lch_l: L coordinate. :param float lch_c: C coordinate. :param float lch_h: H coordinate. :keyword str observer: Observer angle. Either ``'2'`` or ``'10'`` degrees. :keyword str illuminant: See :doc:`illuminants` for valid values. """ super(LCHuvColor, self).__init__() #: L coordinate self.lch_l = float(lch_l) #: C coordinate self.lch_c = float(lch_c) #: H coordinate self.lch_h = float(lch_h) #: The color's observer angle. Set with :py:meth:`set_observer`. self.observer = None #: The color's illuminant. Set with :py:meth:`set_illuminant`. self.illuminant = None self.set_observer(observer) self.set_illuminant(illuminant) class LuvColor(IlluminantMixin, ColorBase): """ Represents an Luv color. """ VALUES = ['luv_l', 'luv_u', 'luv_v'] def __init__(self, luv_l, luv_u, luv_v, observer='2', illuminant='d50'): """ :param float luv_l: L coordinate. :param float luv_u: u coordinate. :param float luv_v: v coordinate. :keyword str observer: Observer angle. Either ``'2'`` or ``'10'`` degrees. :keyword str illuminant: See :doc:`illuminants` for valid values. """ super(LuvColor, self).__init__() #: L coordinate self.luv_l = float(luv_l) #: u coordinate self.luv_u = float(luv_u) #: v coordinate self.luv_v = float(luv_v) #: The color's observer angle. Set with :py:meth:`set_observer`. self.observer = None #: The color's illuminant. Set with :py:meth:`set_illuminant`. self.illuminant = None self.set_observer(observer) self.set_illuminant(illuminant) class XYZColor(IlluminantMixin, ColorBase): """ Represents an XYZ color. """ VALUES = ['xyz_x', 'xyz_y', 'xyz_z'] def __init__(self, xyz_x, xyz_y, xyz_z, observer='2', illuminant='d50'): """ :param float xyz_x: X coordinate. :param float xyz_y: Y coordinate. :param float xyz_z: Z coordinate. :keyword str observer: Observer angle. Either ``'2'`` or ``'10'`` degrees. :keyword str illuminant: See :doc:`illuminants` for valid values. """ super(XYZColor, self).__init__() #: X coordinate self.xyz_x = float(xyz_x) #: Y coordinate self.xyz_y = float(xyz_y) #: Z coordinate self.xyz_z = float(xyz_z) #: The color's observer angle. Set with :py:meth:`set_observer`. self.observer = None #: The color's illuminant. Set with :py:meth:`set_illuminant`. self.illuminant = None self.set_observer(observer) self.set_illuminant(illuminant) def apply_adaptation(self, target_illuminant, adaptation='bradford'): """ This applies an adaptation matrix to change the XYZ color's illuminant. You'll most likely only need this during RGB conversions. """ logger.debug(" \- Original illuminant: %s", self.illuminant) logger.debug(" \- Target illuminant: %s", target_illuminant) # If the XYZ values were taken with a different reference white than the # native reference white of the target RGB space, a transformation matrix # must be applied. if self.illuminant != target_illuminant: logger.debug(" \* Applying transformation from %s to %s ", self.illuminant, target_illuminant) # Sets the adjusted XYZ values, and the new illuminant. apply_chromatic_adaptation_on_color( color=self, targ_illum=target_illuminant, adaptation=adaptation) # noinspection PyPep8Naming class xyYColor(IlluminantMixin, ColorBase): """ Represents an xYy color. """ VALUES = ['xyy_x', 'xyy_y', 'xyy_Y'] def __init__(self, xyy_x, xyy_y, xyy_Y, observer='2', illuminant='d50'): """ :param float xyy_x: x coordinate. :param float xyy_y: y coordinate. :param float xyy_Y: Y coordinate. :keyword str observer: Observer angle. Either ``'2'`` or ``'10'`` degrees. :keyword str illuminant: See :doc:`illuminants` for valid values. """ super(xyYColor, self).__init__() #: x coordinate self.xyy_x = float(xyy_x) #: y coordinate self.xyy_y = float(xyy_y) #: Y coordinate self.xyy_Y = float(xyy_Y) #: The color's observer angle. Set with :py:meth:`set_observer`. self.observer = None #: The color's illuminant. Set with :py:meth:`set_illuminant`. self.illuminant = None self.set_observer(observer) self.set_illuminant(illuminant) class BaseRGBColor(ColorBase): """ Base class for all RGB color spaces. .. warning:: Do not use this class directly! """ VALUES = ['rgb_r', 'rgb_g', 'rgb_b'] def __init__(self, rgb_r, rgb_g, rgb_b, is_upscaled=False): """ :param float rgb_r: R coordinate. 0...1. 1-255 if is_upscaled=True. :param float rgb_g: G coordinate. 0...1. 1-255 if is_upscaled=True. :param float rgb_b: B coordinate. 0...1. 1-255 if is_upscaled=True. :keyword bool is_upscaled: If False, RGB coordinate values are beteween 0.0 and 1.0. If True, RGB values are between 1 and 255. """ super(BaseRGBColor, self).__init__() if is_upscaled: self.rgb_r = rgb_r / 255.0 self.rgb_g = rgb_g / 255.0 self.rgb_b = rgb_b / 255.0 else: self.rgb_r = float(rgb_r) self.rgb_g = float(rgb_g) self.rgb_b = float(rgb_b) self.is_upscaled = is_upscaled def _clamp_rgb_coordinate(self, coord): """ Clamps an RGB coordinate, taking into account whether or not the color is upscaled or not. :param float coord: The coordinate value. :rtype: float :returns: The clamped value. """ if not self.is_upscaled: return min(max(coord, 0.0), 1.0) else: return min(max(coord, 1), 255) @property def clamped_rgb_r(self): """ The clamped (0.0-1.0) R value. """ return self._clamp_rgb_coordinate(self.rgb_r) @property def clamped_rgb_g(self): """ The clamped (0.0-1.0) G value. """ return self._clamp_rgb_coordinate(self.rgb_g) @property def clamped_rgb_b(self): """ The clamped (0.0-1.0) B value. """ return self._clamp_rgb_coordinate(self.rgb_b) def get_upscaled_value_tuple(self): """ Scales an RGB color object from decimal 0.0-1.0 to int 0-255. """ # Scale up to 0-255 values. rgb_r = int(math.floor(0.5 + self.rgb_r * 255)) rgb_g = int(math.floor(0.5 + self.rgb_g * 255)) rgb_b = int(math.floor(0.5 + self.rgb_b * 255)) return rgb_r, rgb_g, rgb_b def get_rgb_hex(self): """ Converts the RGB value to a hex value in the form of: #RRGGBB :rtype: str """ rgb_r, rgb_g, rgb_b = self.get_upscaled_value_tuple() return '#%02x%02x%02x' % (rgb_r, rgb_g, rgb_b) @classmethod def new_from_rgb_hex(cls, hex_str): """ Converts an RGB hex string like #RRGGBB and assigns the values to this sRGBColor object. :rtype: sRGBColor """ colorstring = hex_str.strip() if colorstring[0] == '#': colorstring = colorstring[1:] if len(colorstring) != 6: raise ValueError("input #%s is not in #RRGGBB format" % colorstring) r, g, b = colorstring[:2], colorstring[2:4], colorstring[4:] r, g, b = [int(n, 16) / 255.0 for n in (r, g, b)] return cls(r, g, b) # noinspection PyPep8Naming class sRGBColor(BaseRGBColor): """ Represents an sRGB color. .. note:: If you pass in upscaled values, we automatically scale them down to 0.0-1.0. If you need the old upscaled values, you can retrieve them with :py:meth:`get_upscaled_value_tuple`. :ivar float rgb_r: R coordinate :ivar float rgb_g: G coordinate :ivar float rgb_b: B coordinate :ivar bool is_upscaled: If True, RGB values are between 1-255. If False, 0.0-1.0. """ #: RGB space's gamma constant. rgb_gamma = 2.2 #: The RGB space's native illuminant. Important when converting to XYZ. native_illuminant = "d65" conversion_matrices = { "xyz_to_rgb": numpy.array(( (3.24071, -1.53726, -0.498571), (-0.969258, 1.87599, 0.0415557), (0.0556352, -0.203996, 1.05707))), "rgb_to_xyz": numpy.array(( (0.412424, 0.357579, 0.180464), (0.212656, 0.715158, 0.0721856), (0.0193324, 0.119193, 0.950444))), } class AdobeRGBColor(BaseRGBColor): """ Represents an Adobe RGB color. .. note:: If you pass in upscaled values, we automatically scale them down to 0.0-1.0. If you need the old upscaled values, you can retrieve them with :py:meth:`get_upscaled_value_tuple`. :ivar float rgb_r: R coordinate :ivar float rgb_g: G coordinate :ivar float rgb_b: B coordinate :ivar bool is_upscaled: If True, RGB values are between 1-255. If False, 0.0-1.0. """ #: RGB space's gamma constant. rgb_gamma = 2.2 #: The RGB space's native illuminant. Important when converting to XYZ. native_illuminant = "d65" conversion_matrices = { "xyz_to_rgb": numpy.array(( (2.04148, -0.564977, -0.344713), (-0.969258, 1.87599, 0.0415557), (0.0134455, -0.118373, 1.01527))), "rgb_to_xyz": numpy.array(( (0.576700, 0.185556, 0.188212), (0.297361, 0.627355, 0.0752847), (0.0270328, 0.0706879, 0.991248))), } class AppleRGBColor(BaseRGBColor): """ Represents an AppleRGB color. .. note:: If you pass in upscaled values, we automatically scale them down to 0.0-1.0. If you need the old upscaled values, you can retrieve them with :py:meth:`get_upscaled_value_tuple`. :ivar float rgb_r: R coordinate :ivar float rgb_g: G coordinate :ivar float rgb_b: B coordinate :ivar bool is_upscaled: If True, RGB values are between 1-255. If False, 0.0-1.0. """ #: RGB space's gamma constant. rgb_gamma = 1.8 #: The RGB space's native illuminant. Important when converting to XYZ. native_illuminant = "d65" conversion_matrices = { "xyz_to_rgb": numpy.array(( (2.9515373, -1.2894116, -0.4738445), (-1.0851093, 1.9908566, 0.0372026), (0.0854934, -0.2694964, 1.0912975))), "rgb_to_xyz": numpy.array(( (0.4497288, 0.3162486, 0.1844926), (0.2446525, 0.6720283, 0.0833192), (0.0251848, 0.1411824, 0.9224628))), } class HSLColor(ColorBase): """ Represents an HSL color. """ VALUES = ['hsl_h', 'hsl_s', 'hsl_l'] def __init__(self, hsl_h, hsl_s, hsl_l): """ :param float hsl_h: H coordinate. :param float hsl_s: S coordinate. :param float hsl_l: L coordinate. """ super(HSLColor, self).__init__() #: H coordinate self.hsl_h = float(hsl_h) #: S coordinate self.hsl_s = float(hsl_s) #: L coordinate self.hsl_l = float(hsl_l) class HSVColor(ColorBase): """ Represents an HSV color. """ VALUES = ['hsv_h', 'hsv_s', 'hsv_v'] def __init__(self, hsv_h, hsv_s, hsv_v): """ :param float hsv_h: H coordinate. :param float hsv_s: S coordinate. :param float hsv_v: V coordinate. """ super(HSVColor, self).__init__() #: H coordinate self.hsv_h = float(hsv_h) #: S coordinate self.hsv_s = float(hsv_s) #: V coordinate self.hsv_v = float(hsv_v) class CMYColor(ColorBase): """ Represents a CMY color. """ VALUES = ['cmy_c', 'cmy_m', 'cmy_y'] def __init__(self, cmy_c, cmy_m, cmy_y): """ :param float cmy_c: C coordinate. :param float cmy_m: M coordinate. :param float cmy_y: Y coordinate. """ super(CMYColor, self).__init__() #: C coordinate self.cmy_c = float(cmy_c) #: M coordinate self.cmy_m = float(cmy_m) #: Y coordinate self.cmy_y = float(cmy_y) class CMYKColor(ColorBase): """ Represents a CMYK color. """ VALUES = ['cmyk_c', 'cmyk_m', 'cmyk_y', 'cmyk_k'] def __init__(self, cmyk_c, cmyk_m, cmyk_y, cmyk_k): """ :param float cmyk_c: C coordinate. :param float cmyk_m: M coordinate. :param float cmyk_y: Y coordinate. :param float cmyk_k: K coordinate. """ super(CMYKColor, self).__init__() #: C coordinate self.cmyk_c = float(cmyk_c) #: M coordinate self.cmyk_m = float(cmyk_m) #: Y coordinate self.cmyk_y = float(cmyk_y) #: K coordinate self.cmyk_k = float(cmyk_k) class IPTColor(ColorBase): """ Represents an IPT color. Reference: Fairchild, M. D. (2013). Color appearance models, 3rd Ed. (pp. 271-272). John Wiley & Sons. """ VALUES = ['ipt_i', 'ipt_p', 'ipt_t'] conversion_matrices = { "xyz_to_lms": numpy.array(( ( 0.4002, 0.7075, -0.0807), (-0.2280, 1.1500, 0.0612), ( 0.0000, 0.0000, 0.9184))), "lms_to_ipt": numpy.array( ((0.4000, 0.4000, 0.2000), (4.4550, -4.8510, 0.3960), (0.8056, 0.3572, -1.1628))), } def __init__(self, ipt_i, ipt_p, ipt_t): """ :param ipt_i: I coordinate. :param ipt_p: P coordinate. :param ipt_t: T coordinate. """ super(IPTColor, self).__init__() #: I coordinate self.ipt_i = ipt_i #: P coordinate self.ipt_p = ipt_p #: T coordinate self.ipt_t = ipt_t @property def hue_angle(self): return numpy.arctan2(self.ipt_t, self.ipt_p)colormath-3.0.0/colormath/color_diff.py0000644000175000017500000000541513220777307022703 0ustar greg.taylorgreg.taylor00000000000000""" The functions in this module are used for comparing two LabColor objects using various Delta E formulas. """ import numpy from colormath import color_diff_matrix def _get_lab_color1_vector(color): """ Converts an LabColor into a NumPy vector. :param LabColor color: :rtype: numpy.ndarray """ if not color.__class__.__name__ == 'LabColor': raise ValueError( "Delta E functions can only be used with two LabColor objects.") return numpy.array([color.lab_l, color.lab_a, color.lab_b]) def _get_lab_color2_matrix(color): """ Converts an LabColor into a NumPy matrix. :param LabColor color: :rtype: numpy.ndarray """ if not color.__class__.__name__ == 'LabColor': raise ValueError( "Delta E functions can only be used with two LabColor objects.") return numpy.array([(color.lab_l, color.lab_a, color.lab_b)]) # noinspection PyPep8Naming def delta_e_cie1976(color1, color2): """ Calculates the Delta E (CIE1976) of two colors. """ color1_vector = _get_lab_color1_vector(color1) color2_matrix = _get_lab_color2_matrix(color2) delta_e = color_diff_matrix.delta_e_cie1976(color1_vector, color2_matrix)[0] return numpy.asscalar(delta_e) # noinspection PyPep8Naming def delta_e_cie1994(color1, color2, K_L=1, K_C=1, K_H=1, K_1=0.045, K_2=0.015): """ Calculates the Delta E (CIE1994) of two colors. K_l: 0.045 graphic arts 0.048 textiles K_2: 0.015 graphic arts 0.014 textiles K_L: 1 default 2 textiles """ color1_vector = _get_lab_color1_vector(color1) color2_matrix = _get_lab_color2_matrix(color2) delta_e = color_diff_matrix.delta_e_cie1994( color1_vector, color2_matrix, K_L=K_L, K_C=K_C, K_H=K_H, K_1=K_1, K_2=K_2)[0] return numpy.asscalar(delta_e) # noinspection PyPep8Naming def delta_e_cie2000(color1, color2, Kl=1, Kc=1, Kh=1): """ Calculates the Delta E (CIE2000) of two colors. """ color1_vector = _get_lab_color1_vector(color1) color2_matrix = _get_lab_color2_matrix(color2) delta_e = color_diff_matrix.delta_e_cie2000( color1_vector, color2_matrix, Kl=Kl, Kc=Kc, Kh=Kh)[0] return numpy.asscalar(delta_e) # noinspection PyPep8Naming def delta_e_cmc(color1, color2, pl=2, pc=1): """ Calculates the Delta E (CMC) of two colors. CMC values Acceptability: pl=2, pc=1 Perceptability: pl=1, pc=1 """ color1_vector = _get_lab_color1_vector(color1) color2_matrix = _get_lab_color2_matrix(color2) delta_e = color_diff_matrix.delta_e_cmc( color1_vector, color2_matrix, pl=pl, pc=pc)[0] return numpy.asscalar(delta_e) colormath-3.0.0/colormath/chromatic_adaptation.py0000644000175000017500000001015113220777307024743 0ustar greg.taylorgreg.taylor00000000000000import logging import numpy from numpy.linalg import pinv from colormath import color_constants logger = logging.getLogger(__name__) # noinspection PyPep8Naming def _get_adaptation_matrix(wp_src, wp_dst, observer, adaptation): """ Calculate the correct transformation matrix based on origin and target illuminants. The observer angle must be the same between illuminants. See colormath.color_constants.ADAPTATION_MATRICES for a list of possible adaptations. Detailed conversion documentation is available at: http://brucelindbloom.com/Eqn_ChromAdapt.html """ # Get the appropriate transformation matrix, [MsubA]. m_sharp = color_constants.ADAPTATION_MATRICES[adaptation] # In case the white-points are still input as strings # Get white-points for illuminant if type(wp_src) == str: orig_illum = wp_src.lower() wp_src = color_constants.ILLUMINANTS[observer][orig_illum] elif hasattr(wp_src, '__iter__'): wp_src = wp_src if type(wp_dst) == str: targ_illum = wp_dst.lower() wp_dst = color_constants.ILLUMINANTS[observer][targ_illum] elif hasattr(wp_dst, '__iter__'): wp_dst = wp_dst # Sharpened cone responses ~ rho gamma beta ~ sharpened r g b rgb_src = numpy.dot(m_sharp, wp_src) rgb_dst = numpy.dot(m_sharp, wp_dst) # Ratio of whitepoint sharpened responses m_rat = numpy.diag(rgb_dst / rgb_src) # Final transformation matrix m_xfm = numpy.dot(numpy.dot(pinv(m_sharp), m_rat), m_sharp) return m_xfm # noinspection PyPep8Naming def apply_chromatic_adaptation(val_x, val_y, val_z, orig_illum, targ_illum, observer='2', adaptation='bradford'): """ Applies a chromatic adaptation matrix to convert XYZ values between illuminants. It is important to recognize that color transformation results in color errors, determined by how far the original illuminant is from the target illuminant. For example, D65 to A could result in very high maximum deviance. An informative article with estimate average Delta E values for each illuminant conversion may be found at: http://brucelindbloom.com/ChromAdaptEval.html """ # It's silly to have to do this, but some people may want to call this # function directly, so we'll protect them from messing up upper/lower case. adaptation = adaptation.lower() # Get white-points for illuminant if type(orig_illum) == str: orig_illum = orig_illum.lower() wp_src = color_constants.ILLUMINANTS[observer][orig_illum] elif hasattr(orig_illum, '__iter__'): wp_src = orig_illum if type(targ_illum) == str: targ_illum = targ_illum.lower() wp_dst = color_constants.ILLUMINANTS[observer][targ_illum] elif hasattr(targ_illum, '__iter__'): wp_dst = targ_illum logger.debug(" \* Applying adaptation matrix: %s", adaptation) # Retrieve the appropriate transformation matrix from the constants. transform_matrix = _get_adaptation_matrix(wp_src, wp_dst, observer, adaptation) # Stuff the XYZ values into a NumPy matrix for conversion. XYZ_matrix = numpy.array((val_x, val_y, val_z)) # Perform the adaptation via matrix multiplication. result_matrix = numpy.dot(transform_matrix, XYZ_matrix) # Return individual X, Y, and Z coordinates. return result_matrix[0], result_matrix[1], result_matrix[2] # noinspection PyPep8Naming def apply_chromatic_adaptation_on_color(color, targ_illum, adaptation='bradford'): """ Convenience function to apply an adaptation directly to a Color object. """ xyz_x = color.xyz_x xyz_y = color.xyz_y xyz_z = color.xyz_z orig_illum = color.illuminant targ_illum = targ_illum.lower() observer = color.observer adaptation = adaptation.lower() # Return individual X, Y, and Z coordinates. color.xyz_x, color.xyz_y, color.xyz_z = apply_chromatic_adaptation( xyz_x, xyz_y, xyz_z, orig_illum, targ_illum, observer=observer, adaptation=adaptation) color.set_illuminant(targ_illum) return color colormath-3.0.0/colormath/color_conversions.py0000644000175000017500000007771013220777307024352 0ustar greg.taylorgreg.taylor00000000000000""" Conversion between color spaces. .. note:: This module makes extensive use of imports within functions. That stinks. """ from abc import ABCMeta, abstractmethod import math import logging import numpy import networkx from colormath import color_constants from colormath import spectral_constants from colormath.color_objects import ColorBase, XYZColor, sRGBColor, \ LCHabColor, LCHuvColor, LabColor, xyYColor, LuvColor, HSVColor, HSLColor, \ CMYColor, CMYKColor, BaseRGBColor, IPTColor, SpectralColor, AdobeRGBColor from colormath.chromatic_adaptation import apply_chromatic_adaptation from colormath.color_exceptions import InvalidIlluminantError, \ UndefinedConversionError logger = logging.getLogger(__name__) # noinspection PyPep8Naming def apply_RGB_matrix(var1, var2, var3, rgb_type, convtype="xyz_to_rgb"): """ Applies an RGB working matrix to convert from XYZ to RGB. The arguments are tersely named var1, var2, and var3 to allow for the passing of XYZ _or_ RGB values. var1 is X for XYZ, and R for RGB. var2 and var3 follow suite. """ convtype = convtype.lower() # Retrieve the appropriate transformation matrix from the constants. rgb_matrix = rgb_type.conversion_matrices[convtype] logger.debug(" \* Applying RGB conversion matrix: %s->%s", rgb_type.__class__.__name__, convtype) # Stuff the RGB/XYZ values into a NumPy matrix for conversion. var_matrix = numpy.array(( var1, var2, var3 )) # Perform the adaptation via matrix multiplication. result_matrix = numpy.dot(rgb_matrix, var_matrix) rgb_r, rgb_g, rgb_b = result_matrix # Clamp these values to a valid range. rgb_r = max(rgb_r, 0.0) rgb_g = max(rgb_g, 0.0) rgb_b = max(rgb_b, 0.0) return rgb_r, rgb_g, rgb_b # Avoid the repetition, since the conversion tables for the various RGB # spaces are the same. _RGB_SPACES = [sRGBColor, AdobeRGBColor] class ConversionManager(object): __metaclass__ = ABCMeta def __init__(self): self.registered_color_spaces = set() def add_type_conversion(self, start_type, target_type, conversion_function): """ Register a conversion function between two color spaces. :param start_type: Starting color space. :param target_type: Target color space. :param conversion_function: Conversion function. """ self.registered_color_spaces.add(start_type) self.registered_color_spaces.add(target_type) logger.debug( 'Registered conversion from %s to %s', start_type, target_type) @abstractmethod def get_conversion_path(self, start_type, target_type): """ Return a list of conversion functions that if applied iteratively on a color of the start_type color space result in a color in the result_type color space. Raises an UndefinedConversionError if no valid conversion path can be found. :param start_type: Starting color space type. :param target_type: Target color space type. :return: List of conversion functions. """ pass @staticmethod def _normalise_type(color_type): """ Return the highest superclass that is valid for color space conversions (e.g., AdobeRGB -> BaseRGBColor). """ if issubclass(color_type, BaseRGBColor): return BaseRGBColor else: return color_type class GraphConversionManager(ConversionManager): def __init__(self): super(GraphConversionManager, self).__init__() self.conversion_graph = networkx.DiGraph() def get_conversion_path(self, start_type, target_type): start_type = self._normalise_type(start_type) target_type = self._normalise_type(target_type) try: # Retrieve node sequence that leads from start_type to target_type. return self._find_shortest_path(start_type, target_type) except (networkx.NetworkXNoPath, networkx.NodeNotFound): raise UndefinedConversionError( start_type, target_type, ) def _find_shortest_path(self, start_type, target_type): path = networkx.shortest_path( self.conversion_graph, start_type, target_type) # Look up edges between nodes and retrieve the conversion function # for each edge. return [ self.conversion_graph.get_edge_data(node_a, node_b)['conversion_function'] for node_a, node_b in zip(path[:-1], path[1:]) ] def add_type_conversion(self, start_type, target_type, conversion_function): super(GraphConversionManager, self).add_type_conversion( start_type, target_type, conversion_function) self.conversion_graph.add_edge( start_type, target_type, conversion_function=conversion_function) class DummyConversionManager(ConversionManager): def add_type_conversion(self, start_type, target_type, conversion_function): pass def get_conversion_path(self, start_type, target_type): raise UndefinedConversionError( start_type, target_type, ) _conversion_manager = GraphConversionManager() def color_conversion_function(start_type, target_type): """ Decorator to indicate a function that performs a conversion from one color space to another. This decorator will return the original function unmodified, however it will be registered in the _conversion_manager so it can be used to perform color space transformations between color spaces that do not have direct conversion functions (e.g., Luv to CMYK). Note: For a conversion to/from RGB supply the BaseRGBColor class. :param start_type: Starting color space type :param target_type: Target color space type """ def decorator(f): f.start_type = start_type f.target_type = target_type _conversion_manager.add_type_conversion(start_type, target_type, f) return f return decorator # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(SpectralColor, XYZColor) def Spectral_to_XYZ(cobj, illuminant_override=None, *args, **kwargs): """ Converts spectral readings to XYZ. """ # If the user provides an illuminant_override numpy array, use it. if illuminant_override: reference_illum = illuminant_override else: # Otherwise, look up the illuminant from known standards based # on the value of 'illuminant' pulled from the SpectralColor object. try: reference_illum = spectral_constants.REF_ILLUM_TABLE[cobj.illuminant] except KeyError: raise InvalidIlluminantError(cobj.illuminant) # Get the spectral distribution of the selected standard observer. if cobj.observer == '10': std_obs_x = spectral_constants.STDOBSERV_X10 std_obs_y = spectral_constants.STDOBSERV_Y10 std_obs_z = spectral_constants.STDOBSERV_Z10 else: # Assume 2 degree, since it is theoretically the only other possibility. std_obs_x = spectral_constants.STDOBSERV_X2 std_obs_y = spectral_constants.STDOBSERV_Y2 std_obs_z = spectral_constants.STDOBSERV_Z2 # This is a NumPy array containing the spectral distribution of the color. sample = cobj.get_numpy_array() # The denominator is constant throughout the entire calculation for X, # Y, and Z coordinates. Calculate it once and re-use. denom = std_obs_y * reference_illum # This is also a common element in the calculation whereby the sample # NumPy array is multiplied by the reference illuminant's power distribution # (which is also a NumPy array). sample_by_ref_illum = sample * reference_illum # Calculate the numerator of the equation to find X. x_numerator = sample_by_ref_illum * std_obs_x y_numerator = sample_by_ref_illum * std_obs_y z_numerator = sample_by_ref_illum * std_obs_z xyz_x = x_numerator.sum() / denom.sum() xyz_y = y_numerator.sum() / denom.sum() xyz_z = z_numerator.sum() / denom.sum() return XYZColor( xyz_x, xyz_y, xyz_z, observer=cobj.observer, illuminant=cobj.illuminant) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(LabColor, LCHabColor) def Lab_to_LCHab(cobj, *args, **kwargs): """ Convert from CIE Lab to LCH(ab). """ lch_l = cobj.lab_l lch_c = math.sqrt( math.pow(float(cobj.lab_a), 2) + math.pow(float(cobj.lab_b), 2)) lch_h = math.atan2(float(cobj.lab_b), float(cobj.lab_a)) if lch_h > 0: lch_h = (lch_h / math.pi) * 180 else: lch_h = 360 - (math.fabs(lch_h) / math.pi) * 180 return LCHabColor( lch_l, lch_c, lch_h, observer=cobj.observer, illuminant=cobj.illuminant) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(LabColor, XYZColor) def Lab_to_XYZ(cobj, *args, **kwargs): """ Convert from Lab to XYZ """ illum = cobj.get_illuminant_xyz() xyz_y = (cobj.lab_l + 16.0) / 116.0 xyz_x = cobj.lab_a / 500.0 + xyz_y xyz_z = xyz_y - cobj.lab_b / 200.0 if math.pow(xyz_y, 3) > color_constants.CIE_E: xyz_y = math.pow(xyz_y, 3) else: xyz_y = (xyz_y - 16.0 / 116.0) / 7.787 if math.pow(xyz_x, 3) > color_constants.CIE_E: xyz_x = math.pow(xyz_x, 3) else: xyz_x = (xyz_x - 16.0 / 116.0) / 7.787 if math.pow(xyz_z, 3) > color_constants.CIE_E: xyz_z = math.pow(xyz_z, 3) else: xyz_z = (xyz_z - 16.0 / 116.0) / 7.787 xyz_x = (illum["X"] * xyz_x) xyz_y = (illum["Y"] * xyz_y) xyz_z = (illum["Z"] * xyz_z) return XYZColor( xyz_x, xyz_y, xyz_z, observer=cobj.observer, illuminant=cobj.illuminant) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(LuvColor, LCHuvColor) def Luv_to_LCHuv(cobj, *args, **kwargs): """ Convert from CIE Luv to LCH(uv). """ lch_l = cobj.luv_l lch_c = math.sqrt(math.pow(cobj.luv_u, 2.0) + math.pow(cobj.luv_v, 2.0)) lch_h = math.atan2(float(cobj.luv_v), float(cobj.luv_u)) if lch_h > 0: lch_h = (lch_h / math.pi) * 180 else: lch_h = 360 - (math.fabs(lch_h) / math.pi) * 180 return LCHuvColor( lch_l, lch_c, lch_h, observer=cobj.observer, illuminant=cobj.illuminant) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(LuvColor, XYZColor) def Luv_to_XYZ(cobj, *args, **kwargs): """ Convert from Luv to XYZ. """ illum = cobj.get_illuminant_xyz() # Without Light, there is no color. Short-circuit this and avoid some # zero division errors in the var_a_frac calculation. if cobj.luv_l <= 0.0: xyz_x = 0.0 xyz_y = 0.0 xyz_z = 0.0 return XYZColor( xyz_x, xyz_y, xyz_z, observer=cobj.observer, illuminant=cobj.illuminant) # Various variables used throughout the conversion. cie_k_times_e = color_constants.CIE_K * color_constants.CIE_E u_sub_0 = (4.0 * illum["X"]) / (illum["X"] + 15.0 * illum["Y"] + 3.0 * illum["Z"]) v_sub_0 = (9.0 * illum["Y"]) / (illum["X"] + 15.0 * illum["Y"] + 3.0 * illum["Z"]) var_u = cobj.luv_u / (13.0 * cobj.luv_l) + u_sub_0 var_v = cobj.luv_v / (13.0 * cobj.luv_l) + v_sub_0 # Y-coordinate calculations. if cobj.luv_l > cie_k_times_e: xyz_y = math.pow((cobj.luv_l + 16.0) / 116.0, 3.0) else: xyz_y = cobj.luv_l / color_constants.CIE_K # X-coordinate calculation. xyz_x = xyz_y * 9.0 * var_u / (4.0 * var_v) # Z-coordinate calculation. xyz_z = xyz_y * (12.0 - 3.0 * var_u - 20.0 * var_v) / (4.0 * var_v) return XYZColor( xyz_x, xyz_y, xyz_z, illuminant=cobj.illuminant, observer=cobj.observer) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(LCHabColor, LabColor) def LCHab_to_Lab(cobj, *args, **kwargs): """ Convert from LCH(ab) to Lab. """ lab_l = cobj.lch_l lab_a = math.cos(math.radians(cobj.lch_h)) * cobj.lch_c lab_b = math.sin(math.radians(cobj.lch_h)) * cobj.lch_c return LabColor( lab_l, lab_a, lab_b, illuminant=cobj.illuminant, observer=cobj.observer) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(LCHuvColor, LuvColor) def LCHuv_to_Luv(cobj, *args, **kwargs): """ Convert from LCH(uv) to Luv. """ luv_l = cobj.lch_l luv_u = math.cos(math.radians(cobj.lch_h)) * cobj.lch_c luv_v = math.sin(math.radians(cobj.lch_h)) * cobj.lch_c return LuvColor( luv_l, luv_u, luv_v, illuminant=cobj.illuminant, observer=cobj.observer) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(xyYColor, XYZColor) def xyY_to_XYZ(cobj, *args, **kwargs): """ Convert from xyY to XYZ. """ # avoid division by zero if cobj.xyy_y == 0.0: xyz_x = 0.0 xyz_y = 0.0 xyz_z = 0.0 else: xyz_x = (cobj.xyy_x * cobj.xyy_Y) / cobj.xyy_y xyz_y = cobj.xyy_Y xyz_z = ((1.0 - cobj.xyy_x - cobj.xyy_y) * xyz_y) / cobj.xyy_y return XYZColor( xyz_x, xyz_y, xyz_z, illuminant=cobj.illuminant, observer=cobj.observer) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(XYZColor, xyYColor) def XYZ_to_xyY(cobj, *args, **kwargs): """ Convert from XYZ to xyY. """ xyz_sum = cobj.xyz_x + cobj.xyz_y + cobj.xyz_z # avoid division by zero if xyz_sum == 0.0: xyy_x = 0.0 xyy_y = 0.0 else: xyy_x = cobj.xyz_x / xyz_sum xyy_y = cobj.xyz_y / xyz_sum xyy_Y = cobj.xyz_y return xyYColor( xyy_x, xyy_y, xyy_Y, observer=cobj.observer, illuminant=cobj.illuminant) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(XYZColor, LuvColor) def XYZ_to_Luv(cobj, *args, **kwargs): """ Convert from XYZ to Luv """ temp_x = cobj.xyz_x temp_y = cobj.xyz_y temp_z = cobj.xyz_z denom = temp_x + (15.0 * temp_y) + (3.0 * temp_z) # avoid division by zero if denom == 0.0: luv_u = 0.0 luv_v = 0.0 else: luv_u = (4.0 * temp_x) / denom luv_v = (9.0 * temp_y) / denom illum = cobj.get_illuminant_xyz() temp_y = temp_y / illum["Y"] if temp_y > color_constants.CIE_E: temp_y = math.pow(temp_y, (1.0 / 3.0)) else: temp_y = (7.787 * temp_y) + (16.0 / 116.0) ref_U = (4.0 * illum["X"]) / (illum["X"] + (15.0 * illum["Y"]) + (3.0 * illum["Z"])) ref_V = (9.0 * illum["Y"]) / (illum["X"] + (15.0 * illum["Y"]) + (3.0 * illum["Z"])) luv_l = (116.0 * temp_y) - 16.0 luv_u = 13.0 * luv_l * (luv_u - ref_U) luv_v = 13.0 * luv_l * (luv_v - ref_V) return LuvColor( luv_l, luv_u, luv_v, observer=cobj.observer, illuminant=cobj.illuminant) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(XYZColor, LabColor) def XYZ_to_Lab(cobj, *args, **kwargs): """ Converts XYZ to Lab. """ illum = cobj.get_illuminant_xyz() temp_x = cobj.xyz_x / illum["X"] temp_y = cobj.xyz_y / illum["Y"] temp_z = cobj.xyz_z / illum["Z"] if temp_x > color_constants.CIE_E: temp_x = math.pow(temp_x, (1.0 / 3.0)) else: temp_x = (7.787 * temp_x) + (16.0 / 116.0) if temp_y > color_constants.CIE_E: temp_y = math.pow(temp_y, (1.0 / 3.0)) else: temp_y = (7.787 * temp_y) + (16.0 / 116.0) if temp_z > color_constants.CIE_E: temp_z = math.pow(temp_z, (1.0 / 3.0)) else: temp_z = (7.787 * temp_z) + (16.0 / 116.0) lab_l = (116.0 * temp_y) - 16.0 lab_a = 500.0 * (temp_x - temp_y) lab_b = 200.0 * (temp_y - temp_z) return LabColor( lab_l, lab_a, lab_b, observer=cobj.observer, illuminant=cobj.illuminant) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(XYZColor, BaseRGBColor) def XYZ_to_RGB(cobj, target_rgb, *args, **kwargs): """ XYZ to RGB conversion. """ temp_X = cobj.xyz_x temp_Y = cobj.xyz_y temp_Z = cobj.xyz_z logger.debug(" \- Target RGB space: %s", target_rgb) target_illum = target_rgb.native_illuminant logger.debug(" \- Target native illuminant: %s", target_illum) logger.debug(" \- XYZ color's illuminant: %s", cobj.illuminant) # If the XYZ values were taken with a different reference white than the # native reference white of the target RGB space, a transformation matrix # must be applied. if cobj.illuminant != target_illum: logger.debug(" \* Applying transformation from %s to %s ", cobj.illuminant, target_illum) # Get the adjusted XYZ values, adapted for the target illuminant. temp_X, temp_Y, temp_Z = apply_chromatic_adaptation( temp_X, temp_Y, temp_Z, orig_illum=cobj.illuminant, targ_illum=target_illum) logger.debug(" \* New values: %.3f, %.3f, %.3f", temp_X, temp_Y, temp_Z) # Apply an RGB working space matrix to the XYZ values (matrix mul). rgb_r, rgb_g, rgb_b = apply_RGB_matrix( temp_X, temp_Y, temp_Z, rgb_type=target_rgb, convtype="xyz_to_rgb") # v linear_channels = dict(r=rgb_r, g=rgb_g, b=rgb_b) # V nonlinear_channels = {} if target_rgb == sRGBColor: for channel in ['r', 'g', 'b']: v = linear_channels[channel] if v <= 0.0031308: nonlinear_channels[channel] = v * 12.92 else: nonlinear_channels[channel] = 1.055 * math.pow(v, 1 / 2.4) - 0.055 else: # If it's not sRGB... for channel in ['r', 'g', 'b']: v = linear_channels[channel] nonlinear_channels[channel] = math.pow(v, 1 / target_rgb.rgb_gamma) return target_rgb( nonlinear_channels['r'], nonlinear_channels['g'], nonlinear_channels['b']) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(BaseRGBColor, XYZColor) def RGB_to_XYZ(cobj, target_illuminant=None, *args, **kwargs): """ RGB to XYZ conversion. Expects 0-255 RGB values. Based off of: http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html """ # Will contain linearized RGB channels (removed the gamma func). linear_channels = {} if isinstance(cobj, sRGBColor): for channel in ['r', 'g', 'b']: V = getattr(cobj, 'rgb_' + channel) if V <= 0.04045: linear_channels[channel] = V / 12.92 else: linear_channels[channel] = math.pow((V + 0.055) / 1.055, 2.4) else: # If it's not sRGB... gamma = cobj.rgb_gamma for channel in ['r', 'g', 'b']: V = getattr(cobj, 'rgb_' + channel) linear_channels[channel] = math.pow(V, gamma) # Apply an RGB working space matrix to the XYZ values (matrix mul). xyz_x, xyz_y, xyz_z = apply_RGB_matrix( linear_channels['r'], linear_channels['g'], linear_channels['b'], rgb_type=cobj, convtype="rgb_to_xyz") if target_illuminant is None: target_illuminant = cobj.native_illuminant # The illuminant of the original RGB object. This will always match # the RGB colorspace's native illuminant. illuminant = cobj.native_illuminant xyzcolor = XYZColor(xyz_x, xyz_y, xyz_z, illuminant=illuminant) # This will take care of any illuminant changes for us (if source # illuminant != target illuminant). xyzcolor.apply_adaptation(target_illuminant) return xyzcolor # noinspection PyPep8Naming,PyUnusedLocal def __RGB_to_Hue(var_R, var_G, var_B, var_min, var_max): """ For RGB_to_HSL and RGB_to_HSV, the Hue (H) component is calculated in the same way. """ if var_max == var_min: return 0.0 elif var_max == var_R: return (60.0 * ((var_G - var_B) / (var_max - var_min)) + 360) % 360.0 elif var_max == var_G: return 60.0 * ((var_B - var_R) / (var_max - var_min)) + 120 elif var_max == var_B: return 60.0 * ((var_R - var_G) / (var_max - var_min)) + 240.0 # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(BaseRGBColor, HSVColor) def RGB_to_HSV(cobj, *args, **kwargs): """ Converts from RGB to HSV. H values are in degrees and are 0 to 360. S values are a percentage, 0.0 to 1.0. V values are a percentage, 0.0 to 1.0. """ var_R = cobj.rgb_r var_G = cobj.rgb_g var_B = cobj.rgb_b var_max = max(var_R, var_G, var_B) var_min = min(var_R, var_G, var_B) var_H = __RGB_to_Hue(var_R, var_G, var_B, var_min, var_max) if var_max == 0: var_S = 0 else: var_S = 1.0 - (var_min / var_max) var_V = var_max hsv_h = var_H hsv_s = var_S hsv_v = var_V return HSVColor( var_H, var_S, var_V) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(BaseRGBColor, HSLColor) def RGB_to_HSL(cobj, *args, **kwargs): """ Converts from RGB to HSL. H values are in degrees and are 0 to 360. S values are a percentage, 0.0 to 1.0. L values are a percentage, 0.0 to 1.0. """ var_R = cobj.rgb_r var_G = cobj.rgb_g var_B = cobj.rgb_b var_max = max(var_R, var_G, var_B) var_min = min(var_R, var_G, var_B) var_H = __RGB_to_Hue(var_R, var_G, var_B, var_min, var_max) var_L = 0.5 * (var_max + var_min) if var_max == var_min: var_S = 0 elif var_L <= 0.5: var_S = (var_max - var_min) / (2.0 * var_L) else: var_S = (var_max - var_min) / (2.0 - (2.0 * var_L)) return HSLColor( var_H, var_S, var_L) # noinspection PyPep8Naming,PyUnusedLocal def __Calc_HSL_to_RGB_Components(var_q, var_p, C): """ This is used in HSL_to_RGB conversions on R, G, and B. """ if C < 0: C += 1.0 if C > 1: C -= 1.0 # Computing C of vector (Color R, Color G, Color B) if C < (1.0 / 6.0): return var_p + ((var_q - var_p) * 6.0 * C) elif (1.0 / 6.0) <= C < 0.5: return var_q elif 0.5 <= C < (2.0 / 3.0): return var_p + ((var_q - var_p) * 6.0 * ((2.0 / 3.0) - C)) else: return var_p # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(HSVColor, BaseRGBColor) def HSV_to_RGB(cobj, target_rgb, *args, **kwargs): """ HSV to RGB conversion. H values are in degrees and are 0 to 360. S values are a percentage, 0.0 to 1.0. V values are a percentage, 0.0 to 1.0. """ H = cobj.hsv_h S = cobj.hsv_s V = cobj.hsv_v h_floored = int(math.floor(H)) h_sub_i = int(h_floored / 60) % 6 var_f = (H / 60.0) - (h_floored // 60) var_p = V * (1.0 - S) var_q = V * (1.0 - var_f * S) var_t = V * (1.0 - (1.0 - var_f) * S) if h_sub_i == 0: rgb_r = V rgb_g = var_t rgb_b = var_p elif h_sub_i == 1: rgb_r = var_q rgb_g = V rgb_b = var_p elif h_sub_i == 2: rgb_r = var_p rgb_g = V rgb_b = var_t elif h_sub_i == 3: rgb_r = var_p rgb_g = var_q rgb_b = V elif h_sub_i == 4: rgb_r = var_t rgb_g = var_p rgb_b = V elif h_sub_i == 5: rgb_r = V rgb_g = var_p rgb_b = var_q else: raise ValueError("Unable to convert HSL->RGB due to value error.") # In the event that they define an HSV color and want to convert it to # a particular RGB space, let them override it here. if target_rgb is not None: rgb_type = target_rgb else: rgb_type = cobj.rgb_type return target_rgb(rgb_r, rgb_g, rgb_b) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(HSLColor, BaseRGBColor) def HSL_to_RGB(cobj, target_rgb, *args, **kwargs): """ HSL to RGB conversion. """ H = cobj.hsl_h S = cobj.hsl_s L = cobj.hsl_l if L < 0.5: var_q = L * (1.0 + S) else: var_q = L + S - (L * S) var_p = 2.0 * L - var_q # H normalized to range [0,1] h_sub_k = (H / 360.0) t_sub_R = h_sub_k + (1.0 / 3.0) t_sub_G = h_sub_k t_sub_B = h_sub_k - (1.0 / 3.0) rgb_r = __Calc_HSL_to_RGB_Components(var_q, var_p, t_sub_R) rgb_g = __Calc_HSL_to_RGB_Components(var_q, var_p, t_sub_G) rgb_b = __Calc_HSL_to_RGB_Components(var_q, var_p, t_sub_B) # In the event that they define an HSV color and want to convert it to # a particular RGB space, let them override it here. if target_rgb is not None: rgb_type = target_rgb else: rgb_type = cobj.rgb_type return target_rgb(rgb_r, rgb_g, rgb_b) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(BaseRGBColor, CMYColor) def RGB_to_CMY(cobj, *args, **kwargs): """ RGB to CMY conversion. NOTE: CMYK and CMY values range from 0.0 to 1.0 """ cmy_c = 1.0 - cobj.rgb_r cmy_m = 1.0 - cobj.rgb_g cmy_y = 1.0 - cobj.rgb_b return CMYColor(cmy_c, cmy_m, cmy_y) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(CMYColor, BaseRGBColor) def CMY_to_RGB(cobj, target_rgb, *args, **kwargs): """ Converts CMY to RGB via simple subtraction. NOTE: Returned values are in the range of 0-255. """ rgb_r = 1.0 - cobj.cmy_c rgb_g = 1.0 - cobj.cmy_m rgb_b = 1.0 - cobj.cmy_y return target_rgb(rgb_r, rgb_g, rgb_b) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(CMYColor, CMYKColor) def CMY_to_CMYK(cobj, *args, **kwargs): """ Converts from CMY to CMYK. NOTE: CMYK and CMY values range from 0.0 to 1.0 """ var_k = 1.0 if cobj.cmy_c < var_k: var_k = cobj.cmy_c if cobj.cmy_m < var_k: var_k = cobj.cmy_m if cobj.cmy_y < var_k: var_k = cobj.cmy_y if var_k == 1: cmyk_c = 0.0 cmyk_m = 0.0 cmyk_y = 0.0 else: cmyk_c = (cobj.cmy_c - var_k) / (1.0 - var_k) cmyk_m = (cobj.cmy_m - var_k) / (1.0 - var_k) cmyk_y = (cobj.cmy_y - var_k) / (1.0 - var_k) cmyk_k = var_k return CMYKColor(cmyk_c, cmyk_m, cmyk_y, cmyk_k) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(CMYKColor, CMYColor) def CMYK_to_CMY(cobj, *args, **kwargs): """ Converts CMYK to CMY. NOTE: CMYK and CMY values range from 0.0 to 1.0 """ cmy_c = cobj.cmyk_c * (1.0 - cobj.cmyk_k) + cobj.cmyk_k cmy_m = cobj.cmyk_m * (1.0 - cobj.cmyk_k) + cobj.cmyk_k cmy_y = cobj.cmyk_y * (1.0 - cobj.cmyk_k) + cobj.cmyk_k return CMYColor(cmy_c, cmy_m, cmy_y) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(XYZColor, IPTColor) def XYZ_to_IPT(cobj, *args, **kwargs): """ Converts XYZ to IPT. NOTE: XYZ values need to be adapted to 2 degree D65 Reference: Fairchild, M. D. (2013). Color appearance models, 3rd Ed. (pp. 271-272). John Wiley & Sons. """ if cobj.illuminant != 'd65' or cobj.observer != '2': raise ValueError('XYZColor for XYZ->IPT conversion needs to be D65 adapted.') xyz_values = numpy.array(cobj.get_value_tuple()) lms_values = numpy.dot( IPTColor.conversion_matrices['xyz_to_lms'], xyz_values) lms_prime = numpy.sign(lms_values) * numpy.abs(lms_values) ** 0.43 ipt_values = numpy.dot( IPTColor.conversion_matrices['lms_to_ipt'], lms_prime) return IPTColor(*ipt_values) # noinspection PyPep8Naming,PyUnusedLocal @color_conversion_function(IPTColor, XYZColor) def IPT_to_XYZ(cobj, *args, **kwargs): """ Converts IPT to XYZ. """ ipt_values = numpy.array(cobj.get_value_tuple()) lms_values = numpy.dot( numpy.linalg.inv(IPTColor.conversion_matrices['lms_to_ipt']), ipt_values) lms_prime = numpy.sign(lms_values) * numpy.abs(lms_values) ** (1 / 0.43) xyz_values = numpy.dot( numpy.linalg.inv(IPTColor.conversion_matrices['xyz_to_lms']), lms_prime) return XYZColor(*xyz_values, observer='2', illuminant='d65') # We use this as a template conversion dict for each RGB color space. They # are all identical. _RGB_CONVERSION_DICT_TEMPLATE = { "HSLColor": [RGB_to_HSL], "HSVColor": [RGB_to_HSV], "CMYColor": [RGB_to_CMY], "CMYKColor": [RGB_to_CMY, CMY_to_CMYK], "XYZColor": [RGB_to_XYZ], "xyYColor": [RGB_to_XYZ, XYZ_to_xyY], "LabColor": [RGB_to_XYZ, XYZ_to_Lab], "LCHabColor": [RGB_to_XYZ, XYZ_to_Lab, Lab_to_LCHab], "LCHuvColor": [RGB_to_XYZ, XYZ_to_Luv, Luv_to_LCHuv], "LuvColor": [RGB_to_XYZ, XYZ_to_Luv], "IPTColor": [RGB_to_XYZ, XYZ_to_IPT], } def convert_color(color, target_cs, through_rgb_type=sRGBColor, target_illuminant=None, *args, **kwargs): """ Converts the color to the designated color space. :param color: A Color instance to convert. :param target_cs: The Color class to convert to. Note that this is not an instance, but a class. :keyword BaseRGBColor through_rgb_type: If during your conversion between your original and target color spaces you have to pass through RGB, this determines which kind of RGB to use. For example, XYZ->HSL. You probably don't need to specify this unless you have a special usage case. :type target_illuminant: None or str :keyword target_illuminant: If during conversion from RGB to a reflective color space you want to explicitly end up with a certain illuminant, pass this here. Otherwise the RGB space's native illuminant will be used. :returns: An instance of the type passed in as ``target_cs``. :raises: :py:exc:`colormath.color_exceptions.UndefinedConversionError` if conversion between the two color spaces isn't possible. """ if isinstance(target_cs, str): raise ValueError("target_cs parameter must be a Color object.") if not issubclass(target_cs, ColorBase): raise ValueError("target_cs parameter must be a Color object.") conversions = _conversion_manager.get_conversion_path(color.__class__, target_cs) logger.debug('Converting %s to %s', color, target_cs) logger.debug(' @ Conversion path: %s', conversions) # Start with original color in case we convert to the same color space. new_color = color if issubclass(target_cs, BaseRGBColor): # If the target_cs is an RGB color space of some sort, then we # have to set our through_rgb_type to make sure the conversion returns # the expected RGB colorspace (instead of defaulting to sRGBColor). through_rgb_type = target_cs # We have to be careful to use the same RGB color space that created # an object (if it was created by a conversion) in order to get correct # results. For example, XYZ->HSL via Adobe RGB should default to Adobe # RGB when taking that generated HSL object back to XYZ. # noinspection PyProtectedMember if through_rgb_type != sRGBColor: # User overrides take priority over everything. # noinspection PyProtectedMember target_rgb = through_rgb_type elif color._through_rgb_type: # Otherwise, a value on the color object is the next best thing, # when available. # noinspection PyProtectedMember target_rgb = color._through_rgb_type else: # We could collapse this into a single if statement above, # but I think this reads better. target_rgb = through_rgb_type # Iterate through the list of functions for the conversion path, storing # the results in a dictionary via update(). This way the user has access # to all of the variables involved in the conversion. for func in conversions: # Execute the function in this conversion step and store the resulting # Color object. logger.debug(' * Conversion: %s passed to %s()', new_color.__class__.__name__, func) logger.debug(' |-> in %s', new_color) if func: # This can be None if you try to convert a color to the color # space that is already in. IE: XYZ->XYZ. new_color = func( new_color, target_rgb=target_rgb, target_illuminant=target_illuminant, *args, **kwargs) logger.debug(' |-< out %s', new_color) # If this conversion had something other than the default sRGB color space # requested, if through_rgb_type != sRGBColor: new_color._through_rgb_type = through_rgb_type return new_color colormath-3.0.0/colormath/density_standards.py0000644000175000017500000002223613162226447024316 0ustar greg.taylorgreg.taylor00000000000000""" Various density standards. """ from numpy import array # Visual density is typically used on grey patches. Take a reading and get # the density values of the Red, Green, and Blue filters. If the difference # between the highest and lowest value is less than or equal to the value # below, return the density reading calculated against the ISO Visual spectral # weighting curve. The X-Rite 500 uses a thresh of 0.05, the X-Rite i1 appears # to use 0.08. VISUAL_DENSITY_THRESH = 0.08 ANSI_STATUS_A_RED = array(( 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.37, 43.45, 100.00, 74.30, 40.18, 19.32, 7.94, 3.56, 1.46, 0.60, 0.24, 0.09, 0.04, 0.01, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ANSI_STATUS_A_GREEN = array(( 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.04, 6.64, 60.53, 100.00, 80.54, 44.06, 16.63, 4.06, 0.58, 0.04, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ANSI_STATUS_A_BLUE = array(( 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 4.00, 65.92, 100.00, 81.66, 41.69, 10.96, 0.79, 0.04, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ANSI_STATUS_E_RED = array(( 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.01, 0.06, 0.45, 29.99, 100.00, 84.92, 54.95, 25.00, 10.00, 5.00, 1.50, 0.50, 0.30, 0.15, 0.05, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ANSI_STATUS_E_GREEN = array(( 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.01, 1.00, 5.00, 27.99, 68.08, 92.04, 100.00, 87.90, 66.07, 41.98, 21.98, 8.99, 2.50, 0.70, 0.09, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ANSI_STATUS_E_BLUE = array(( 0.00, 0.00, 0.00, 0.01, 0.27, 2.70, 13.00, 29.99, 59.98, 82.04, 100.00, 90.99, 76.03, 46.99, 17.99, 6.00, 0.80, 0.05, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ANSI_STATUS_M_RED = array(( 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.13, 30.13, 100.00, 79.25, 37.84, 17.86, 7.50, 3.10, 1.26, 0.49, 0.19, 0.07, 0.03, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ANSI_STATUS_M_GREEN = array(( 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.01, 0.16, 1.43, 6.37, 18.71, 42.27, 74.47, 100.00, 98.86, 65.77, 28.71, 8.22, 1.49, 0.17, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ANSI_STATUS_M_BLUE = array(( 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.13, 12.91, 42.85, 74.30, 100.00, 90.16, 55.34, 22.03, 5.53, 0.98, 0.07, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ANSI_STATUS_T_RED = array(( 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.06, 0.45, 29.99, 100.00, 84.92, 54.95, 25.00, 10.00, 5.00, 1.50, 0.50, 0.30, 0.15, 0.05, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ANSI_STATUS_T_GREEN = array(( 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 5.00, 27.99, 68.08, 92.04, 100.00, 87.90, 66.07, 41.98, 21.98, 8.99, 2.50, 0.70, 0.09, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ANSI_STATUS_T_BLUE = array(( 0.00, 0.01, 0.02, 0.10, 0.30, 1.50, 6.00, 16.98, 39.99, 59.98, 82.04, 93.97, 100.00, 97.05, 84.92, 65.01, 39.99, 17.99, 5.00, 0.20, 0.04, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) TYPE1 = array(( 0.00, 0.00, 0.01, 0.04, 0.72, 28.84, 100.00, 28.84, 0.72, 0.04, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) TYPE2 = array(( 0.01, 0.51, 19.05, 38.28, 57.54, 70.96, 82.41, 90.36, 97.27, 100.00, 97.72, 89.33, 73.11, 55.34, 38.19, 22.44, 9.84, 2.52, 0.64, 0.16, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) ISO_VISUAL = array(( 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.01, 0.02, 0.08, 0.28, 0.65, 1.23, 2.22, 3.82, 6.58, 10.99, 18.88, 32.58, 50.35, 66.83, 80.35, 90.57, 97.50, 100.00, 97.50, 90.36, 79.80, 67.14, 53.83, 39.17, 27.10, 17.30, 10.30, 5.61, 3.09, 1.54, 0.80, 0.42, 0.22, 0.11, 0.05, 0.03, 0.01, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 )) colormath-3.0.0/README.rst0000644000175000017500000000230713220777307017717 0ustar greg.taylorgreg.taylor00000000000000Python Color Math Module (colormath) ==================================== .. image:: https://travis-ci.org/gtaylor/python-colormath.png?branch=master :target: https://travis-ci.org/gtaylor/python-colormath .. image:: https://pypip.in/d/colormath/badge.png :target: https://crate.io/packages/colormath/ This module implements a large number of different color operations such as color space conversions, Delta E, and density to spectral. Requirements ------------ * numpy * NetworkX 2.0+ * Python 2.7 or Python 3.5+ Installation ------------ The easiest way to install colormath is via pip/easy_install:: pip install colormath Documentation ------------- For documentation, see the project webpage at: http://python-colormath.readthedocs.org/ There are also a lot of useful examples under the examples directory within this directory. Support ------- Head over to https://github.com/gtaylor/python-colormath and submit an issue if you have any problems or questions. Legal Mumbo Jumbo ----------------- Copyright (C) 2018 `Gregory Taylor`_ This software is licensed under the BSD License. .. _Gregory Taylor: http://gc-taylor.com colormath-3.0.0/examples/0000755000175000017500000000000013220777516020046 5ustar greg.taylorgreg.taylor00000000000000colormath-3.0.0/examples/conversions.py0000644000175000017500000001110713162226447022765 0ustar greg.taylorgreg.taylor00000000000000""" This module shows you how to perform color space conversions. Please see the chart on www.brucelindbloom.com/Math.html for an illustration of the conversions you may perform. """ # Does some sys.path manipulation so we can run examples in-place. # noinspection PyUnresolvedReferences import example_config from colormath.color_conversions import convert_color from colormath.color_objects import LabColor, LCHabColor, SpectralColor, sRGBColor, \ XYZColor, LCHuvColor, IPTColor def example_lab_to_xyz(): """ This function shows a simple conversion of an Lab color to an XYZ color. """ print("=== Simple Example: Lab->XYZ ===") # Instantiate an Lab color object with the given values. lab = LabColor(0.903, 16.296, -2.22) # Show a string representation. print(lab) # Convert to XYZ. xyz = convert_color(lab, XYZColor) print(xyz) print("=== End Example ===\n") def example_lchab_to_lchuv(): """ This function shows very complex chain of conversions in action. LCHab to LCHuv involves four different calculations, making this the conversion requiring the most steps. """ print("=== Complex Example: LCHab->LCHuv ===") # Instantiate an LCHab color object with the given values. lchab = LCHabColor(0.903, 16.447, 352.252) # Show a string representation. print(lchab) # Convert to LCHuv. lchuv = convert_color(lchab, LCHuvColor) print(lchuv) print("=== End Example ===\n") def example_lab_to_rgb(): """ Conversions to RGB are a little more complex mathematically. There are also several kinds of RGB color spaces. When converting from a device-independent color space to RGB, sRGB is assumed unless otherwise specified with the target_rgb keyword arg. """ print("=== RGB Example: Lab->RGB ===") # Instantiate an Lab color object with the given values. lab = LabColor(0.903, 16.296, -2.217) # Show a string representation. print(lab) # Convert to XYZ. rgb = convert_color(lab, sRGBColor) print(rgb) print("=== End Example ===\n") def example_rgb_to_xyz(): """ The reverse is similar. """ print("=== RGB Example: RGB->XYZ ===") # Instantiate an Lab color object with the given values. rgb = sRGBColor(120, 130, 140) # Show a string representation. print(rgb) # Convert RGB to XYZ using a D50 illuminant. xyz = convert_color(rgb, XYZColor, target_illuminant='D50') print(xyz) print("=== End Example ===\n") def example_spectral_to_xyz(): """ Instantiate an Lab color object with the given values. Note that the spectral range can run from 340nm to 830nm. Any omitted values assume a value of 0.0, which is more or less ignored. For the distribution below, we are providing an example reading from an X-Rite i1 Pro, which only measures between 380nm and 730nm. """ print("=== Example: Spectral->XYZ ===") spc = SpectralColor( observer='2', illuminant='d50', spec_380nm=0.0600, spec_390nm=0.0600, spec_400nm=0.0641, spec_410nm=0.0654, spec_420nm=0.0645, spec_430nm=0.0605, spec_440nm=0.0562, spec_450nm=0.0543, spec_460nm=0.0537, spec_470nm=0.0541, spec_480nm=0.0559, spec_490nm=0.0603, spec_500nm=0.0651, spec_510nm=0.0680, spec_520nm=0.0705, spec_530nm=0.0736, spec_540nm=0.0772, spec_550nm=0.0809, spec_560nm=0.0870, spec_570nm=0.0990, spec_580nm=0.1128, spec_590nm=0.1251, spec_600nm=0.1360, spec_610nm=0.1439, spec_620nm=0.1511, spec_630nm=0.1590, spec_640nm=0.1688, spec_650nm=0.1828, spec_660nm=0.1996, spec_670nm=0.2187, spec_680nm=0.2397, spec_690nm=0.2618, spec_700nm=0.2852, spec_710nm=0.2500, spec_720nm=0.2400, spec_730nm=0.2300) xyz = convert_color(spc, XYZColor) print(xyz) print("=== End Example ===\n") def example_lab_to_ipt(): """ This function shows a simple conversion of an XYZ color to an IPT color. """ print("=== Simple Example: XYZ->IPT ===") # Instantiate an XYZ color object with the given values. xyz = XYZColor(0.5, 0.5, 0.5, illuminant='d65') # Show a string representation. print(xyz) # Convert to IPT. ipt = convert_color(xyz, IPTColor) print(ipt) print("=== End Example ===\n") # Feel free to comment/un-comment examples as you please. example_lab_to_xyz() example_lchab_to_lchuv() example_lab_to_rgb() example_spectral_to_xyz() example_rgb_to_xyz() example_lab_to_ipt() colormath-3.0.0/examples/example_config.py0000644000175000017500000000044713162226447023402 0ustar greg.taylorgreg.taylor00000000000000""" This file holds various configuration options used for all of the examples. """ import os import sys # Use the colormath directory included in the downloaded package instead of # any globally installed versions. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))colormath-3.0.0/examples/delta_e.py0000644000175000017500000000232113162226447022010 0ustar greg.taylorgreg.taylor00000000000000""" This module shows some examples of Delta E calculations of varying types. """ # Does some sys.path manipulation so we can run examples in-place. # noinspection PyUnresolvedReferences import example_config from colormath.color_objects import LabColor from colormath.color_diff import delta_e_cie1976, delta_e_cie1994, \ delta_e_cie2000, delta_e_cmc # Reference color. color1 = LabColor(lab_l=0.9, lab_a=16.3, lab_b=-2.22) # Color to be compared to the reference. color2 = LabColor(lab_l=0.7, lab_a=14.2, lab_b=-1.80) print("== Delta E Colors ==") print(" COLOR1: %s" % color1) print(" COLOR2: %s" % color2) print("== Results ==") print(" CIE1976: %.3f" % delta_e_cie1976(color1, color2)) print(" CIE1994: %.3f (Graphic Arts)" % delta_e_cie1994(color1, color2)) # Different values for textiles. print(" CIE1994: %.3f (Textiles)" % delta_e_cie1994(color1, color2, K_1=0.048, K_2=0.014, K_L=2)) print(" CIE2000: %.3f" % delta_e_cie2000(color1, color2)) # Typically used for acceptability. print(" CMC: %.3f (2:1)" % delta_e_cmc(color1, color2, pl=2, pc=1)) # Typically used to more closely model human perception. print(" CMC: %.3f (1:1)" % delta_e_cmc(color1, color2, pl=1, pc=1)) colormath-3.0.0/examples/density.py0000644000175000017500000000434313162226447022100 0ustar greg.taylorgreg.taylor00000000000000""" This module shows you how to perform various kinds of density calculations. """ # Does some sys.path manipulation so we can run examples in-place. # noinspection PyUnresolvedReferences import example_config from colormath.color_objects import SpectralColor from colormath.density_standards import ANSI_STATUS_T_RED, ISO_VISUAL EXAMPLE_COLOR = SpectralColor( observer=2, illuminant='d50', spec_380nm=0.0600, spec_390nm=0.0600, spec_400nm=0.0641, spec_410nm=0.0654, spec_420nm=0.0645, spec_430nm=0.0605, spec_440nm=0.0562, spec_450nm=0.0543, spec_460nm=0.0537, spec_470nm=0.0541, spec_480nm=0.0559, spec_490nm=0.0603, spec_500nm=0.0651, spec_510nm=0.0680, spec_520nm=0.0705, spec_530nm=0.0736, spec_540nm=0.0772, spec_550nm=0.0809, spec_560nm=0.0870, spec_570nm=0.0990, spec_580nm=0.1128, spec_590nm=0.1251, spec_600nm=0.1360, spec_610nm=0.1439, spec_620nm=0.1511, spec_630nm=0.1590, spec_640nm=0.1688, spec_650nm=0.1828, spec_660nm=0.1996, spec_670nm=0.2187, spec_680nm=0.2397, spec_690nm=0.2618, spec_700nm=0.2852, spec_710nm=0.2500, spec_720nm=0.2400, spec_730nm=0.2300) def example_auto_status_t_density(): print("=== Example: Automatic Status T Density ===") # If no arguments are provided to calc_density(), ANSI Status T density is # assumed. The correct RGB "filter" is automatically selected for you. print("Density: %f" % EXAMPLE_COLOR.calc_density()) print("=== End Example ===\n") def example_manual_status_t_density(): print("=== Example: Manual Status T Density ===") # Here we are specifically requesting the value of the red band via the # ANSI Status T spec. print("Density: %f (Red)" % EXAMPLE_COLOR.calc_density( density_standard=ANSI_STATUS_T_RED)) print("=== End Example ===\n") def example_visual_density(): print("=== Example: Visual Density ===") # Here we pass the ISO Visual spectral standard. print("Density: %f" % EXAMPLE_COLOR.calc_density( density_standard=ISO_VISUAL)) print("=== End Example ===\n") # Feel free to comment/un-comment examples as you please. example_auto_status_t_density() example_manual_status_t_density() example_visual_density() colormath-3.0.0/examples/delta_e_matrix.py0000644000175000017500000000242113162226447023375 0ustar greg.taylorgreg.taylor00000000000000""" For a massive matrix of colors and color labels you can download the follow two files # http://lyst-classifiers.s3.amazonaws.com/color/lab-colors.pk # http://lyst-classifiers.s3.amazonaws.com/color/lab-matrix.pk lab-colors is a cPickled list of color names and lab-matrix is a cPickled (n,3) numpy array LAB values such that row q maps to index q in the lab color list """ import sys import csv import bz2 import numpy as np # Does some sys.path manipulation so we can run examples in-place. # noinspection PyUnresolvedReferences import example_config from colormath.color_diff_matrix import delta_e_cie2000 from colormath.color_objects import LabColor # load list of 1000 random colors from the XKCD color chart if sys.version_info >= (3, 0): reader = csv.DictReader(bz2.open('lab_matrix.csv.bz2', mode='rt')) lab_matrix = np.array([list(map(float, row.values())) for row in reader]) else: reader = csv.DictReader(bz2.BZ2File('lab_matrix.csv.bz2')) lab_matrix = np.array([map(float, row.values()) for row in reader]) color = LabColor(lab_l=69.34, lab_a=-0.88, lab_b=-52.57) lab_color_vector = np.array([color.lab_l, color.lab_a, color.lab_b]) delta = delta_e_cie2000(lab_color_vector, lab_matrix) print('%s is closest to %s' % (color, lab_matrix[np.argmin(delta)])) colormath-3.0.0/examples/color_appearance_model.py0000644000175000017500000000221413162226447025071 0ustar greg.taylorgreg.taylor00000000000000from colormath.color_appearance_models import CIECAM02 from colormath.color_objects import XYZColor # Color stimulus color = XYZColor(19.01, 20, 21.78) #Illuminant illuminant_d65 = XYZColor(95.05, 100, 108.88) # Background relative luminance y_b_dark = 10 y_b_light = 100 # Adapting luminance l_a = 328.31 # Surround condition assumed to be average (see CIECAM02 documentation for values) c = 0.69 n_c = 1 f = 1 model_a = CIECAM02(color.xyz_x, color.xyz_y, color.xyz_z, illuminant_d65.xyz_x, illuminant_d65.xyz_y, illuminant_d65.xyz_z, y_b_dark, l_a, c, n_c, f) model_b = CIECAM02(color.xyz_x, color.xyz_y, color.xyz_z, illuminant_d65.xyz_x, illuminant_d65.xyz_y, illuminant_d65.xyz_z, y_b_light, l_a, c, n_c, f) print('== CIECAM02 Predictions ==') print('Observed under CIE illuminant D65') print('Lightness {:.2f}, saturation {:.2f}, hue {:.2f}'.format(model_a.lightness, model_a.saturation, model_a.hue_angle)) print('Observed under CIE illuminant A') print('Lightness {:.2f}, saturation {:.2f}, hue {:.2f}'.format(model_b.lightness, model_b.saturation, model_b.hue_angle)) colormath-3.0.0/MANIFEST.in0000644000175000017500000000011513162226447017760 0ustar greg.taylorgreg.taylor00000000000000include LICENSE.txt include README.rst recursive-include examples *.txt *.py colormath-3.0.0/setup.py0000755000175000017500000000234113220777307017743 0ustar greg.taylorgreg.taylor00000000000000#!/usr/bin/env python import colormath from setuptools import setup LONG_DESCRIPTION = open('README.rst').read() CLASSIFIERS = [ 'Development Status :: 5 - Production/Stable', 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Natural Language :: English', 'Operating System :: OS Independent', 'Programming Language :: Python', 'Topic :: Scientific/Engineering :: Mathematics', 'Topic :: Software Development :: Libraries :: Python Modules', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', ] KEYWORDS = 'color math conversions' setup( name='colormath', version=colormath.VERSION, description='Color math and conversion library.', long_description=LONG_DESCRIPTION, author='Gregory Taylor', author_email='gtaylor@gc-taylor.com', url='https://github.com/gtaylor/python-colormath', download_url='http://pypi.python.org/pypi/colormath/', packages=['colormath'], platforms=['Platform Independent'], license='BSD', classifiers=CLASSIFIERS, keywords=KEYWORDS, install_requires=['numpy', 'networkx>=2.0'], )