
    (ph
#                        S SK Jr  S SKrS SKJrJrJr  SSKJrJ	r	J
r
Jr  \(       a  SSKJr   " S S5      rSS	 jrSS
 jrSS jrSSS jjrSSS jjrSSS jjrSSS jjrSS jrg)    )annotationsN)IOTYPE_CHECKINGSequence   )GimpGradientFileGimpPaletteFile
ImageColorPaletteFile)Imagec                     \ rS rSrSr  S     SS jjr\SS j5       r\R                  SS j5       r\SS j5       r	\	R                  SS j5       r	SS	 jr
SS
 jrSS jr\r S     SS jjr S     SS jjrSS jrSrg)ImagePalette   a  
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.
Nc                \    Xl         S U l        U=(       d
    [        5       U l        S U l        g N)moderawmode	bytearraypalettedirty)selfr   r   s      C/var/www/html/venv/lib/python3.13/site-packages/PIL/ImagePalette.py__init__ImagePalette.__init__)   s%    
 	#'-)+!%
    c                    U R                   $ r   )_paletter   s    r   r   ImagePalette.palette3   s    }}r   c                    S U l         Xl        g r   )_colorsr   )r   r   s     r   r   r   7   s    :>r   c                2   U R                   c  [        U R                  5      n0 U l         [        S[        U R                  5      U5       H?  n[        U R                  X"U-    5      nX0R                   ;   a  M.  X!-  U R                   U'   MA     U R                   $ )Nr   )r!   lenr   ranger   tuple)r   mode_lenicolors       r   colorsImagePalette.colors<   s|    <<499~HDL1c$,,/:dll18|<=LL(&'mU#	 ;
 ||r   c                    Xl         g r   )r!   )r   r)   s     r   r)   r*   H   s    r   c                    [        5       nU R                  Ul        U R                  Ul        U R                  b  U R                  S S  Ul        U R                  Ul        U$ r   )r   r   r   r   r   )r   news     r   copyImagePalette.copyL   sH    n99ll<<#,,q/CKJJ	
r   c                    U R                   (       a  U R                   U R                  4$ U R                  U R                  5       4$ )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   getdataImagePalette.getdataW   s3     <<<<--yy$,,.((r   c                    U R                   (       a  Sn[        U5      e[        U R                  [        5      (       a  U R                  $ [
        R
                  " SU R                  5      nUR                  5       $ )zEConvert palette to bytes.

.. warning:: This method is experimental.
!palette contains raw palette dataB)r   
ValueError
isinstancer   bytesarrayr1   )r   msgarrs      r   r1   ImagePalette.tobytesb   sU    
 <<5CS/!dllE**<<kk#t||,{{}r   c                   [        U R                  [        5      (       d  [        U R                  5      U l        [	        U R                  5      S-  nSnU(       aG  UR
                  R                  S5      UR
                  R                  S5      4nX4;   a  US-  nX4;   a  M  US:  a`  U(       aE  [        [        [        UR                  5       5      5      5       H  u  pVUS:X  d  M  XT;  d  M  Un  O   US:  a  Sn[        U5      UeU$ )	N    
backgroundtransparencyr      r   z$cannot allocate more than 256 colors)r8   r   r   r   r#   infogetreversedlist	enumerate	histogramr7   )r   imageeindexspecial_colorsr'   countr;   s           r   _new_color_indexImagePalette._new_color_indexr   s     $,,	22%dll3DMDLL!Q&CE

|,

~.N )
 )C< (i8I.J)K LHAza&= ! !M |< o1,r   c                    U R                   (       a  Sn[        U5      e[        U[        5      (       ao  U R                  S:X  a+  [        U5      S:X  a  US   S:w  a  Sn[        U5      eUSS nO$U R                  S:X  a  [        U5      S:X  a  US	-  n U R                  U   $ S[        U5       3n[        U5      e! [         a  nU R                  X$5      n[        U R                  [        5      (       d   eXPR                  U'   US-  [        U R                  5      :  a:  U R                  SUS-   [        U5      -   U R                  US-  S-   S -   U l	        OU =R                  [        U5      -  sl	        S
U l        Us SnA$ SnAff = f)zWGiven an rgb tuple, allocate palette entry.

.. warning:: This method is experimental.
r5   RGB   r?      z/cannot add non-opaque RGBA color to RGB paletteNRGBA)rT   r   zunknown color specifier: )r   r7   r8   r%   r   r#   r)   KeyErrorrO   r   r   r   r9   r   repr)r   r(   rJ   r;   rK   rL   s         r   getcolorImagePalette.getcolor   sn    <<5CS/!eU##yyE!u:?Qx3O(o-!"1IEf$u:?VOE{{5))" .d5k];CS/!#  --e7!$--;;;;%*E"19s4<<00k	2,'--	A89 M MMU5\1M
s   B< <
FB<FFFc                x   U R                   (       a  Sn[        U5      e[        U[        5      (       a  [	        US5      nUR                  S5        UR                  SU R                   S35        [        S5       H  nUR                  U 5        [        U[        U R                  5      -  US-   [        U R                  5      -  5       H%  n UR                  SU R                  U    35        M'     UR                  S5        M     UR                  5         g
! [         a    UR                  S	5         Mm  f = f)zFSave palette to text file.

.. warning:: This method is experimental.
r5   wz
# Palette
z# Mode: 
rC   r    z 0N)r   r7   r8   stropenwriter   r$   r#   r   
IndexErrorclose)r   fpr;   r'   js        r   saveImagePalette.save   s    
 <<5CS/!b#b#B

8DII;b)*sAHHs1s499~-ATYY/GH#HHqa 123 I
 HHTN  	
 " #HHTN#s   !DD98D9)r!   r   r   r   r   r   )rR   N)r   r^   r   z(Sequence[int] | bytes | bytearray | NonereturnNone)rg   !Sequence[int] | bytes | bytearray)r   ri   rg   rh   )rg   dict[tuple[int, ...], int])r)   rj   rg   rh   )rg   r   )rg   z-tuple[str, Sequence[int] | bytes | bytearray])rg   r9   )NN)rJ   Image.Image | NonerK   zException | Nonerg   intr   )r(   ztuple[int, ...]rJ   rk   rg   rl   )rc   zstr | IO[str]rg   rh   )__name__
__module____qualname____firstlineno____doc__r   propertyr   setterr)   r.   r2   r1   tostringrO   rX   re   __static_attributes__r@   r   r   r   r      s    	 <@&& :& 
	&   ^^    	 	 ]] 		) H GK'3C	: %))")" ")" 
	)"Vr   r   c                @    [        5       nXl        Xl        SUl        U$ )Nr   )r   r   r   r   )r   datar   s      r   rawrx      s    nGOOGMNr   c                    U S:X  a*  [        S5       Vs/ s H  n[        X-  S-  5      PM     sn$ Sn[        U5      es  snf )Nr   rC   rT   z"unavailable when black is non-zero)r$   rl   NotImplementedError)blackwhiter'   r;   s       r   make_linear_lutr}      sC    z/4Sz:z!EI$%z::
.C
c
"" ;s   >c                n    [        S5       Vs/ s H  n[        US-  U -  S-  S-   5      PM     sn$ s  snf )NrC   g     o@g      ?)r$   rl   )expr'   s     r   make_gamma_lutr      s6    =B3ZHZC!e)#u,s23ZHHHs   !2c           
         [        [        S[        U 5      -  5      5      nUR                  5         [	        X Vs/ s H  o"[        U 5      -  PM     sn5      $ s  snf NrC   )rG   r$   r#   reverser   r   r   r'   s      r   negativer      sH    5s4y)*GOOw?w!CI~w?@@?s   A
c                    SSK Jn  [        S[        U 5      -  5       Vs/ s H  o!" SS5      PM     nn[	        X5      $ s  snf )Nr   )randintrC   rT   )randomr   r$   r#   r   )r   r   _r   s       r   r   r      s?    (-cCIo(>?(>1wq#(>G?&& @s   ?c           	         [         R                  " U 5       Vs/ s H  n[        SU5      PM     nn[        S[	        S5       Vs/ s H  o2US-     US-     PM     sn5      $ s  snf s  snf )Nr   rR   i   r?   )r
   getrgbr}   r   r$   )r|   bandbandsr'   s       r   sepiar      sb    2<2C2CE2JK2J$_Q%2JEKeGnMna!eQ!V 4nMNN LMs   A"A'
c           
         [        [        S[        U 5      -  5      5      n[        X Vs/ s H  o"[        U 5      -  PM     sn5      $ s  snf r   )rG   r$   r#   r   r   s      r   wedger      s>    5s4y)*Gw?w!CI~w?@@?s   A
c                p   [        U S5       n[        R                  [        R                  [        R                  /nU H4  n UR	                  S5        U" U5      R                  5       nU(       a    OM6     Sn[        U5      e S S S 5        U$ ! [        [        4 a     Mc  f = f! , (       d  f       W$ = f)Nrbr   zcannot load palette)	r_   r	   r   r   seek
getpaletteSyntaxErrorr7   OSError)filenamerc   paletteHandlerspaletteHandlerlutr;   s         r   loadr     s     
h	 ++--##
 	 .N
$R(335 	 . (C#, # 
0 J  , % 
	0 Js/   5B&.B1B&B#B&"B##B&&
B5)rw   ri   rg   r   )r{   rl   r|   floatrg   	list[int])r   r   rg   r   )rR   )r   r^   rg   r   )z#fff0c0)r|   r^   rg   r   )r   r^   rg   ztuple[bytes, str])
__future__r   r:   typingr   r   r    r   r	   r
   r   r   r   rx   r}   r   r   r   r   r   r   r@   r   r   <module>r      sX   $ #  . . H Hn nj#IA'O
A
r   