o
    svXjÕ  ã                   @   s†   d dl Z ddlmZmZmZmZ ddl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ÚPaletteFile)Ú	deprecatec                   @   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.
    ÚRGBNr   c                 C   sT   || _ d | _|p
tƒ | _d | _|dkr&tddd ƒ |t| jƒkr(d}t|ƒ‚d S d S )Nr   zThe size parameteré
   zwrong palette size)ÚmodeÚrawmodeÚ	bytearrayÚpaletteÚdirtyr   ÚlenÚ
ValueError)Úselfr   r   ÚsizeÚmsg© r   úM/var/www/html/myproject/venv/lib/python3.10/site-packages/PIL/ImagePalette.pyÚ__init__%   s   üzImagePalette.__init__c                 C   s   | j S ©N)Ú_palette©r   r   r   r   r   0   s   zImagePalette.palettec                 C   s   d | _ || _d S r   )Ú_colorsr   )r   r   r   r   r   r   4   s   
c                 C   sh   | j d u r1t| jƒ}i | _ tdt| jƒ|ƒD ]}t| j||| … ƒ}|| j v r)q|| | j |< q| j S )Nr   )r   r   r   Úranger   Útuple)r   Úmode_lenÚiÚcolorr   r   r   Úcolors9   s   


zImagePalette.colorsc                 C   s
   || _ d S r   )r   )r   r!   r   r   r   r!   E   s   
c                 C   s<   t ƒ }| j|_| j|_| jd ur| jd d … |_| j|_|S r   )r   r   r   r   r   )r   Únewr   r   r   ÚcopyI   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   ÚgetdataT   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   r   Ú
isinstancer   ÚbytesÚarrayr$   )r   r   Úarrr   r   r   r$   _   s   zImagePalette.tobytesc           	   
   C   sà  | j r	d}t|ƒ‚t|tƒrå| jdkr,t|ƒdkr+|d dkr%d}t|ƒ‚|dd… }n| jdkr;t|ƒdkr;|d	7 }z| j| W S  tyä } z—t| jt	ƒsUt	| jƒ| _
t| jƒd }d
}|rl|j d¡|j d¡f}||v rx|d7 }||v sp|dkr¤|r™ttt| ¡ ƒƒƒD ]\}}|dkr˜||vr˜|} nqˆ|dkr¤d}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}~ww dt|ƒ› �}t|ƒ‚)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ÚindexÚspecial_colorsr   Úcountr   r   r   Úgetcoloro   sj   

€


þÿ€

ÿþÿ€ß#zImagePalette.getcolorc              
   C   sÒ   | j r	d}t|ƒ‚t|tƒrt|dƒ}| d¡ | d| j› d�¡ tdƒD ]<}| |› ¡ t|t| jƒ |d t| jƒ ƒD ]}z| d| j	| › �¡ W q? t
y\   | d	¡ Y q?w | d¡ q&| ¡  d
S )zVSave palette to text file.

        .. warning:: This method is experimental.
        r&   Úwz
# Palette
z# Mode: Ú
r2   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	   Nr   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   Úsetterr!   r#   r%   r$   Útostringr@   rK   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
   rT   c                 C   s4   g }| dkrt dƒD ]}| || d ¡ q
|S t‚)Nr   r2   r.   )r   ÚappendÚNotImplementedError)ÚblackÚwhiteÚlutr   r   r   r   Úmake_linear_lutÐ   s   ÿrZ   c                 C   s4   g }t dƒD ]}| t|d |  d d ƒ¡ q|S )Nr2   g     ào@g      à?)r   rU   Úint)ÚexprY   r   r   r   r   Úmake_gamma_lutÚ   s    r]   r	   c                    s4   t tdtˆ ƒ ƒƒ}| ¡  tˆ ‡ fdd„|D ƒƒS )Nr2   c                    ó   g | ]}|t ˆ ƒ ‘qS r   ©r   ©Ú.0r   ©r   r   r   Ú
<listcomp>ä   ó    znegative.<locals>.<listcomp>)r7   r   r   Úreverser   ©r   r   r   rb   r   Únegativeá   s   rg   c                 C   s@   ddl m} g }tdt| ƒ ƒD ]
}| |ddƒ¡ qt| |ƒS )Nr   )Úrandintr2   r.   )Úrandomrh   r   r   rU   r   )r   rh   r   r   r   r   r   ri   ç   s
   
ri   ú#fff0c0c                    s0   dd„ t  | ¡D ƒ‰ td‡ fdd„tdƒD ƒƒS )Nc                 S   s   g | ]}t d |ƒ‘qS )r   )rZ   )ra   Úbandr   r   r   rc   ñ   s    zsepia.<locals>.<listcomp>r	   c                    s    g | ]}ˆ |d   |d   ‘qS )r-   r   r`   ©Úbandsr   r   rc   ò   s     i   )r   Úgetrgbr   r   )rX   r   rl   r   Úsepiað   s   ro   c                    s,   t tdtˆ ƒ ƒƒ}tˆ ‡ fdd„|D ƒƒS )Nr2   c                    r^   r   r_   r`   rb   r   r   rc   ÷   rd   zwedge.<locals>.<listcomp>)r7   r   r   r   rf   r   rb   r   Úwedgeõ   s   rp   c                 C   sŒ   t | dƒ�7}tjtjtjfD ]}z| d¡ ||ƒ ¡ }|r!W  nW q ttfy-   Y qw d}t|ƒ‚W d   ƒ |S 1 s?w   Y  |S )NÚrbr   zcannot load palette)	rE   r   r   r   ÚseekÚ
getpaletteÚSyntaxErrorr   ÚOSError)ÚfilenamerI   ÚpaletteHandlerrY   r   r   r   r   Úloadú   s*   ý
ÿýù
öírx   )r	   )rj   )r*   Ú r   r   r   r   Ú
_deprecater   r   rT   rZ   r]   rg   ri   ro   rp   rx   r   r   r   r   Ú<module>   s    ,



	
