o
    :ήc1                     @   s~   d Z ddlZddlmZ ddlmZmZ ddd	ZdddZ	dd
dddZ
dd
dddZdd
dddZd
dddZdS )a  
Functions for calculating the "distance" between colors.

Implicit in these definitions of "distance" is the notion of "Just Noticeable
Distance" (JND).  This represents the distance between colors where a human can
perceive different colors.  Humans are more sensitive to certain colors than
others, which different deltaE metrics correct for with varying degrees of
sophistication.

The literature often mentions 1 as the minimum distance for visual
differentiation, but more recent studies (Mahy 1994) peg JND at 2.3

The delta-E notation comes from the German word for "Sensation" (Empfindung).

Reference
---------
https://en.wikipedia.org/wiki/Color_difference

    N   )_supported_float_type   )lab2lch_cart2polar_2piTc                 C   sT   t | } t |}|rt| j|jg}nt j}| j|dd} |j|dd}| |fS )NF)copy)npasarrayr   dtypefloat64astype)lab1lab2allow_float32float_dtype r   </tmp/pip-target-vg8gfxp4/lib/python/skimage/color/delta_e.py_float_inputs   s   

r   c           	      C   sx   t | |dd\} }tj| |dddd \}}}tj||dddd \}}}t|| d || d  || d  S )a  Euclidean distance between two points in Lab color space

    Parameters
    ----------
    lab1 : array_like
        reference color (Lab colorspace)
    lab2 : array_like
        comparison color (Lab colorspace)
    channel_axis : int, optional
        This parameter indicates which axis of the arrays corresponds to
        channels.

        .. versionadded:: 0.19
           ``channel_axis`` was added in 0.19.

    Returns
    -------
    dE : array_like
        distance between colors `lab1` and `lab2`

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Color_difference
    .. [2] A. R. Robertson, "The CIE 1976 color-difference formulae,"
           Color Res. Appl. 2, 7-11 (1977).
    Tr   r   sourcedestinationN   r   )r   r   moveaxissqrt)	r   r   channel_axisL1a1b1L2a2b2r   r   r   deltaE_cie76'   s   *r#   
ףp=
?Q?r   c                C   s   t | |dd\} }tj| |dd} tj||dd}t| dddd \}}	t|dddd \}
}||
 }|	| }t| |dd}d}d||	  }d||	  }|||  d }||||  d 7 }|||| d  7 }tt|dS )	a  Color difference according to CIEDE 94 standard

    Accommodates perceptual non-uniformities through the use of application
    specific scale factors (`kH`, `kC`, `kL`, `k1`, and `k2`).

    Parameters
    ----------
    lab1 : array_like
        reference color (Lab colorspace)
    lab2 : array_like
        comparison color (Lab colorspace)
    kH : float, optional
        Hue scale
    kC : float, optional
        Chroma scale
    kL : float, optional
        Lightness scale
    k1 : float, optional
        first scale parameter
    k2 : float, optional
        second scale parameter
    channel_axis : int, optional
        This parameter indicates which axis of the arrays corresponds to
        channels.

        .. versionadded:: 0.19
           ``channel_axis`` was added in 0.19.

    Returns
    -------
    dE : array_like
        color difference between `lab1` and `lab2`

    Notes
    -----
    deltaE_ciede94 is not symmetric with respect to lab1 and lab2.  CIEDE94
    defines the scales for the lightness, hue, and chroma in terms of the first
    color.  Consequently, the first color should be regarded as the "reference"
    color.

    `kL`, `k1`, `k2` depend on the application and default to the values
    suggested for graphic arts

    ==========  ==============  ==========
    Parameter    Graphic Arts    Textiles
    ==========  ==============  ==========
    `kL`         1.000           2.000
    `k1`         0.045           0.048
    `k2`         0.015           0.014
    ==========  ==============  ==========

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Color_difference
    .. [2] http://www.brucelindbloom.com/index.html?Eqn_DeltaE_CIE94.html
    Tr   r   r   r&   Nr   r   )r   r   r   r   get_dH2r   maximum)r   r   kHkCkLk1k2r   r   C1r    C2dLdCdH2SLSCSHdE2r   r   r   deltaE_ciede94H   s   :r7   c          *      C   s  t | |dd\} }|| j }d}| jdkr:|jdkr:d}| jdkr)| dddf } |jdkr6|dddf }|d7 }tj| |dddd \}}}	tj||dddd \}
}}d	t||	t||  }|d
 }d	dt||d    }d| }t|| |	\}}t|| |\}}d	||
  }|d d }dd| td|   }|
| ||  }d	||  }dd|  }|| ||  }|| }|| }|| }| }||tjk  dtj 8  < ||tj k   dtj 7  < d||dk< dt| t	|d  }| } t
|dkt|tjk}!| |!|dtj k    dtj 7  < | |!|dtj k   dtj 8  < | |dk  d9  < | d	9 } ddt| td   dtd|    dtd|  td   dtd|  td   }"dd| |"  }#|||#  }$|d
 }dt||d   }%tdtt| d d d   }&t	d|&  |% | |$ }'|d }(|(|d 7 }(|(|$d 7 }(|(|'7 }(tt|(d})|r|)d })|)S )a  Color difference as given by the CIEDE 2000 standard.

    CIEDE 2000 is a major revision of CIDE94.  The perceptual calibration is
    largely based on experience with automotive paint on smooth surfaces.

    Parameters
    ----------
    lab1 : array_like
        reference color (Lab colorspace)
    lab2 : array_like
        comparison color (Lab colorspace)
    kL : float (range), optional
        lightness scale factor, 1 for "acceptably close"; 2 for "imperceptible"
        see deltaE_cmc
    kC : float (range), optional
        chroma scale factor, usually 1
    kH : float (range), optional
        hue scale factor, usually 1
    channel_axis : int, optional
        This parameter indicates which axis of the arrays corresponds to
        channels.

        .. versionadded:: 0.19
           ``channel_axis`` was added in 0.19.

    Returns
    -------
    deltaE : array_like
        The distance between `lab1` and `lab2`

    Notes
    -----
    CIEDE 2000 assumes parametric weighting factors for the lightness, chroma,
    and hue (`kL`, `kC`, `kH` respectively).  These default to 1.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Color_difference
    .. [2] http://www.ece.rochester.edu/~gsharma/ciede2000/ciede2000noteCRNA.pdf
           :DOI:`10.1364/AO.33.008069`
    .. [3] M. Melgosa, J. Quesada, and E. Hita, "Uniformity of some recent
           color metrics tested with an accurate color-difference tolerance
           dataset," Appl. Opt. 33, 8069-8077 (1994).
    Tr   Fr   Nr   r   r   g      ?   l   AW 2   r   r%      r$   g        g(\?   gQ?g{Gz?   皙?   ?   i     )r   ndimr   r   hypotr   r   r   pisinlogical_andabscosdeg2radexprad2degr(   )*r   r   r+   r*   r)   r   unrollr   r   r   r    r!   r"   Cbarc7Gscaler.   h1r/   h2Lbartmpr3   L_termr4   C_termh_diffh_sumCCdHdH_termHbarmaskTr5   H_termRcdthetaR_termr6   ansr   r   r   deltaE_ciede2000   s|   -



$$(rc   c                C   s  t | |dd\} }tj| |dd} tj||dd}t| dddd \}}}t|dddd \}}	}
||	 }|| }t| |dd}ttt|dkt|d	kd
dtt	|t
d   ddtt	|t
d   }|d }t||d  }t|dk dd| dd|   }dd| dd|    }||| d |  }|||  d }||||  d 7 }|||d  7 }tt|dS )a  Color difference from the  CMC l:c standard.

    This color difference was developed by the Colour Measurement Committee
    (CMC) of the Society of Dyers and Colourists (United Kingdom). It is
    intended for use in the textile industry.

    The scale factors `kL`, `kC` set the weight given to differences in
    lightness and chroma relative to differences in hue.  The usual values are
    ``kL=2``, ``kC=1`` for "acceptability" and ``kL=1``, ``kC=1`` for
    "imperceptibility".  Colors with ``dE > 1`` are "different" for the given
    scale factors.

    Parameters
    ----------
    lab1 : array_like
        reference color (Lab colorspace)
    lab2 : array_like
        comparison color (Lab colorspace)
    channel_axis : int, optional
        This parameter indicates which axis of the arrays corresponds to
        channels.

        .. versionadded:: 0.19
           ``channel_axis`` was added in 0.19.

    Returns
    -------
    dE : array_like
        distance between colors `lab1` and `lab2`

    Notes
    -----
    deltaE_cmc the defines the scales for the lightness, hue, and chroma
    in terms of the first color.  Consequently
    ``deltaE_cmc(lab1, lab2) != deltaE_cmc(lab2, lab1)``

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Color_difference
    .. [2] http://www.brucelindbloom.com/index.html?Eqn_DeltaE_CIE94.html
    .. [3] F. J. J. Clarke, R. McDonald, and B. Rigg, "Modification to the
           JPC79 colour-difference formula," J. Soc. Dyers Colour. 100, 128-132
           (1984).
    Tr   r   r   r&   Nr      iY  gQ?r=      g
ףp=
?g?#   r>   il     g'1Z?gB>٬?g      ?gPs?g"~j?g|a2U?g<,Ԋ?r   r   )r   r   r   r   r'   whererE   rJ   rF   rG   rH   r   r(   )r   r   r+   r*   r   r   r.   rP   r    r/   rQ   r1   r0   r2   r]   c1_4Fr3   r4   r5   r6   r   r   r   
deltaE_cmc  s*   -"  "rk   c                C   s   t | |gtjk}t| |dd\} }tj| |dddd \}}tj||dddd \}}t||}t||}	||	 || ||   }
d|
 }|rR|tj}|S )a  squared hue difference term occurring in deltaE_cmc and deltaE_ciede94

    Despite its name, "dH" is not a simple difference of hue values.  We avoid
    working directly with the hue value, since differencing angles is
    troublesome.  The hue term is usually written as:
        c1 = sqrt(a1**2 + b1**2)
        c2 = sqrt(a2**2 + b2**2)
        term = (a1-a2)**2 + (b1-b2)**2 - (c1-c2)**2
        dH = sqrt(term)

    However, this has poor roundoff properties when a or b is dominant.
    Instead, ab is a vector with elements a and b.  The same dH term can be
    re-written as:
        |ab1-ab2|**2 - (|ab1| - |ab2|)**2
    and then simplified to:
        2*|ab1|*|ab2| - 2*dot(ab1, ab2)
    Fr   r   r   r   r   r   )r   r   float32r   r   rB   r   )r   r   r   input_is_float_32r   r   r!   r"   r.   r/   termoutr   r   r   r'   f  s   r'   )T)r   )r   r   r   r$   r%   )r   r   r   )r   r   )__doc__numpyr   _shared.utilsr   	colorconvr   r   r   r#   r7   rc   rk   r'   r   r   r   r   <module>   s    

!O I