Last active
December 14, 2015 15:29
-
-
Save fedecarg/5107967 to your computer and use it in GitHub Desktop.
Python script to find the nearest matching colour name given input R,G, B values (from 0-255).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rgb_from_str(s): | |
# s starts with a #. | |
r, g, b = int(s[1:3],16), int(s[3:5], 16),int(s[5:7], 16) | |
return r, g, b | |
def find_nearest_colour(R,G, B, colorD): | |
mindiff = None | |
for d in colorD: | |
r, g, b = rgb_from_str(colorD[d]) | |
diff = abs(R -r)*256 + abs(G-g)* 256 + abs(B- b)* 256 | |
if mindiff is None or diff < mindiff: | |
mindiff = diff | |
mincolorname = d | |
return mincolorname | |
if __name__ == "__main__": | |
D={} | |
for line in lines.split("\n"): | |
tokens = line.split() | |
if len(tokens) > 1: | |
D[tokens[0]]= tokens[1] | |
R, G, B = 0, 127, 127 | |
colorname = find_nearest_colour(0, 127, 127, D) | |
print hex(R), hex(G), hex(B) | |
print colorname, D[colorname] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment