Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
closest color to a list of colors in json format
#!/usr/bin/python
import json
target = "f6efe8"
# kelly more
# curl 'http://kellymoore.com/mycolorstudio-color-palette/data/colors.json' -o colors.json
# behr
# http://www.behr.com/consumer/colors/paint copy(JSON.stringify(window.colorData))
# pbpaste > colors.json
cols = json.load(open("colors.json"))
closest = None
lowest = 3000
def ComponentDiff(a, b):
return abs(int(a, 16) - int(b, 16))
def ColorDiff(col):
return (ComponentDiff(col[0:2], target[0:2]) +
ComponentDiff(col[2:4], target[2:4]) +
ComponentDiff(col[4:6], target[4:6]))
for col in cols:
# hex = col['hex'][1:] # kelly more
hex = col[14][1:] # behr
diff = ColorDiff(hex)
if diff < lowest:
lowest = diff
closest = col
print lowest, closest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment