B
    |b_                 @   s   d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZmZmZ d	ZG d
d deZdd ZdS )zW:mod:`wand.color` --- Colors
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 0.1.2

    N   )library)MagickPixelPacket	PixelInfo)binarytext)Resource)MAGICK_VERSION_NUMBERMAGICK_HDRIQUANTUM_DEPTH)Colorscale_quantum_to_int8c               @   sj  e Zd ZdZdZejZejZ	ej
ZejZdZdvddZdd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zedd Zedwd!d"Zed#d$ Z e!d%d& Z"e"j#d'd& Z"e!d(d) Z$e$j#d*d) Z$e!d+d, Z%e%j#d-d, Z%e!d.d/ Z&e&j#d0d/ Z&e!d1d2 Z'e'j#d3d2 Z'e!d4d5 Z(e(j#d6d5 Z(e!d7d8 Z)e)j#d9d8 Z)e!d:d; Z*e*j#d<d; Z*e!d=d> Z+e+j#d?d> Z+e!d@dA Z,e,j#dBdA Z,e!dCdD Z-e-j#dEdD Z-e!dFdG Z.e.j#dHdG Z.e!dIdJ Z/e/j#dKdJ Z/e!dLdM Z0e0j#dNdM Z0e!dOdP Z1e1j#dQdP Z1e!dRdS Z2e2j#dTdS Z2e!dUdV Z3e3j#dWdV Z3e!dXdY Z4e4j#dZdY Z4e!d[d\ Z5e5j#d]d\ Z5e!d^d_ Z6e!d`da Z7e7j#dbda Z7e!dcdd Z8e8j#dedd Z8e!dfdg Z9e9j#dhdg Z9e!didj Z:e!dkdl Z;e;j#dmdl Z;e!dndo Z<e<j#dpdo Z<e!dqdr Z=e=j#dsdr Z=dtdu Z>dS )xr   a_  Color value.

    Unlike any other objects in Wand, its resource management can be
    implicit when it used outside of :keyword:`with` block. In these case,
    its resource are allocated for every operation which requires a resource
    and destroyed immediately. Of course it is inefficient when the
    operations are much, so to avoid it, you should use color objects
    inside of :keyword:`with` block explicitly e.g.::

        red_count = 0
        with Color('#f00') as red:
            with Image(filename='image.png') as img:
                for row in img:
                    for col in row:
                        if col == red:
                            red_count += 1

    :param string: a color name string e.g. ``'rgb(255, 255, 255)'``,
                   ``'#fff'``, ``'white'``. see `ImageMagick Color Names`_
                   doc also
    :type string: :class:`basestring`

    .. versionchanged:: 0.3.0
       :class:`Color` objects become hashable.

    .. versionchanged:: 0.5.1
       Color channel properties can now be set.

    .. versionchanged:: 0.5.1
       Added :attr:`cyan`, :attr:`magenta`, :attr:`yellow`, & :attr:`black`
       properties for CMYK :class:`Color` instances.

    .. versionchanged:: 0.5.1
       Method :meth:`Color.from_hsl()` can create a RGB color from ``hue``,
       ``saturation``, & ``lightness`` values.

    .. seealso::

       `ImageMagick Color Names`_
          The color can then be given as a color name (there is a limited
          but large set of these; see below) or it can be given as a set
          of numbers (in decimal or hexadecimal), each corresponding to
          a channel in an RGB or RGBA color model. HSL, HSLA, HSB, HSBA,
          CMYK, or CMYKA color models may also be specified. These topics
          are briefly described in the sections below.

    .. _ImageMagick Color Names: http://www.imagemagick.org/script/color.php

    .. describe:: == (other)

       Equality operator.

       :param other: a color another one
       :type color: :class:`Color`
       :returns: ``True`` only if two images equal.
       :rtype: :class:`bool`

    N)rawZ
