Skip to content

Instantly share code, notes, and snippets.

@lwiecek
Created July 2, 2018 13:54
Show Gist options
  • Save lwiecek/2a74e0d302dfaede5e0b621e64c8faa0 to your computer and use it in GitHub Desktop.
Save lwiecek/2a74e0d302dfaede5e0b621e64c8faa0 to your computer and use it in GitHub Desktop.
Convert UIColor to HTML hex value
import re
def uicolor2hex(s):
""" Convert UIColor to HTML hex value.
I thought invisionapp only supports this format but eventually found the switch in settings.
Leaving it here in case someone needs it.
uicolor2hex('UIColor(red:0.08, green:0.14, blue:0.24, alpha:1)') == '#14233dff'
"""
m = re.match('UIColor\(red:(.*), green:(.*), blue:(.*), alpha:(.*)\)', s)
r, g, b, a = m.groups()
def hex_value(c):
return '{:x}'.format(int(round(float(c) * 255))).zfill(2)
return '#{}{}{}{}'.format(hex_value(r), hex_value(g), hex_value(b), hex_value(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment