B .^@szddlZddlmZmZmZmZGdddZddZdd Zd d Z dd dZ dddZ dddZ dddZ ddZdS)N)GimpGradientFileGimpPaletteFile ImageColor PaletteFilec@sFeZdZdZdddZddZd d Zd d ZeZd dZ ddZ dS) 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. RGBNrcCsv||_d|_|p$ttdt|j|_i|_d|_|dkrTt|jdt|jksj|dkrr|t|jkrrtddS)Nrzwrong palette size) moderawmode bytearrayrangelenpalettecolorsdirty ValueError)selfr rsizer4/tmp/pip-install-mwhup7aw/Pillow/PIL/ImagePalette.py__init__'s zImagePalette.__init__cCsHt}|j|_|j|_|jdk r0|jdd|_|j|_|j|_|S)N)rr r rrcopyr)rnewrrrr2s  zImagePalette.copycCs$|jr|j|jfS|jd|fS)z Get palette contents in format suitable for the low-level ``im.putpalette`` primitive. .. warning:: This method is experimental. z;L)r rr tobytes)rrrrgetdata>s zImagePalette.getdatacCsH|jrtdt|jtr |jStd|j}t|dr@|S|S)zUConvert palette to bytes. .. warning:: This method is experimental. z!palette contains raw palette dataBr) r r isinstancerbytesarrayhasattrrtostring)rZarrrrrrIs  zImagePalette.tobytescCs|jrtdt|try |j|Stk rt|jtrJt|j|_t |j}|dkrdtd||j|<|d|j|<|d|j|d<|d|j|d<d|_ |SXn td|d S) zgGiven an rgb tuple, allocate palette entry. .. warning:: This method is experimental. z!palette contains raw palette datar z$cannot allocate more than 256 colorsrrizunknown color specifier: %rN) r rrtuplerKeyErrorrrr rr)rcolorindexrrrgetcolorZs$      zImagePalette.getcolorc Cs|jrtdt|tr"t|d}|d|d|jxtdD]}|d|xbt|t|j|dt|jD]<}y|d|j |Wq|t k r|d Yq|Xq|W|d qFW| d S) zVSave palette to text file. .. warning:: This method is experimental. z!palette contains raw palette datawz # Palette z # Mode: %s r z%drz %dz 0 N) r rrstropenwriter r rr IndexErrorclose)rfpijrrrsavets   (zImagePalette.save)rNr) __name__ __module__ __qualname____doc__rrrrr!r'r2rrrrrs    rcCst}||_||_d|_|S)Nr)rr rr)r datarrrrraws r8cCs:g}|dkr2x(tdD]}|||dqWnt|S)Nrr )r appendNotImplementedError)Zblackwhitelutr0rrrmake_linear_luts r>cCs8g}x.tdD]"}|t|d|ddqW|S)Nr go@g?)r r:int)expr=r0rrrmake_gamma_luts"rArcCs&ttd}|t||t|S)Nr )listr reverserr)r rrrrnegatives rDcCsDddlm}g}x(tdt|D]}||ddq"Wt||S)Nr)randintr r9)randomrEr rr:r)r rErr0rrrrFs  rF#fff0c0cCs@t|\}}}td|}td|}td|}td|||S)Nrr)rZgetrgbr>r)r<rgbrrrsepias    rKcCst|ttdt|S)Nr )rrBr r)r rrrwedgesrLc Csvt|db}xZtjtjtjgD]>}y |d||}|r@PWqttfk rZYqXqWtdWdQRX|S)Nrbrzcannot load palette) r+rrrseekZ getpalette SyntaxErrorrOSError)filenamer/ZpaletteHandlerr=rrrloads     rR)r)r)rG)r)rrrrrrr8r>rArDrFrKrLrRrrrrsv