c_resource	allocatedc          	   C   s   |d kr|d ks |d k	r(|d k	r(t dtdk| _d| _d| _|d kr| jr`ttt| _	nttt
| _	| F t| jt|}|s|   d|}t|t| j| j	 W d Q R X n|| _	d S )Nzexpected one argumenti   Fr   zUnrecognized color string "{0}")	TypeErrorr	   	use_pixeldirtyr   ctypescreate_string_buffersizeofr   r   r   r   ZPixelSetColorresourcer   Zraise_exceptionformat
ValueErrorPixelGetMagickColor)selfstringr   okmsg r   M/home/ankuromar296_gmail_com/.local/lib/python3.7/site-packages/wand/color.py__init__Y   s(    

zColor.__init__c             C   s
   | j d fS )N)r   )r   r   r   r   __getinitargs__z   s    zColor.__getinitargs__c          	   C   sp   | j dk rX|  8 t | _| jr6t| j| j nt| j| j W d Q R X d| _ n|  j d7  _ t	
| S )Nr   )r   allocater   ZNewPixelWandr   r   ZPixelSetPixelColorr   ZPixelSetMagickColorr   	__enter__)r   r   r   r   r#   }   s    


zColor.__enter__c             C   sH   |  j d8  _ | jr*t| j| j d| _| j dk rDt| ||| d S )Nr   F)r   r   r   r   r   r   r   __exit__)r   typevalue	tracebackr   r   r   r$      s    
zColor.__exit__c          
   C   s>   t |tsdS | "}| | |j|jS Q R X W d Q R X d S )NF)
isinstancer   c_equalsr   )r   otherthisr   r   r   __eq__   s
    
zColor.__eq__c             C   s
   | |k S )Nr   )r   r*   r   r   r   __ne__   s    zColor.__ne__c             C   s   | j rt| jS td S )N)alphahashnormalized_string)r   r   r   r   __hash__   s    
zColor.__hash__c             C   s   | j S )N)r   )r   r   r   r   __str__   s    zColor.__str__c             C   s   t | }d|j|j| jS )Nz{0}.{1}({2!r}))r%   r   
__module____name__r   )r   cr   r   r   __repr__   s    zColor.__repr__c             C   s@   t |tjstdt| |dk s,|dkr<tdt| dS )aq  Ensure the given ``subject`` is a float type, and value between
        0.0 & 1.0.

        :param subject: value to assert as a valid double.
        :type subject: :class:`numbers.Real`
        :raises ValueError: if the subject is not between 0.0 and 1.0
        :raises TypeError: if the subject is not a float-point number.

        ..versionadded:: 0.5.1
        z)Expecting a float-point real number, not g        g      ?z.Expecting a real number between 0.0 & 1.0, notN)r(   numbersRealr   reprr   )r   subjectr   r   r   _assert_double   s    zColor._assert_doublec             C   s@   t |tjstdt| |dk s,|dkr<tdt| dS )ap  Ensure the given ``subject`` is a integer type, and value between
        0 & 255.

        :param subject: value to assert as a valid number.
        :type subject: :class:`numbers.Integral`
        :raises ValueError: if the subject is not between 0 and 255
        :raises TypeError: if the subject is not a Integral number.

        ..versionadded:: 0.5.1
        z!Expecting an integer number, not r      z,Expecting a real number between 0 & 255, notN)r(   r7   Integralr   r9   r   )r   r:   r   r   r   _assert_int8   s    zColor._assert_int8c             C   s^   ddddd}t |tjs*tdt| |dk s>||t krZd}t||t t|d	S )
a  Ensure the given ``subject`` is a number, and value between
        0.0 & QuantumRange.

        The QuantumRange is the max value based on the QuantumDepth of the
        ImageMagick library (i.e. Q16).

        :param subject: value to assert as a valid double.
        :type subject: :class:`numbers.Number`
        :raises ValueError: if the subject is not between 0 and QuantumRange
        :raises TypeError: if the subject is not a number.

        ..versionadded:: 0.5.1
        g     o@g    @g  Ag      C)          @   zExpecting a number, not g        z+Expecting a number between 0 & {0}, not {1}N)r(   r7   Numberr   r9   r   r   r   )r   r:   Zquantum_rangemessager   r   r   _assert_quantum   s    zColor._assert_quantumc             C   s   d}|j | j| j| jdS )Na	  
        <span style="background-color:#{red:02X}{green:02X}{blue:02X};
                     display:inline-block;
                     line-height:1em;
                     width:1em;">&nbsp;</span>
        <strong>#{red:02X}{green:02X}{blue:02X}</strong>
        )redgreenblue)r   red_int8
green_int8	blue_int8)r   htmlr   r   r   _repr_html_   s    zColor._repr_html_c             C   s(   t j}tt | |do$|| ||kS )a  Raw level version of equality test function for two pixels.

        :param a: a pointer to PixelWand to compare
        :type a: :class:`ctypes.c_void_p`
        :param b: a pointer to PixelWand to compare
        :type b: :class:`ctypes.c_void_p`
        :returns: ``True`` only if two pixels equal
        :rtype: :class:`bool`

        .. note::

           It's only for internal use. Don't use it directly.
           Use ``==`` operator of :class:`Color` instead.

        r   )r   PixelGetAlphaboolZIsPixelWandSimilar)abr.   r   r   r   r)      s    zColor.c_equals        c          	   C   sR   | d}| | | | | | d|_| t|j||| W dQ R X |S )a  Creates a RGB color from HSL values. The ``hue``, ``saturation``,
        and ``lightness`` must be normalized between 0.0 & 1.0.

        .. code::

            h=0.75  # 270 Degrees
            s=1.0   # 100 Percent
            l=0.5   # 50 Percent
            with Color.from_hsl(hue=h, saturation=s, lightness=l) as color:
                print(color)  #=> srgb(128,0,255)

        :param hue: a normalized double between 0.0 & 1.0.
        :type hue: :class:`numbers.Real`
        :param saturation: a normalized double between 0.0 & 1.0.
        :type saturation: :class:`numbers.Real`
        :param lightness: a normalized double between 0.0 & 1.0.
        :type lightness: :class:`numbers.Real`
        :rtype: :class:`Color`

        .. versionadded:: 0.5.1
        ZWHITETN)r;   r   r   ZPixelSetHSLr   )clshue
saturation	lightnesscolorr   r   r   from_hsl
  s    


zColor.from_hslc             C   sD   |st tdk rt}nt}t|}t|}t|| | |dS )Ni   )r   )	AssertionErrorr	   r   r   r   r   r   r   r   )rS   Z	pixelwandZpixel_structuresizeZ
raw_bufferr   r   r   from_pixelwand*  s    

zColor.from_pixelwandc          	   C   s   |  t | jS Q R X dS )z5(:class:`numbers.Real`) Alpha value, from 0.0 to 1.0.N)r   rN   r   )r   r   r   r   r.   6  s    zColor.alphac          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)r;   r   r   ZPixelSetAlphar   )r   r&   r   r   r   r.   <  s    
c             C   s   t dtdtd| j S )z(:class:`numbers.Integral`) Alpha value as 8bit integer which is
        a common style.  From 0 to 255.

        .. versionadded:: 0.3.0

        r   r<   g     o@)maxminintr.   )r   r   r   r   
alpha_int8C  s    zColor.alpha_int8c             C   s   |  | t|d | _d S )Ng     o@)r>   floatr.   )r   r&   r   r   r   r_   M  s    
c          	   C   s   |  t | jS Q R X dS )z(:class:`numbers.Integral`) Alpha value.
        Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.

        .. versionadded:: 0.3.0

        N)r   ZPixelGetAlphaQuantumr   )r   r   r   r   alpha_quantumR  s    zColor.alpha_quantumc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)rE   r   r   ZPixelSetAlphaQuantumr   )r   r&   r   r   r   ra   ]  s    
c          	   C   s   |  t | jS Q R X dS )z(:class:`numbers.Real`) Black, or ``'K'``, color channel in CMYK
        colorspace. Unused by RGB colorspace.

        .. versionadded:: 0.5.1
        N)r   ZPixelGetBlackr   )r   r   r   r   blackd  s    zColor.blackc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)r;   r   r   ZPixelSetBlackr   )r   r&   r   r   r   rb   n  s    
c             C   s   t dtdtd| j S )z(:class:`numbers.Integral`) Black value as 8bit integer which is
        a common style.  From 0 to 255.

        .. versionadded:: 0.5.1
        r   r<   g     o@)r\   r]   r^   rb   )r   r   r   r   
black_int8u  s    zColor.black_int8c             C   s   |  | t|d | _d S )Ng     o@)r>   r`   rb   )r   r&   r   r   r   rc   ~  s    
c          	   C   s   |  t | jS Q R X dS )z(:class:`numbers.Integral`) Black.
        Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.

        .. versionadded:: 0.5.1
        N)r   ZPixelGetBlackQuantumr   )r   r   r   r   black_quantum  s    zColor.black_quantumc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)rE   r   r   ZPixelSetBlackQuantumr   )r   r&   r   r   r   rd     s    
c          	   C   s   |  t | jS Q R X dS )z.(:class:`numbers.Real`) Blue, from 0.0 to 1.0.N)r   ZPixelGetBluer   )r   r   r   r   rH     s    z
Color.bluec          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)r;   r   r   ZPixelSetBluer   )r   r&   r   r   r   rH     s    
c             C   s   t dtdtd| j S )z(:class:`numbers.Integral`) Blue as 8bit integer which is
        a common style.  From 0 to 255.

        .. versionadded:: 0.3.0

        r   r<   g     o@)r\   r]   r^   rH   )r   r   r   r   rK     s    zColor.blue_int8c             C   s   |  | t|d | _d S )Ng     o@)r>   r`   rH   )r   r&   r   r   r   rK     s    
c          	   C   s   |  t | jS Q R X dS )z(:class:`numbers.Integral`) Blue.
        Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.

        .. versionadded:: 0.3.0

        N)r   ZPixelGetBlueQuantumr   )r   r   r   r   blue_quantum  s    zColor.blue_quantumc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)rE   r   r   ZPixelSetBlueQuantumr   )r   r&   r   r   r   re     s    
c          	   C   s   |  t | jS Q R X dS )z(:class:`numbers.Real`) Cyan color channel in CMYK
        colorspace. Unused by RGB colorspace.

        .. versionadded:: 0.5.1
        N)r   ZPixelGetCyanr   )r   r   r   r   cyan  s    z
Color.cyanc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)r;   r   r   ZPixelSetCyanr   )r   r&   r   r   r   rf     s    
c             C   s   t dtdtd| j S )z(:class:`numbers.Integral`) Cyan value as 8bit integer which is
        a common style.  From 0 to 255.

        .. versionadded:: 0.5.1
        r   r<   g     o@)r\   r]   r^   rf   )r   r   r   r   	cyan_int8  s    zColor.cyan_int8c             C   s   |  | t|d | _d S )Ng     o@)r>   r`   rf   )r   r&   r   r   r   rg     s    
c          	   C   s   |  t | jS Q R X dS )z(:class:`numbers.Integral`) Cyan.
        Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.

        .. versionadded:: 0.5.1
        N)r   ZPixelGetCyanQuantumr   )r   r   r   r   cyan_quantum  s    zColor.cyan_quantumc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)rE   r   r   ZPixelSetCyanQuantumr   )r   r&   r   r   r   rh     s    
c          	   C   s   |  t | jS Q R X d S )N)r   ZPixelGetFuzzr   )r   r   r   r   fuzz  s    z
Color.fuzzc          	   C   sD   t |tjstdt| d| _|  t| j| W d Q R X d S )Nz)Expecting a float-point real number, not T)	r(   r7   r8   r   r9   r   r   ZPixelSetFuzzr   )r   r&   r   r   r   ri     s    c          	   C   s   |  t | jS Q R X dS )z/(:class:`numbers.Real`) Green, from 0.0 to 1.0.N)r   ZPixelGetGreenr   )r   r   r   r   rG      s    zColor.greenc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)r;   r   r   ZPixelSetGreenr   )r   r&   r   r   r   rG     s    
c             C   s   t dtdtd| j S )z(:class:`numbers.Integral`) Green as 8bit integer which is
        a common style.  From 0 to 255.

        .. versionadded:: 0.3.0

        r   r<   g     o@)r\   r]   r^   rG   )r   r   r   r   rJ     s    zColor.green_int8c             C   s   |  | t|d | _d S )Ng     o@)r>   r`   rG   )r   r&   r   r   r   rJ     s    
c          	   C   s   |  t | jS Q R X dS )z(:class:`numbers.Integral`) Green.
        Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.

        .. versionadded:: 0.3.0

        N)r   ZPixelGetGreenQuantumr   )r   r   r   r   green_quantum  s    zColor.green_quantumc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)rE   r   r   ZPixelSetGreenQuantumr   )r   r&   r   r   r   rj   '  s    
c          	   C   s   |  t | jS Q R X dS )z(:class:`numbers.Real`) Magenta color channel in CMYK
        colorspace. Unused by RGB colorspace.

        .. versionadded:: 0.5.1
        N)r   ZPixelGetMagentar   )r   r   r   r   magenta.  s    zColor.magentac          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)r;   r   r   ZPixelSetMagentar   )r   r&   r   r   r   rk   8  s    
c             C   s   t dtdtd| j S )z(:class:`numbers.Integral`) Magenta value as 8bit integer which is
        a common style.  From 0 to 255.

        .. versionadded:: 0.5.1
        r   r<   g     o@)r\   r]   r^   rk   )r   r   r   r   magenta_int8?  s    zColor.magenta_int8c             C   s   |  | t|d | _d S )Ng     o@)r>   r`   rk   )r   r&   r   r   r   rl   H  s    
