Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import binascii
- import re
- def parse(line):
- line = line[:-1]
- if re.match('#[0-9a-f]{6}', line):
- num = line[1:]
- number = int('0x' + num, 0)
- return int(number / 256 / 256), int(number / 256) % 256,\
- number % 256
- spline = line.split(', ')
- match = {'r': 0, 'g': 1, 'b': 2}
- if re.match('[r,g,b]{3}', line[:3]):
- for i in range(3):
- match[line[i]] = i
- line = line[4:-1]
- spline = line.split(', ')
- if len(spline) == 3:
- try:
- r = int(spline[0])
- g = int(spline[1])
- b = int(spline[2])
- except Exception:
- try:
- r = int(spline[0][:-1])
- g = int(spline[1][:-1])
- b = int(spline[2][:-1])
- except Exception:
- raise
- else:
- if r >= 0 and r <= 255 \
- and g >= 0 and g <= 255 \
- and b >= 0 and b <= 255:
- q = (r, g, b)
- return int(q[match['r']] * 255 / 100),\
- int(q[match['g']] * 255 / 100),\
- int(q[match['b']] * 255 / 100)
- else:
- raise Exception
- else:
- if r >= 0 and r <= 255 \
- and g >= 0 and g <= 255 \
- and b >= 0 and b <= 255:
- q = (r, g, b)
- return q[match['r']], q[match['g']], q[match['b']]
- else:
- raise Exception
- for line in sys.stdin:
- try:
- r, g, b = parse(line)
- except Exception:
- print('ERROR')
- else:
- print(r, g, b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement