Checks that a colour argument for transparent or background options is the right form. Returns the colour (which, if it's a bare integer, is "corrected" to a 1-tuple).
(c, greyscale, which)
| 299 | |
| 300 | |
| 301 | def check_color(c, greyscale, which): |
| 302 | """ |
| 303 | Checks that a colour argument for transparent or background options |
| 304 | is the right form. |
| 305 | Returns the colour |
| 306 | (which, if it's a bare integer, is "corrected" to a 1-tuple). |
| 307 | """ |
| 308 | |
| 309 | if c is None: |
| 310 | return c |
| 311 | if greyscale: |
| 312 | try: |
| 313 | len(c) |
| 314 | except TypeError: |
| 315 | c = (c,) |
| 316 | if len(c) != 1: |
| 317 | raise ProtocolError("%s for greyscale must be 1-tuple" % which) |
| 318 | if not is_natural(c[0]): |
| 319 | raise ProtocolError("%s colour for greyscale must be integer" % which) |
| 320 | else: |
| 321 | if not ( |
| 322 | len(c) == 3 and is_natural(c[0]) and is_natural(c[1]) and is_natural(c[2]) |
| 323 | ): |
| 324 | raise ProtocolError("%s colour must be a triple of integers" % which) |
| 325 | return c |
| 326 | |
| 327 | |
| 328 | class Error(Exception): |
no test coverage detected