|
| 1 | +from . import color4 |
| 2 | + |
| 3 | +COLOR_SPACES = color4.COLOR_SPACES | {'device-cmyk'} |
| 4 | +D50 = color4.D50 |
| 5 | +D65 = color4.D65 |
| 6 | + |
| 7 | + |
| 8 | +class Color(color4.Color): |
| 9 | + COLOR_SPACES = COLOR_SPACES |
| 10 | + |
| 11 | + |
| 12 | +def parse_color(input): |
| 13 | + color = color4.parse_color(input) |
| 14 | + |
| 15 | + if color: |
| 16 | + return color |
| 17 | + |
| 18 | + if isinstance(input, str): |
| 19 | + token = parse_one_component_value(input, skip_comments=True) |
| 20 | + else: |
| 21 | + token = input |
| 22 | + |
| 23 | + if token.type == 'function': |
| 24 | + tokens = [ |
| 25 | + token for token in token.arguments |
| 26 | + if token.type not in ('whitespace', 'comment')] |
| 27 | + name = token.lower_name |
| 28 | + length = len(tokens) |
| 29 | + |
| 30 | + if length in (7, 9) and all(token == ',' for token in tokens[1::2]): |
| 31 | + old_syntax = True |
| 32 | + tokens = tokens[::2] |
| 33 | + elif length == 4: |
| 34 | + old_syntax = False |
| 35 | + elif length == 6 and tokens[4] == '/': |
| 36 | + tokens.pop(4) |
| 37 | + old_syntax = False |
| 38 | + else: |
| 39 | + return |
| 40 | + args, alpha = tokens[:4], color4._parse_alpha(tokens[4:]) |
| 41 | + |
| 42 | + if name == 'device-cmyk': |
| 43 | + return _parse_device_cmyk(args, alpha, old_syntax) |
| 44 | + |
| 45 | + |
| 46 | +def _parse_device_cmyk(args, alpha, old_syntax): |
| 47 | + """Parse a list of CMYK channels. |
| 48 | +
|
| 49 | + If args is a list of 4 NUMBER or PERCENTAGE tokens, return |
| 50 | + device-cmyk :class:`Color`. Otherwise, return None. |
| 51 | +
|
| 52 | + Input C, M, Y, K ranges are [0, 1], output are [0, 1]. |
| 53 | +
|
| 54 | + """ |
| 55 | + if old_syntax: |
| 56 | + if color4._types(args) != {'number'}: |
| 57 | + return |
| 58 | + else: |
| 59 | + if color4._types(args) <= {'numbers', 'percentage'}: |
| 60 | + return |
| 61 | + cmyk = [ |
| 62 | + arg.value if arg.type == 'number' else |
| 63 | + arg.value / 100 if arg.type == 'percentage' else None |
| 64 | + for arg in args] |
| 65 | + return Color('device-cmyk', cmyk, alpha) |
0 commit comments