B
    -a                 @   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               @   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 )ImagePaletteas  
    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 and of length ``size``
        times the number of colors in ``mode``. The list must be aligned
        by channel (All R values must be contiguous in the list before G
        and B values.) Defaults to 0 through 255 per channel.
    :param size: An optional palette size. If given, it cannot be equal to
        or greater than 256. Defaults to 0.
    RGBNr   c             C   s@   || _ d | _|pt | _d | _|dkr<|t| jkr<tdd S )Nr   zwrong palette size)moderawmode	bytearraypalettedirtylen
ValueError)selfr	   r   size r   4/tmp/pip-unpacked-wheel-rysjrues/PIL/ImagePalette.py__init__'   s    zImagePalette.__init__c             C   s   | j S )N)_palette)r   r   r   r   r   /   s    zImagePalette.palettec             C   sf   || _ t| j}i | _xJtdt| j|D ]4}t| j|||  }|| jkrPq*|| | j|< q*W d S )Nr   )r   r   r	   colorsranger   tuple)r   r   Zmode_lenicolorr   r   r   r   3   s    

c             C   s<   t  }| j|_| j|_| jd k	r0| jd d  |_| j|_|S )N)r   r	   r
   r   r   )r   newr   r   r   copy?   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	   tobytes)r   r   r   r   getdataJ   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.
        z!palette contains raw palette dataB)r
   r   
isinstancer   bytesarrayr   )r   Zarrr   r   r   r   U   s    zImagePalette.tobytesc          
   C   s  | j rtdt|tr| jdkrJt|dkrh|d dkrh|dd }n| jdkrht|dkrh|d7 }y
| j| S  tk
r } z,t| jt	st	| j| _
t| jd }d	}|r|jd
|jdf}x||kr|d7 }qW |dkrF|r2x<ttt| D ]$\}}|dkr
||kr
|}P q
W |dkrFtd||| j|< |d t| jk r| jd|d  t| | j|d d d  | _
n|  j
t|7  _
d| _|S d}~X Y nX ntdt| dS )zgGiven an rgb tuple, allocate palette entry.

        .. warning:: This method is experimental.
        z!palette contains raw palette datar            NZRGBA)r%   r   Z
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   imageeindexZspecial_colorsr   countr   r   r   getcolord   sH    








4zImagePalette.getcolorc          
   C   s   | j rtdt|tr"t|d}|d |d| j d xtdD ]}||  xdt|t| j |d t| j D ]>}y|d| j	|   W q~ t
k
r   |d	 Y q~X q~W |d qJW |  d
S )zVSave palette to text file.

        .. warning:: This method is experimental.
        z!palette contains raw palette datawz
# Palette
z# Mode: 
r&   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   )N)__name__
__module____qualname____doc__r   propertyr   setterr   r   r   tostringr2   r=   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
    rF   c             C   s:   g }| dkr2x(t dD ]}||| d  qW nt|S )Nr   r&   r%   )r   appendNotImplementedError)Zblackwhitelutr   r   r   r   make_linear_lut   s    rK   c             C   s8   g }x.t dD ]"}|t|d |  d d  qW |S )Nr&   g     o@g      ?)r   rG   int)exprJ   r   r   r   r   make_gamma_lut   s    "rN   r   c             C   s&   t td}|  t| |t|  S )Nr&   )r+   r   reverser   r   )r	   r   r   r   r   negative   s    rP   c             C   sD   ddl m} g }x(tdt|  D ]}||dd q"W t| |S )Nr   )randintr&   r%   )randomrQ   r   r   rG   r   )r	   rQ   r   r   r   r   r   rR      s
    rR   #fff0c0c             C   s@   t | \}}}td|}td|}td|}td|| | S )Nr   r   )r   ZgetrgbrK   r   )rI   rgbr   r   r   sepia   s
    


rW   c             C   s   t | ttdt|  S )Nr&   )r   r+   r   r   )r	   r   r   r   wedge   s    rX   c             C   sv   t | db}xZtjtjtjgD ]>}y |d || }|r@P W q ttfk
rZ   Y qX qW tdW d Q R X |S )Nrbr   zcannot load palette)	r7   r   r   r   seekZ
getpaletteSyntaxErrorr   OSError)filenamer;   ZpaletteHandlerrJ   r   r   r   load   s    

r^   )r   )r   )rS   )r   )r"    r   r   r   r   r   rF   rK   rN   rP   rR   rW   rX   r^   r   r   r   r   <module>   s    


	

