
    }f                        d dl mZ d dlZd dlmZ ddlmZmZmZm	Z	  G d d          Z
dd	Zd
 Zd ZddZddZddZddZd ZdS )    )annotationsN)Sequence   )GimpGradientFileGimpPaletteFile
ImageColorPaletteFilec                      e Zd ZdZddd
Zed             Zej        d             Zed             Zej        d             Zd Z	d Z
d ZeZddZdddZ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.
    RGBNmodestrpaletteSequence[int] | NonereturnNonec                Z    || _         d | _        |pt                      | _        d | _        d S N)r   rawmode	bytearrayr   dirty)selfr   r   s      I/var/www/rtfcompare/venv/lib/python3.11/site-packages/PIL/ImagePalette.py__init__zImagePalette.__init__&   s+    	-)++!%


    c                    | j         S r   )_paletter   s    r   r   zImagePalette.palette,   s
    }r   c                "    d | _         || _        d S r   )_colorsr   )r   r   s     r   r   zImagePalette.palette0   s    r   c                   | j         wt          | j                  }i | _         t          dt          | j                  |          D ]8}t          | j        |||z                      }|| j         v r+||z  | j         |<   9| j         S )Nr   )r    lenr   ranger   tuple)r   mode_lenicolors       r   colorszImagePalette.colors5   s    <49~~HDL1c$,//:: 4 4dl1q8|+;<==DL((&'8mU##|r   c                    || _         d S r   )r    )r   r(   s     r   r(   zImagePalette.colorsA   s    r   c                    t                      }| j        |_        | j        |_        | j        | j        d d          |_        | j        |_        |S r   )r   r   r   r   r   )r   news     r   copyzImagePalette.copyE   sD    nn9l<#,qqq/CKJ	
r   c                b    | 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   s    r   getdatazImagePalette.getdataP   s2     < 	.<--y$,,..((r   c                    | j         rd}t          |          t          | j        t                    r| j        S t          j        d| j                  }|                                S )zUConvert palette to bytes.

        .. warning:: This method is experimental.
        !palette contains raw palette dataB)r   
ValueError
isinstancer   bytesarrayr.   )r   msgarrs      r   r.   zImagePalette.tobytes[   s\    
 < 	"5CS//!dlE** 	 <k#t|,,{{}}r   c                   t          | j        t                    st          | j                  | _        t	          | j                  dz  }d}|rA|j                            d          |j                            d          f}||v r	|dz  }||v 	|dk    ri|rOt          t          t          |
                                                              D ]\  }}|dk    r||vr|} n|dk    rd}t          |          ||S )	N    
backgroundtransparencyr      r   z$cannot allocate more than 256 colors)r4   r   r   r   r"   infogetreversedlist	enumerate	histogramr3   )r   imageeindexspecial_colorsr&   countr7   s           r   _new_color_indexzImagePalette._new_color_indexk   s   $,	22 	4%dl33DMDL!!Q& 	
|,,
~..N >))
 >))C<<  (i8I8I.J.J)K)K L L  HAuzza~&=&= !||< oo1,r   intc                   | 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	z  }	 | j        |         S # t          $ r}|                     ||          }|| j        |<   |dz  t          | j	                  k     r>| j	        d|dz           t          |          z   | j	        |dz  dz   d         z   | _        n| xj        t          |          z  c_        d
| _        |cY d}~S d}~ww xY wdt          |           }t          |          )zgGiven an rgb tuple, allocate palette entry.

        .. warning:: This method is experimental.
        r1   r      r:      z/cannot add non-opaque RGBA color to RGB paletteNRGBArN   r   zunknown color specifier: )r   r3   r4   r$   r   r"   r(   KeyErrorrJ   r   r5   r   r   repr)r   r'   rE   r7   rF   rG   s         r   getcolorzImagePalette.getcolor   s   
 < 	"5CS//!eU## 	"yE!!u::??Qx3O(oo-!"1"IEf$$u::??VOE{5))   --eQ77%*E"19s4<0000[uqy[1,,',uqy1}78 MM MMU5\\1MM
 <d5kk;;CS//!s   B& &
E0BEEEc                   | j         rd}t          |          t          |t                    rt	          |d          }|                    d           |                    d| j         d           t          d          D ]}|                    |            t          |t          | j                  z  |dz   t          | j                  z            D ]K}	 |                    d| j	        |                     '# t          $ r |                    d	           Y Hw xY w|                    d           |                                 d
S )zVSave palette to text file.

        .. warning:: This method is experimental.
        r1   wz
