## 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__importannotationsimportrefromtypingimportIOfrom._binaryimporto8
[docs]classGimpPaletteFile:"""File handler for GIMP's palette format."""rawmode="RGB"def__init__(self,fp:IO[bytes])->None:palette=[o8(i)*3foriinrange(256)]iffp.readline()[:12]!=b"GIMP Palette":msg="not a GIMP palette file"raiseSyntaxError(msg)foriinrange(256):s=fp.readline()ifnots:break# skip fields and comment linesifre.match(rb"\w+:|#",s):continueiflen(s)>100:msg="bad palette file"raiseSyntaxError(msg)v=tuple(map(int,s.split()[:3]))iflen(v)!=3:msg="bad palette entry"raiseValueError(msg)palette[i]=o8(v[0])+o8(v[1])+o8(v[2])self.palette=b"".join(palette)