o
    ]oiS                     @  s   d dl m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 )    )annotationsN   )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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   X/var/www/html/kuke-dev/django-kuke/venv/lib/python3.10/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 r1t| j}i | _ tdt| j|D ]}t| j|||  }|| j v r)q|| | j |< q| j S )Nr   )r   lenr   ranger   tuple)r   Zmode_lenicolorr   r   r   colors4   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r| jd d  |_| j|_|S r
   )r   r   r   r   r   )r   newr   r   r   copyD   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   getdataO   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   msgZarrr   r   r   r   Z   s   zImagePalette.tobytesc                 C  s   t | jtst| j| _t| jd }d}|r/|jd|jdf}||v r/|d7 }||v s'|dkr[|rPttt	|
 D ]\}}|dkrO||vrO|} nq?|dkr[d}t|||S )	N   r   
backgroundZtransparencyr      r   z$cannot allocate more than 256 colors)r$   r   r   r   r   infogetreversedlist	enumerateZ	histogramr#   )r   imageeindexZspecial_colorsr   countr'   r   r   r   _new_color_indexj   s,   


zImagePalette._new_color_indexc              
   C  s6  | 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 } zB| ||}|| j|< |d t| j	k rw| 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	      r(      z/cannot add non-opaque RGBA color to RGB paletteNZRGBAr6   r   zunknown color specifier: )r   r#   r$   r   r   r   r   KeyErrorr4   r   r%   r   r   repr)r   r   r0   r'   r1   r2   r   r   r   getcolor   sB   



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: 
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)NNr
   )__name__
__module____qualname____doc__r   propertyr   setterr   r   r    r   Ztostringr4   r:   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                   s*   | dkr fddt dD S d}t|)Nr   c                   s   g | ]} | d  qS r7   r   .0r   whiter   r   
<listcomp>       z#make_linear_lut.<locals>.<listcomp>r*   z"unavailable when black is non-zero)r   NotImplementedError)blackrQ   r'   r   rP   r   make_linear_lut   s   rV   c                   s    fddt dD S )Nc                   s$   g | ]}t |d    d  d qS )g     o@g      ?)intrN   expr   r   rR      s   $ z"make_gamma_lut.<locals>.<listcomp>r*   )r   rX   r   rX   r   make_gamma_lut   s   rZ   r	   c                   s4   t tdt  }|  t  fdd|D S )Nr*   c                      g | ]}|t   qS r   r   rN   r   r   r   rR      rS   znegative.<locals>.<listcomp>)r.   r   r   reverser   r   r   r   r]   r   negative   s   r`   c                   s4   ddl m   fddtdt|  D }t| |S )Nr   randintc                   s   g | ]} d dqS )r   r6   r   )rO   _ra   r   r   rR          zrandom.<locals>.<listcomp>r*   )randomrb   r   r   r   r_   r   ra   r   re      s   
re   #fff0c0c                   s0   dd t | D  td fddtdD S )Nc                 S  s   g | ]}t d |qS )r   )rV   )rO   Zbandr   r   r   rR      rd   zsepia.<locals>.<listcomp>r	   c                   s    g | ]} |d   |d   qS )r(   r   rN   Zbandsr   r   rR      s     i   )r   Zgetrgbr   r   rP   r   rg   r   sepia   s   rh   c                   s,   t tdt  }t  fdd|D S )Nr*   c                   r[   r   r\   rN   r]   r   r   rR      rS   zwedge.<locals>.<listcomp>)r.   r   r   r   r_   r   r]   r   wedge   s   ri   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)	r?   r   r   r   seekZ
getpaletteSyntaxErrorr#   OSError)filenamerC   ZpaletteHandlerZlutr'   r   r   r   load   s*   

ro   )r	   )rf   )
__future__r   r&    r   r   r   r   r   rM   rV   rZ   r`   re   rh   ri   ro   r   r   r   r   <module>   s    +



