[英]How to check RGB colors against a color range?
I am writing a python script where I need to check colour code against 3 colours: red, yellow and green in this way: 我正在编写一个python脚本,需要在其中检查以下三种颜色的颜色代码:红色,黄色和绿色:
if (255,255,255) is in green range:
print("green")
else if (255,255,255) is in yellow range:
print("yellow")
else if (255,255,255) is in red range:
print("red")
else:
print("none")
The thing I am having trouble is with how to see if it is in yellow range
? 我遇到的麻烦是如何查看它
is in yellow range
?
Any suggestions will be appreciated. 任何建议将不胜感激。
EDIT 编辑
The following picture represents my sense of yellow, green and red: 下图代表了我对黄色,绿色和红色的感觉:
I just hacked the following solution in the terminal, not very clean but I hope you got the idea. 我只是在终端上破解了以下解决方案,虽然不是很干净,但希望您能理解。
TARGET_COLORS = {"Red": (255, 0, 0), "Yellow": (255, 255, 0), "Green": (0, 255, 0)}
def color_difference (color1, color2):
return sum([abs(component1-component2) for component1, component2 in zip(color1, color2)])
my_color = (123, 234, 100)
differences = [[color_difference(my_color, target_value), target_name] for target_name, target_value in TARGET_COLORS.items()]
differences.sort() # sorted by the first element of inner lists
my_color_name = differences[0][1]
print(my_color_name)
Figures, my_color = (123, 234, 100)
closest match was green :) 数字,
my_color = (123, 234, 100)
最接近的是绿色:)
将颜色转换为HSL,并参考像这样的色轮选择黄色,红色和绿色的定义,特别是H值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.