Skip to content

Instantly share code, notes, and snippets.

@anandkunal
Created March 10, 2010 06:42
Show Gist options
  • Select an option

  • Save anandkunal/327599 to your computer and use it in GitHub Desktop.

Select an option

Save anandkunal/327599 to your computer and use it in GitHub Desktop.
from __future__ import division
from PIL import Image, ImageColor
import re
color_map = { '10':'A', '11':'B', '12':'C', '13':'D', '14':'E', '15':'F' }
def get_parts(number):
parts = re.match("^([0-9]+)\.([0-9]+)", str(number))
return (parts.group(1), parts.group(2))
def get_hex(rgb_tuple):
hex_string = ''
for color in rgb_tuple:
quotient = str(color / 16)
first_a, first_b = get_parts(quotient)
second_a, second_b = get_parts(float("0." + first_b) * 16)
if color_map.has_key(first_a):
hex_string += color_map[first_a]
else:
hex_string += first_a
if color_map.has_key(second_a):
hex_string += color_map[second_a]
else:
hex_string += second_a
return hex_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment