## Python Imaging Library# $Id$## stuff to read GIMP palette files## History:# 1997-08-23 fl Created# 2004-09-07 fl Support GIMP 2.0 palette files.## Copyright (c) Secret Labs AB 1997-2004. All rights reserved.# Copyright (c) Fredrik Lundh 1997-2004.## See the README file for information on usage and redistribution.#from__future__importannotationsimportrefromioimportBytesIOfromtypingimportIO
[docs]classGimpPaletteFile:"""File handler for GIMP's palette format."""rawmode="RGB"def_read(self,fp:IO[bytes],limit:bool=True)->None:ifnotfp.readline().startswith(b"GIMP Palette"):msg="not a GIMP palette file"raiseSyntaxError(msg)palette:list[int]=[]i=0whileTrue:iflimitandi==256+3:breaki+=1s=fp.readline()ifnots:break# skip fields and comment linesifre.match(rb"\w+:|#",s):continueiflimitandlen(s)>100:msg="bad palette file"raiseSyntaxError(msg)v=s.split(maxsplit=3)iflen(v)<3:msg="bad palette entry"raiseValueError(msg)palette+=(int(v[i])foriinrange(3))iflimitandlen(palette)==768:breakself.palette=bytes(palette)def__init__(self,fp:IO[bytes])->None:self._read(fp)