19 lines
822 B
Python
19 lines
822 B
Python
'''
|
|
based on: http://24ways.org/2010/calculating-color-contrast/
|
|
accepts hex colors with 3 or 6 chars. hash-char is optional.
|
|
'''
|
|
def get_contrast_yiq(hex_color):
|
|
hex_color = hex_color.replace("#", "")
|
|
|
|
if len(hex_color) == 3:
|
|
hex_color = "".join([c + c for c in hex_color])
|
|
|
|
r = int(hex_color[0:2], 16)
|
|
g = int(hex_color[2:4], 16)
|
|
b = int(hex_color[4:6], 16)
|
|
|
|
yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000
|
|
|
|
if yiq >= 128:
|
|
return 'black'
|
|
return 'white'
|