o
    Z#a                     @   s   d dl Z 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                   @   sb   e Zd ZdZd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.
    :param size: An optional palette size. If given, an error is raised
        if ``palette`` is not of equal length.
    RGBNr   c                 C   sP   || _ d | _|p
t | _d | _|dkr$tdt |t| jkr&t	dd S d S )Nr   zOThe size parameter is deprecated and will be removed in Pillow 10 (2023-07-01).zwrong palette size)
moderawmode	bytearraypalettedirtywarningswarnDeprecationWarninglen
ValueError)selfr	   r   size r   2/usr/lib/python3/dist-packages/PIL/ImagePalette.py__init__'   s   zImagePalette.__init__c                 C   s   | j S N)_paletter   r   r   r   r   5   s   zImagePalette.palettec                 C   sb   || _ t| j}i | _tdt| j|D ]}t| j|||  }|| jv r'q|| | j|< qd S )Nr   )r   r   r	   colorsranger   tuple)r   r   mode_lenicolorr   r   r   r   9   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   copyE   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   getdataP   s   zImagePalette.getdatac                 C   s6   | j rtd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   arrr   r   r   r#   [   s   zImagePalette.tobytesc              
   C   s  | j rtdt|tr| jdkr$t|dkr#|d dkr#|dd }n| jdkr3t|dkr3|d7 }z| j| W S  ty } zt| jt	sMt	| j| _
t| jd }d	}|rd|jd
|jdf}||v rp|d7 }||v sh|dkr|rttt| D ]\}}|dkr||vr|} nq|dkrtd||| 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 tdt| )zgGiven an rgb tuple, allocate palette entry.

        .. warning:: This method is experimental.
        r%   r            N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    imageeindexspecial_colorsr   countr   r   r   getcolorj   s^   






"zImagePalette.getcolorc              
   C   s   | j rtd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
yZ   |d	 Y q=w |d q$|  d
S )zVSave palette to text file.

        .. warning:: This method is experimental.
        r%   wz
# Palette
z# Mode: 
r1   r    z 0N)r
   r   r'   stropenwriter	   r   r   r   
IndexErrorclose)r   fpr   jr   r   r   save   s    


&zImagePalette.save)r   Nr   r   )__name__
__module____qualname____doc__r   propertyr   setterr"   r$   r#   tostringr?   rJ   r   r   r   r   r      s    



4r   c                 C   s   t  }| |_||_d|_|S )Nr   )r   r
   r   r   )r
   datar   r   r   r   raw   s
   rS   c                 C   s4   g }| dkrt dD ]}||| d  q
|S t)Nr   r1   r-   )r   appendNotImplementedError)blackwhitelutr   r   r   r   make_linear_lut   s   rY   c                 C   s4   g }t dD ]}|t|d |  d d  q|S )Nr1   g     o@g      ?)r   rT   int)exprX   r   r   r   r   make_gamma_lut   s    r\   r   c                    s4   t tdt  }|  t  fdd|D S )Nr1   c                       g | ]}|t   qS r   r   .0r   r	   r   r   
<listcomp>       znegative.<locals>.<listcomp>)r6   r   r   reverser   r	   r   r   ra   r   negative   s   rf   c                 C   s@   ddl m} g }tdt|  D ]
}||dd qt| |S )Nr   )randintr1   r-   )randomrg   r   r   rT   r   )r	   rg   r   r   r   r   r   rh      s
   
rh   #fff0c0c                    s0   dd t | D  td fddtdD S )Nc                 S   s   g | ]}t d |qS )r   )rY   )r`   bandr   r   r   rb      s    zsepia.<locals>.<listcomp>r   c                    s    g | ]} |d   |d   qS )r,   r   r_   bandsr   r   rb      s     i   )r   getrgbr   r   )rW   r   rk   r   sepia   s   rn   c                    s,   t tdt  }t  fdd|D S )Nr1   c                    r]   r   r^   r_   ra   r   r   rb      rc   zwedge.<locals>.<listcomp>)r6   r   r   r   re   r   ra   r   wedge   s   ro   c                 C   s   t | d5}tjtjtjfD ]}z|d || }|r!W  nW q ttfy-   Y qw tdW d    |S 1 s=w   Y  |S )Nrbr   zcannot load palette)	rD   r   r   r   seek
getpaletteSyntaxErrorr   OSError)filenamerH   paletteHandlerrX   r   r   r   load   s(   

rw   )r   )ri   )r)   r    r   r   r   r   r   rS   rY   r\   rf   rh   rn   ro   rw   r   r   r   r   <module>   s     



	