c          	   C   s   |  t | jS Q R X d S )N)r   ZPixelGetMagentaQuantumr   )r   r   r   r   magenta_quantumM  s    zColor.magenta_quantumc          	   C   s2   |  | d| _|  t| j| W dQ R X dS )z(:class:`numbers.Integral`) Magenta.
        Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.

        .. versionadded:: 0.5.1
        TN)rE   r   r   ZPixelSetMagentaQuantumr   )r   r&   r   r   r   rm   R  s    
c          	   C   s@   | 2 d}t | j}|r2tt|}t |}|S Q R X dS )z(:class:`basestring`) The normalized string representation of
        the color.  The same color is always represented to the same
        string.

        .. versionadded:: 0.3.0

        N)r   ZPixelGetColorAsNormalizedStringr   r   r   	string_atMagickRelinquishMemory)r   r   ptrr   r   r   r0   ^  s    	
zColor.normalized_stringc          	   C   s   |  t | jS Q R X dS )z-(:class:`numbers.Real`) Red, from 0.0 to 1.0.N)r   ZPixelGetRedr   )r   r   r   r   rF   o  s    z	Color.redc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)r;   r   r   ZPixelSetRedr   )r   r&   r   r   r   rF   u  s    
c             C   s   t dtdtd| j S )z(:class:`numbers.Integral`) Red as 8bit integer which is a common
        style.  From 0 to 255.

        .. versionadded:: 0.3.0

        r   r<   g     o@)r\   r]   r^   rF   )r   r   r   r   rI   |  s    zColor.red_int8c             C   s   |  | t|d | _d S )Ng     o@)r>   r`   rF   )r   r&   r   r   r   rI     s    
c          	   C   s   |  t | jS Q R X dS )z(:class:`numbers.Integral`) Red.
        Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.

        .. versionadded:: 0.3.0

        N)r   ZPixelGetRedQuantumr   )r   r   r   r   red_quantum  s    zColor.red_quantumc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)rE   r   r   ZPixelSetRedQuantumr   )r   r&   r   r   r   rq     s    
c          	   C   s@   | 2 d}t | j}|r2tt|}t |}|S Q R X dS )z=(:class:`basestring`) The string representation of the color.N)r   ZPixelGetColorAsStringr   r   r   rn   ro   )r   Zcolor_stringrp   r   r   r   r     s    
zColor.stringc          	   C   s   |  t | jS Q R X dS )z(:class:`numbers.Real`) Yellow color channel in CMYK
        colorspace. Unused by RGB colorspace.

        .. versionadded:: 0.5.1
        N)r   ZPixelGetYellowr   )r   r   r   r   yellow  s    zColor.yellowc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)r;   r   r   ZPixelSetYellowr   )r   r&   r   r   r   rr     s    
c             C   s   t dtdtd| j S )z(:class:`numbers.Integral`) Yellow as 8bit integer which is a common
        style. From 0 to 255.

        .. versionadded:: 0.5.1
        r   r<   g     o@)r\   r]   r^   rr   )r   r   r   r   yellow_int8  s    zColor.yellow_int8c             C   s   |  | t|d | _d S )Ng     o@)r>   r`   rr   )r   r&   r   r   r   rs     s    
c          	   C   s   |  t | jS Q R X dS )z(:class:`numbers.Integral`) Yellow.
        Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.

        .. versionadded:: 0.5.1
        N)r   ZPixelGetYellowQuantumr   )r   r   r   r   yellow_quantum  s    zColor.yellow_quantumc          	   C   s2   |  | d| _|  t| j| W d Q R X d S )NT)rE   r   r   ZPixelSetYellowQuantumr   )r   r&   r   r   r   rt     s    
c          	   C   sb   t d}t d}t d}| * t| jt |t |t | W dQ R X |j|j|jfS )a&  Calculate the HSL color values from the RGB color.

        :returns: Tuple containing three normalized doubles, between 0.0 &
                  1.0, representing ``hue``, ``saturation``, and ``lightness``.
        :rtype: :class:`collections.Sequence`

        .. versionadded:: 0.5.1
        g        N)r   c_doubler   ZPixelGetHSLr   byrefr&   )r   rT   rU   rV   r   r   r   hsl  s    	


z	Color.hsl)NN)rR   rR   rR   )?r4   r3   __qualname____doc__r   r   ZIsPixelWandZc_is_resourceZDestroyPixelWandZc_destroy_resourceZPixelGetExceptionZc_get_exceptionZPixelClearExceptionZc_clear_exception	__slots__r    r!   r#   r$   r,   r-   r1   r2   r6   r;   r>   rE   rM   staticmethodr)   classmethodrX   r[   propertyr.   setterr_   ra   rb   rc   rd   rH   rK   re   rf   rg   rh   ri   rG   rJ   rj   rk   rl   rm   r0   rF   rI   rq   r   rr   rs   rt   rw   r   r   r   r   r      s   :
!

	


	
	

	

	
r   c             C   s   | dkrdS ddddd}t rtdkr4| |t  }qtdkr^t| d	 t| d	 d?  d? }qtd
krx| d |t  }qtdkr| |t  }n| |t  }|dkrdS t|d S )aq  Straightforward port of :c:func:`ScaleQuantumToChar()` inline
    function.

    .. deprecated:: 0.6.6

    :param quantum: quantum value
    :type quantum: :class:`numbers.Integral`
    :returns: 8bit integer of the given ``quantum`` value
    :rtype: :class:`numbers.Integral`

    .. versionadded:: 0.3.0
    .. versionchanged:: 0.5.0
        Added HDRI support
    r   r   g     p@g   pAgpC)r?   r@   rA   rB   r?   r@      rA   i rB   r<   g      ?)r
   r   r^   )Zquantumtablevr   r   r   r     s     "r   )ry   r   r7   apir   Zcdefs.structuresr   r   compatr   r   r   r   versionr	   r
   r   __all__r   r   r   r   r   r   <module>   s        ^