Skip to content

Instantly share code, notes, and snippets.

@champierre
Created December 19, 2012 14:41
Show Gist options
  • Select an option

  • Save champierre/4337114 to your computer and use it in GitHub Desktop.

Select an option

Save champierre/4337114 to your computer and use it in GitHub Desktop.
Get the brightness of hex color
# e.g.
# brightness("#ffffff") = (255/255.0 + 255/255.0 + 255/255.0)/3.0 = 1.0
# brightness("#000000") = (0/255.0 + 0/255.0 + 0/255.0)/3.0 = 0.0
# brightness("#666666") = (102/255.0 + 102/255.0 + 102/255.0)/3.0 = 0.4
def brightness(color):
rgb = struct.unpack('BBB', color[1:].decode('hex'))
brightness = 0
for x in rgb:
brightness += x / 255.0
return brightness / 3.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment