builds up a byte-chunk for a color

    the format for this is
        b'\x00\x01' +
        Big-Endian Unsigned Int == len(bytes that follow in this block)
          • Big-Endian Unsigned Short == len(color_name)
              in practice, because utf-16 takes up 2 bytes per letter
              this will be 2 * (len(name) + 1)
              so a color named 'foo' would be 8 bytes long
          • UTF-16BE Encoded color_name terminated with '\0'
              using 'foo', this yields '\x00f\x00o\x00o\x00\x00'
          • A 4-byte char for Color mode ('RGB ', 'Gray', 'CMYK', 'LAB ')
              note the trailing spaces
          • a variable-length number of 4-byte length floats
              this depends entirely on the color mode of the color.
          • A Big-Endian short int for either a global, spot, or process color
              global == 0, spot == 1, process == 2

    the chunk has no terminating string although other sites have indicated
    that the global/spot/process short is a terminator, it's actually used
    to indicate how illustrator should deal with the color.

def chunk_for_color(obj):
    """builds up a byte-chunk for a color

    the format for this is
        b'\x00\x01' +
        Big-Endian Unsigned Int == len(bytes that follow in this block)
          • Big-Endian Unsigned Short == len(color_name)
              in practice, because utf-16 takes up 2 bytes per letter
              this will be 2 * (len(name) + 1)
              so a color named 'foo' would be 8 bytes long
          • UTF-16BE Encoded color_name terminated with '\0'
              using 'foo', this yields '\x00f\x00o\x00o\x00\x00'
          • A 4-byte char for Color mode ('RGB ', 'Gray', 'CMYK', 'LAB ')
              note the trailing spaces
          • a variable-length number of 4-byte length floats
              this depends entirely on the color mode of the color.
          • A Big-Endian short int for either a global, spot, or process color
              global == 0, spot == 1, process == 2

    the chunk has no terminating string although other sites have indicated
    that the global/spot/process short is a terminator, it's actually used
    to indicate how illustrator should deal with the color.
    """
    title = obj['name'] + '\0'
    title_length = len(title)
    chunk = struct.pack('>H', title_length)
    chunk += title.encode('utf-16be')

    mode = obj['data']['mode'].encode()
    values = obj['data']['values']
    color_type = obj['type']

    fmt = {b'RGB': '!fff', b'Gray': '!f', b'CMYK': '!ffff', b'LAB': '!fff'}
    if mode in fmt:
        padded_mode = mode.decode().ljust(4).encode()
        chunk += struct.pack('!4s', padded_mode) # the color mode
        chunk += struct.pack(fmt[mode], *values) # the color values

    color_types = ['Global', 'Spot', 'Process']
    if color_type in color_types:
        color_int = color_types.index(color_type)
        chunk += struct.pack('>h', color_int) # append swatch mode

    chunk = struct.pack('>I', len(chunk)) + chunk # prepend the chunk size
    return b'\x00\x01' + chunk