Check a palette argument (to the :class:`Writer` class) for validity. Returns the palette as a list if okay; raises an exception otherwise.
(palette)
| 240 | |
| 241 | |
| 242 | def check_palette(palette): |
| 243 | """ |
| 244 | Check a palette argument (to the :class:`Writer` class) for validity. |
| 245 | Returns the palette as a list if okay; |
| 246 | raises an exception otherwise. |
| 247 | """ |
| 248 | |
| 249 | # None is the default and is allowed. |
| 250 | if palette is None: |
| 251 | return None |
| 252 | |
| 253 | p = list(palette) |
| 254 | if not (0 < len(p) <= 256): |
| 255 | raise ProtocolError( |
| 256 | "a palette must have between 1 and 256 entries," |
| 257 | " see https://www.w3.org/TR/PNG/#11PLTE" |
| 258 | ) |
| 259 | seen_triple = False |
| 260 | for i, t in enumerate(p): |
| 261 | if len(t) not in (3, 4): |
| 262 | raise ProtocolError("palette entry %d: entries must be 3- or 4-tuples." % i) |
| 263 | if len(t) == 3: |
| 264 | seen_triple = True |
| 265 | if seen_triple and len(t) == 4: |
| 266 | raise ProtocolError( |
| 267 | "palette entry %d: all 4-tuples must precede all 3-tuples" % i |
| 268 | ) |
| 269 | for x in t: |
| 270 | if int(x) != x or not (0 <= x <= 255): |
| 271 | raise ProtocolError( |
| 272 | "palette entry %d: values must be integer: 0 <= x <= 255" % i |
| 273 | ) |
| 274 | return p |
| 275 | |
| 276 | |
| 277 | def check_sizes(size, width, height): |
no test coverage detected