简体   繁体   English

如何根据颜色范围检查RGB颜色?

[英]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.

相关问题 检查色标范围内是否包含像素(RGB或HSV),如何 - Check if a pixel (RGB or HSV) is contained in a color scale range, how 如何确定给定的 RGB 颜色是否介于一系列已知颜色之间? - How can I find if a given RGB color is between a range of known colors? 如何根据一定范围的值指定rgb颜色到Contourf - How to specify rgb colors to contourf based on certain range of values 如何从 RGB 颜色列表构建 seaborn 中的颜色图? - How to construct a color map in seaborn from a list of RGB colors? 如何检查 RGB colors 列表中的所有项目是否在没有循环的图像中? - How to check if all items in list of RGB colors is in an image without a loop? 如何找到 colors 颜色的“上”和“下”范围? - How to find a colors “Upper” and “Lower” range of a color? 如何量化图像颜色从RGB 24位到RGB 6位(64色)? - How to quantize image color from RGB 24 bit to RGB 6 bit (64 colors)? 如何根据计算检查范围内的所有数字? - How to check all the numbers in a range against a calculation? 如何使用opencv python将RGB颜色范围更改为红色 - how to change a range of RGB color to Red with opencv python 如何使用 Python 检查 PNG 是否具有特定的 RGB 颜色? - How to check if a PNG has a certain RGB color using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM