a
    !f                     @   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   sP   || _ d | _|pt | _d | _|dkrLtddd  |t| jkrLd}t|d S )Nr   zThe size parameter
   zwrong palette size)moderawmode	bytearraypalettedirtyr   len
ValueError)selfr   r   sizemsg r   Q/var/www/html/python-backend/venv/lib/python3.9/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 rbt| j}i | _ tdt| j|D ]4}t| j|||  }|| j v rRq,|| | j |< q,| j S )Nr   )r   r   r   ranger   tuple)r   Z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r0| 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   Zarrr   r   r   r#   _   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	st	| 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 paletteNZRGBA)r,   r   
backgroundZ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	enumerateZ	histogramr(   r   repr)	r   r   imager   eindexZspecial_colorsr   countr   r   r   getcoloro   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	   Nr   )N)__name__
__module____qualname____doc__r   propertyr   setterr    r"   r$   r#   Ztostringr:   rE   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
    rM   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    rS   c                 C   s4   g }t dD ]"}|t|d |  d d  q|S )Nr.   g     o@g      ?)r   rN   int)exprR   r   r   r   r   make_gamma_lut   s     rV   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   rZ   r   negative   s    r_   c                 C   s@   ddl m} g }tdt|  D ]}||dd q t| |S )Nr   )randintr.   r,   )randomr`   r   r   rN   r   )r   r`   r   r   r   r   r   ra      s
    ra   #fff0c0c                    s0   dd t | D  td fddtdD S )Nc                 S   s   g | ]}t d |qS )r   )rS   )rY   Zbandr   r   r   r[      r\   zsepia.<locals>.<listcomp>r	   c                    s    g | ]} |d   |d   qS )r+   r   rX   Zbandsr   r   r[      r\   i   )r   Zgetrgbr   r   )rQ   r   rc   r   sepia   s    rd   c                    s,   t tdt  }t  fdd|D S )Nr.   c                    s   g | ]}|t   qS r   rW   rX   rZ   r   r   r[      r\   zwedge.<locals>.<listcomp>)r3   r   r   r   r^   r   rZ   r   wedge   s    re   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   seekZ
getpaletteSyntaxErrorr   OSError)filenamerC   ZpaletteHandlerrR   r   r   r   r   load   s    

&rk   )r	   )r	   )rb   )r	   )r)    r   r   r   r   Z
_deprecater   r   rM   rS   rV   r_   ra   rd   re   rk   r   r   r   r   <module>   s    ,


	

