In the following, every capital letter represents some hexadecimal digit from 0 to f.
The red-green-blue color “#AABBCC” can be written as “#ABC” in shorthand. For example, “#15c” is shorthand for the color “#1155cc”.
Now, say the similarity between two colors “#ABCDEF” and “#UVWXYZ” is -(AB - UV)^2 - (CD - WX)^2 - (EF - YZ)^2.
Given the color “#ABCDEF”, return a 7 character color that is most similar to #ABCDEF, and has a shorthand (that is, it can be represented as some “#XYZ”)
题目是从[‘00’,’11’,’22’,’33’,’44’,’55’,’66’,’77’,’88’,’99’,’aa’,’bb’,’cc’,’dd’,’ee’,’ff’]的颜色组合中找出RGB距离给出的颜色最近的,如果由多组输出一组即可,组数不多直接枚举即可。
class Solution:
"""
@param color: the given color
@return: a 7 character color that is most similar to the given color
"""
def similarRGB(self, color):
# Write your code here
num1, num2, num3 = int(color[1:3], 16), int(color[3:5], 16), int(color[5:7], 16)
x,y,z = 0,0,0
num=['00','11','22','33','44','55','66','77','88','99','aa','bb','cc','dd','ee','ff']
sum =0
for i in num:
for j in num:
for k in num:
tmp = (int(i,16)-num1)**2+(int(j,16)-num2)**2+(int(k,16)-num3)**2
if tmp > sum:
sum = tmp;
x,y,z = i, j ,k
return '#' + x + y + z