# Palette
z# Mode: 
r>   r    z 0N)r   r3   r4   r   openwriter   r#   r"   r   
IndexErrorclose)r   fpr7   r&   js        r   savezImagePalette.save   sI   
 < 	"5CS//!b# 	b#B

)DI)))***s 	 	AHHV1s49~~-ATY/GHH # ##HH2a223333! # # #HHTNNNNN#HHTNNNN





s   #C<<DD)r   N)r   r   r   r   r   r   )NNr   )r   rK   )__name__
__module____qualname____doc__r   propertyr   setterr(   r,   r/   r.   tostringrJ   rS   r^   r;   r   r   r   r      s       	 	& & & & &   X ^    ^  	 	 X	 ]  ]	 	 		) 	) 	)   H   0$" $" $" $" $"L    r   r   r   c                L    t                      }| |_        ||_        d|_        |S )Nr   )r   r   r   r   )r   datar   s      r   rawrh      s%    nnGGOGOGMNr   c                h    | dk    rfdt          d          D             S d}t          |          )Nr   c                     g | ]
}|z  d z  S rP   r;   ).0r&   whites     r   
<listcomp>z#make_linear_lut.<locals>.<listcomp>   s"    555Q	S 555r   r>   z"unavailable when black is non-zero)r#   NotImplementedError)blackrl   r7   s    ` r   make_linear_lutrp      s?    zz5555%**5555
.C
c
"
""r   c                :      fdt          d          D             S )Nc                F    g | ]}t          |d z  z  d z  dz             S )g     o@g      ?)rK   )rk   r&   exps     r   rm   z"make_gamma_lut.<locals>.<listcomp>   s4    HHHC!e)#u,s233HHHr   r>   )r#   )rs   s   `r   make_gamma_lutrt      s#    HHHHU3ZZHHHHr   r   c                     t          t          dt                     z                      }|                                 t	            fd|D                       S )Nr>   c                4    g | ]}|t                    z  S r;   r"   rk   r&   r   s     r   rm   znegative.<locals>.<listcomp>   #    ???!qCII~???r   )rB   r#   r"   reverser   r   r   s   ` r   negativer|      sW    5s4yy))**GOO????w???@@@r   c                    ddl m fdt          dt          |           z            D             }t	          | |          S )Nr   )randintc                (    g | ]} d d          S )r   rN   r;   )rk   _r~   s     r   rm   zrandom.<locals>.<listcomp>   s#    ???1wwq#???r   r>   )randomr~   r#   r"   r   )r   r   r~   s     @r   r   r      sP    ????cCIIo(>(>???Gg&&&r   #fff0c0c                    d t          j        |           D             t          dfdt          d          D                       S )Nc                .    g | ]}t          d |          S )r   )rp   )rk   bands     r   rm   zsepia.<locals>.<listcomp>   s"    KKK$_Q%%KKKr   r   c                8    g | ]}|d z           |d z           S )r:   r;   )rk   r&   bandss     r   rm   zsepia.<locals>.<listcomp>   s*    MMMa!eQ!V 4MMMr   i   )r   getrgbr   r#   )rl   r   s    @r   sepiar      sL    KK*2CE2J2JKKKEMMMMeGnnMMMNNNr   c                     t          t          dt                     z                      }t            fd|D                       S )Nr>   c                4    g | ]}|t                    z  S r;   rw   rx   s     r   rm   zwedge.<locals>.<listcomp>   ry   r   )rB   r#   r"   r   r{   s   ` r   wedger      sG    5s4yy))**G????w???@@@r   c                b   t          | d          5 }t          j        t          j        t          j        fD ]P}	 |                    d            ||                                          }|r n):# t          t          f$ r Y Mw xY wd}t          |          	 d d d            n# 1 swxY w Y   |S )Nrbr   zcannot load palette)	rX   r   r   r	   seek
getpaletteSyntaxErrorr3   OSError)filenamer\   paletteHandlerlutr7   s        r   loadr      s    
h		 +-#
 	 	N



$nR((3355 E,    (C#,,               " Js4   &B$4A/,B$/B B$BB$$B(+B()r   r   )r   )r   )
__future__r   r6   typingr    r   r   r   r	   r   rh   rp   rt   r|   r   r   r   r   r;   r   r   <module>r      s3  $ # " " " " "        H H H H H H H H H H H Hc c c c c c c cT   # # #I I IA A A A' ' ' 'O O O O
A A A A
    r   