def parse_color_map(f):
color_map_config = ConfigParser()
color_map_config.readfp(f)
num_pattern = re.compile(r'(\d+)')
rgb_pattern = re.compile(r'\((\d+),(\d+),(\d+)\)')
def parse_color(s):
m = num_pattern.match(s)
if m:
x = int(m.group(1))
return (x, x, x)
else:
m = rgb_pattern.match(s)
if m:
return (int(m.group(1)), int(m.group(2)), int(m.group(3)))
raise Exception('invalid color value: {}'.format(s))
color_map = dict()
for k, v in color_map_config.items('color_map'):
color_map[parse_color(k)] = parse_color(v)
return color_map
评论列表
文章目录