Advertisement
mrlantan

Untitled

Oct 30th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. import sys
  2. import binascii
  3. import re
  4.  
  5.  
  6. def parse(line):
  7.     line = line[:-1]
  8.     if re.match('#[0-9a-f]{6}', line):
  9.         num = line[1:]
  10.         number = int('0x' + num, 0)
  11.         return int(number / 256 / 256), int(number / 256) % 256,\
  12.             number % 256
  13.     spline = line.split(', ')
  14.     match = {'r': 0, 'g': 1, 'b': 2}
  15.     if re.match('[r,g,b]{3}', line[:3]):
  16.         for i in range(3):
  17.             match[line[i]] = i
  18.         line = line[4:-1]
  19.  
  20.     spline = line.split(', ')
  21.     if len(spline) == 3:
  22.         try:
  23.             r = int(spline[0])
  24.             g = int(spline[1])
  25.             b = int(spline[2])
  26.         except Exception:
  27.             try:
  28.                 r = int(spline[0][:-1])
  29.                 g = int(spline[1][:-1])
  30.                 b = int(spline[2][:-1])
  31.             except Exception:
  32.                 raise
  33.             else:
  34.                 if r >= 0 and r <= 255 \
  35.                     and g >= 0 and g <= 255 \
  36.                         and b >= 0 and b <= 255:
  37.                             q = (r, g, b)
  38.                             return int(q[match['r']] * 255 / 100),\
  39.                                 int(q[match['g']] * 255 / 100),\
  40.                                 int(q[match['b']] * 255 / 100)
  41.                 else:
  42.                     raise Exception
  43.         else:
  44.             if r >= 0 and r <= 255 \
  45.                 and g >= 0 and g <= 255 \
  46.                     and b >= 0 and b <= 255:
  47.                         q = (r, g, b)
  48.                         return q[match['r']], q[match['g']], q[match['b']]
  49.             else:
  50.                 raise Exception
  51.  
  52. for line in sys.stdin:
  53.     try:
  54.         r, g, b = parse(line)
  55.     except Exception:
  56.         print('ERROR')
  57.     else:
  58.         print(r, g, b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement