# std imports from math import cos, exp, sin, sqrt, atan2
# isort: off try: from functools import lru_cache except ImportError: # lru_cache was added in Python 3.2 from backports.functools_lru_cache import lru_cache
def rgb_to_xyz(red, green, blue): """
Convert standard RGB color to XYZ color.
:arg int red: RGB value of Red.
:arg int green: RGB value of Green.
:arg int blue: RGB value of Blue.
:returns: Tuple (X, Y, Z) representing XYZ color
:rtype: tuple
D65/2° standard illuminant """
rgb = [] for val in red, green, blue:
val /= 255.0 if val > 0.04045:
val = pow((val + 0.055) / 1.055, 2.4) else:
val /= 12.92
val *= 100
rgb.append(val)
red, green, blue = rgb # pylint: disable=unbalanced-tuple-unpacking
x_val = red * 0.4124 + green * 0.3576 + blue * 0.1805
y_val = red * 0.2126 + green * 0.7152 + blue * 0.0722
z_val = red * 0.0193 + green * 0.1192 + blue * 0.9505
return x_val, y_val, z_val
def xyz_to_lab(x_val, y_val, z_val): """
Convert XYZ color to CIE-Lab color.
:arg float x_val: XYZ value of X.
:arg float y_val: XYZ value of Y.
:arg float z_val: XYZ value of Z.
:returns: Tuple (L, a, b) representing CIE-Lab color
:rtype: tuple
D65/2° standard illuminant """
xyz = [] for val, ref in (x_val, 95.047), (y_val, 100.0), (z_val, 108.883):
val /= ref
val = pow(val, 1 / 3.0) if val > 0.008856 else 7.787 * val + 16 / 116.0
xyz.append(val)
@lru_cache(maxsize=256) def rgb_to_lab(red, green, blue): """
Convert RGB color to CIE-Lab color.
:arg int red: RGB value of Red.
:arg int green: RGB value of Green.
:arg int blue: RGB value of Blue.
:returns: Tuple (L, a, b) representing CIE-Lab color
:rtype: tuple
D65/2° standard illuminant """ return xyz_to_lab(*rgb_to_xyz(red, green, blue))
def dist_rgb(rgb1, rgb2): """
Determine distance between two rgb colors.
:arg tuple rgb1: RGB color definition
:arg tuple rgb2: RGB color definition
:returns: Square of the distance between provided colors
:rtype: float
This works by treating RGB colors as coordinates in three dimensional
space and finding the closest point within the configured color range
using the formula::
d^2 = (r2 - r1)^2 + (g2 - g1)^2 + (b2 - b1)^2
For efficiency, the square of the distance is returned
which is sufficient for comparisons """ return sum(pow(rgb1[idx] - rgb2[idx], 2) for idx in (0, 1, 2))
def dist_rgb_weighted(rgb1, rgb2): """
Determine the weighted distance between two rgb colors.
:arg tuple rgb1: RGB color definition
:arg tuple rgb2: RGB color definition
:returns: Square of the distance between provided colors
:rtype: float
Similar to a standard distance formula, the values are weighted
to approximate human perception of color differences
For efficiency, the square of the distance is returned
which is sufficient for comparisons """
red_mean = (rgb1[0] + rgb2[0]) / 2.0
def dist_cie76(rgb1, rgb2): """
Determine distance between two rgb colors using the CIE94 algorithm.
:arg tuple rgb1: RGB color definition
:arg tuple rgb2: RGB color definition
:returns: Square of the distance between provided colors
:rtype: float
For efficiency, the square of the distance is returned
which is sufficient for comparisons """
l_1, a_1, b_1 = rgb_to_lab(*rgb1)
l_2, a_2, b_2 = rgb_to_lab(*rgb2) return pow(l_1 - l_2, 2) + pow(a_1 - a_2, 2) + pow(b_1 - b_2, 2)
def dist_cie94(rgb1, rgb2): # pylint: disable=too-many-locals """
Determine distance between two rgb colors using the CIE94 algorithm.
:arg tuple rgb1: RGB color definition
:arg tuple rgb2: RGB color definition
:returns: Square of the distance between provided colors
:rtype: float
For efficiency, the square of the distance is returned
which is sufficient for comparisons """
l_1, a_1, b_1 = rgb_to_lab(*rgb1)
l_2, a_2, b_2 = rgb_to_lab(*rgb2)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.