Created
July 2, 2018 13:54
-
-
Save lwiecek/2a74e0d302dfaede5e0b621e64c8faa0 to your computer and use it in GitHub Desktop.
Convert UIColor to HTML hex value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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