a
    ¥Q•hä  ã                   @   sz   d dl Z ddlmZmZmZmZ G dd„ dƒZdd„ Zdd	„ Zd
d„ Z	ddd„Z
ddd„Zddd„Zddd„Zdd„ ZdS )é    Né   )ÚGimpGradientFileÚGimpPaletteFileÚ
ImageColorÚPaletteFilec                   @   s|   e Zd ZdZddd„Zedd„ ƒZejdd„ ƒZed	d
„ ƒZejdd
„ ƒZdd„ Z	dd„ Z
dd„ ZeZddd„Zdd„ ZdS )ÚImagePalettea¥  
    Color palette for palette mapped images

    :param mode: The mode to use for the palette. See:
        :ref:`concept-modes`. Defaults to "RGB"
    :param palette: An optional palette. If given, it must be a bytearray,
        an array or a list of ints between 0-255. The list must consist of
        all channels for one color followed by the next color (e.g. RGBRGBRGB).
        Defaults to an empty palette.
    ÚRGBNc                 C   s"   || _ d | _|ptƒ | _d | _d S ©N)ÚmodeÚrawmodeÚ	bytearrayÚpaletteÚdirty)Úselfr
   r   © r   úI/var/www/sistema_ama/venv/lib/python3.9/site-packages/PIL/ImagePalette.pyÚ__init__$   s    zImagePalette.__init__c                 C   s   | j S r	   )Ú_palette©r   r   r   r   r   *   s    zImagePalette.palettec                 C   s   d | _ || _d S r	   )Ú_colorsr   )r   r   r   r   r   r   .   s    c                 C   sh   | j d u rbt| jƒ}i | _ tdt| jƒ|ƒD ]4}t| j||| … ƒ}|| j v rRq,|| | j |< q,| j S )Nr   )r   Úlenr
   Úranger   Útuple)r   Zmode_lenÚiÚcolorr   r   r   Úcolors3   s    


zImagePalette.colorsc                 C   s
   || _ d S r	   )r   )r   r   r   r   r   r   ?   s    c                 C   s<   t ƒ }| j|_| j|_| jd ur0| jd d … |_| j|_|S r	   )r   r
   r   r   r   )r   Únewr   r   r   ÚcopyC   s    
zImagePalette.copyc                 C   s    | j r| j | jfS | j|  ¡ fS )z£
        Get palette contents in format suitable for the low-level
        ``im.putpalette`` primitive.

        .. warning:: This method is experimental.
        )r   r   r
   Útobytesr   r   r   r   ÚgetdataN   s    zImagePalette.getdatac                 C   s:   | j rd}t|ƒ‚t| jtƒr$| jS t d| j¡}| ¡ S )zUConvert palette to bytes.

        .. warning:: This method is experimental.
        ú!palette contains raw palette dataÚB)r   Ú
ValueErrorÚ
isinstancer   ÚbytesÚarrayr   )r   ÚmsgÚarrr   r   r   r   Y   s    zImagePalette.tobytesc           	   
   C   sö  | j rd}t|ƒ‚t|tƒ�rÜ| jdkrZt|ƒdkrx|d dkrLd}t|ƒ‚|dd… }n| jdkrxt|ƒdkrx|d	7 }z| j| W S  t�yØ } �z8t| jt	ƒs°t	| jƒ| _
t| jƒd }d
}|rÞ|j d¡|j d¡f}||v rð|d7 }qÞ|dk�rV|�r>ttt| ¡ ƒƒƒD ](\}}|dk�r||v�r|} �q>�q|dk�rVd}t|ƒ|‚|| j|< |d t| jƒk �r¨| jd|d … t|ƒ | j|d d d…  | _
n|  j
t|ƒ7  _
d| _|W  Y d}~S d}~0 0 ndt|ƒ› �}t|ƒ‚dS )zgGiven an rgb tuple, allocate palette entry.

        .. warning:: This method is experimental.
        r    r   é   é   éÿ   z/cannot add non-opaque RGBA color to RGB paletteNÚRGBA)r*   r   Ú
backgroundÚtransparencyr   é   r   z$cannot allocate more than 256 colorszunknown color specifier: )r   r"   r#   r   r
   r   r   ÚKeyErrorr   r   r   ÚinfoÚgetÚreversedÚlistÚ	enumerateÚ	histogramr$   r   Úrepr)	r   r   Úimager&   ÚeÚindexZspecial_colorsr   Úcountr   r   r   Úgetcolori   s`    



þ





ÿþÿzImagePalette.getcolorc              
   C   sÔ   | j rd}t|ƒ‚t|tƒr&t|dƒ}| d¡ | d| j› d�¡ tdƒD ]z}| |› ¡ t|t| jƒ |d t| jƒ ƒD ]<}z| d| j	| › �¡ W q~ t
y¸   | d	¡ Y q~0 q~| d¡ qL| ¡  d
S )zVSave palette to text file.

        .. warning:: This method is experimental.
        r    Úwz
# Palette
z# Mode: Ú
r.   r   ú z 0N)r   r"   r#   ÚstrÚopenÚwriter
   r   r   r   Ú
IndexErrorÚclose)r   Úfpr&   r   Újr   r   r   Úsave£   s     


&zImagePalette.save)r   N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   Úsetterr   r   r   r   Útostringr;   rF   r   r   r   r   r      s    





:r   c                 C   s   t ƒ }| |_||_d|_|S )Nr   )r   r   r   r   )r   Údatar   r   r   r   Úraw¾   s
    rO   c                 C   s6   g }| dkr.t dƒD ]}| || d ¡ qnt‚|S )Nr   r.   r*   )r   ÚappendÚNotImplementedError)ÚblackÚwhiteÚlutr   r   r   r   Úmake_linear_lutÊ   s    rU   c                 C   s4   g }t dƒD ]"}| t|d |  d d ƒ¡ q|S )Nr.   g     ào@g      à?)r   rP   Úint)ÚexprT   r   r   r   r   Úmake_gamma_lutÔ   s     rX   r   c                    s4   t tdtˆ ƒ ƒƒ}| ¡  tˆ ‡ fdd„|D ƒƒS )Nr.   c                    s   g | ]}|t ˆ ƒ ‘qS r   ©r   ©Ú.0r   ©r
   r   r   Ú
<listcomp>Þ   ó    znegative.<locals>.<listcomp>)r3   r   r   Úreverser   ©r
   r   r   r\   r   ÚnegativeÛ   s    ra   c                 C   s@   ddl m} g }tdt| ƒ ƒD ]}| |ddƒ¡ q t| |ƒS )Nr   )Úrandintr.   r*   )Úrandomrb   r   r   rP   r   )r
   rb   r   r   r   r   r   rc   á   s
    rc   ú#fff0c0c                    s0   dd„ t  | ¡D ƒ‰ td‡ fdd„tdƒD ƒƒS )Nc                 S   s   g | ]}t d |ƒ‘qS )r   )rU   )r[   Úbandr   r   r   r]   ë   r^   zsepia.<locals>.<listcomp>r   c                    s    g | ]}ˆ |d   |d   ‘qS )r)   r   rZ   ©Úbandsr   r   r]   ì   r^   i   )r   Zgetrgbr   r   )rS   r   rf   r   Úsepiaê   s    rh   c                    s,   t tdtˆ ƒ ƒƒ}tˆ ‡ fdd„|D ƒƒS )Nr.   c                    s   g | ]}|t ˆ ƒ ‘qS r   rY   rZ   r\   r   r   r]   ñ   r^   zwedge.<locals>.<listcomp>)r3   r   r   r   r`   r   r\   r   Úwedgeï   s    ri   c                 C   sŒ   t | dƒ�n}tjtjtjfD ]@}z$| d¡ ||ƒ ¡ }|rBW  qjW q ttfyZ   Y q0 qd}t|ƒ‚W d   ƒ n1 s~0    Y  |S )NÚrbr   zcannot load palette)	r@   r   r   r   ÚseekÚ
getpaletteÚSyntaxErrorr"   ÚOSError)ÚfilenamerD   ZpaletteHandlerrT   r&   r   r   r   Úloadô   s    ý

&rp   )r   )r   )rd   )r   )r%   Ú r   r   r   r   r   rO   rU   rX   ra   rc   rh   ri   rp   r   r   r   r   Ú<module>   s    '


	

