def html5_parse_legacy_color(input):
<EOL>if not isinstance(input, unicode):<EOL><INDENT>raise ValueError(<EOL>u"<STR_LIT>"<EOL>)<EOL><DEDENT>if input == "<STR_LIT>":<EOL><INDENT>raise ValueError(<EOL>u"<STR_LIT>"<EOL>)<EOL><DEDENT>input = input.strip()<EOL>if input.lower() == u"<STR_LIT>":<EOL><INDENT>raise ValueError(<EOL>u'<STR_LIT>'<EOL>)<EOL><DEDENT>keyword_hex = CSS3_NAMES_TO_HEX.get(input.lower())<EOL>if keyword_hex is not None:<EOL><INDENT>return html5_parse_simple_color(keyword_hex)<EOL><DEDENT>if len(input) == <NUM_LIT:4> andinput.startswith(u'<STR_LIT:#>') andall(c in string.hexdigits for c in input[<NUM_LIT:1>:]):<EOL><INDENT>result = HTML5SimpleColor(<EOL>int(input[<NUM_LIT:1>], <NUM_LIT:16>) * <NUM_LIT>,<EOL>int(input[<NUM_LIT:2>], <NUM_LIT:16>) * <NUM_LIT>,<EOL>int(input[<NUM_LIT:3>], <NUM_LIT:16>) * <NUM_LIT><EOL>)<EOL>return result<EOL><DEDENT>encoded_input = input.encode('<STR_LIT>')<EOL>format_string = '<STR_LIT:<>' + ('<STR_LIT:L>' * (int(len(encoded_input) / <NUM_LIT:4>)))<EOL>codepoints = struct.unpack(format_string, encoded_input)<EOL>input = '<STR_LIT>'.join(u'<STR_LIT>' if c > <NUM_LIT><EOL>else unichr(c)<EOL>for c in codepoints)<EOL>if len(input) > <NUM_LIT>:<EOL><INDENT>input = input[:<NUM_LIT>]<EOL><DEDENT>if input.startswith(u'<STR_LIT:#>'):<EOL><INDENT>input = input[<NUM_LIT:1>:]<EOL><DEDENT>if any(c for c in input if c not in string.hexdigits):<EOL><INDENT>input = '<STR_LIT>'.join(c if c in string.hexdigits else u'<STR_LIT:0>' for c in input)<EOL><DEDENT>while (len(input) == <NUM_LIT:0>) or (len(input) % <NUM_LIT:3> != <NUM_LIT:0>):<EOL><INDENT>input += u'<STR_LIT:0>'<EOL><DEDENT>length = int(len(input) / <NUM_LIT:3>)<EOL>red = input[:length]<EOL>green = input[length:length*<NUM_LIT:2>]<EOL>blue = input[length*<NUM_LIT:2>:]<EOL>if length > <NUM_LIT:8>:<EOL><INDENT>red, green, blue = (red[length-<NUM_LIT:8>:],<EOL>green[length-<NUM_LIT:8>:],<EOL>blue[length-<NUM_LIT:8>:])<EOL>length = <NUM_LIT:8><EOL><DEDENT>while (length > <NUM_LIT:2>) and (red[<NUM_LIT:0>] == u'<STR_LIT:0>' and<EOL>green[<NUM_LIT:0>] == u'<STR_LIT:0>' and<EOL>blue[<NUM_LIT:0>] == u'<STR_LIT:0>'):<EOL><INDENT>red, green, blue = (red[<NUM_LIT:1>:],<EOL>green[<NUM_LIT:1>:],<EOL>blue[<NUM_LIT:1>:])<EOL>length -= <NUM_LIT:1><EOL><DEDENT>if length > <NUM_LIT:2>:<EOL><INDENT>red, green, blue = (red[:<NUM_LIT:2>],<EOL>green[:<NUM_LIT:2>],<EOL>blue[:<NUM_LIT:2>])<EOL><DEDENT>return HTML5SimpleColor(<EOL>int(red, <NUM_LIT:16>),<EOL>int(green, <NUM_LIT:16>),<EOL>int(blue, <NUM_LIT:16>)<EOL>)<EOL>
Apply the legacy color parsing algorithm from section 2.4.6 of
HTML5.