def normalize_rgb_colors_to_hex(css):
 regex = re.compile('rgb\\s*\\(\\s*([0-9,\\s]+)\\s*\\)')
 match = regex.search(css)
 while match:
  colors = map((lambda s: s.strip()), match.group(1).split(','))
  hexcolor = ('#%.2x%.2x%.2x' % tuple(map(int, colors)))
  css = css.replace(match.group(), hexcolor)
  match = regex.search(css)
 return css

def normalize_rgb_colors_to_hex ( css ) : regex = re . compile ( 'rgb\\s*\\(\\s*([0-9,\\s]+)\\s*\\)' ) match = regex . search ( css ) while match : colors = map ( ( lambda s : s . strip ( ) ) , match . group ( 1 ) . split ( ',' ) ) hexcolor = ( '#%.2x%.2x%.2x' % tuple ( map ( int , colors ) ) ) css = css . replace ( match . group ( ) , hexcolor ) match = regex . search ( css ) return css
convert rgb to #336699 .