Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
closest color to a list of colors in json format
import json
target = "f6efe8"
# kelly more
# curl '' -o colors.json
# behr
# 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
You can’t perform that action at this time.