'Check if a color is an interpolation of other two colors
Here is my current code:
from PIL import ImageColor
import numpy as np
a=0
colors = [
([17, 15, 100], [50, 56, 200]),
([86, 31, 4], [220, 88, 50]),
([25, 146, 190], [62, 174, 250]),
([103, 86, 65], [145, 133, 128])
]
for (low, high) in colors:
low = np.array(low, dtype="uint8")
high = np.array(high, dtype="uint8")
#if np.array(ImageColor.getcolor('#300103', "RGB"), dtype="uint8") is in between low and high, a=1
and I want to know if a certain color is in between two colors, sort of like cv2.inRange(), but instead of seeing if a picture's pixels are in between two colors and keeping them in the picture if so, I want to see if a single color is inbetween two colors.
Solution 1:[1]
A color "in between" two others is a bit problematic, because there is not a single definition for that: if vary colors in the RGB space, as is, in practice, defined in your question, it is a matter of school mathematics: taking the first component (red), check the relative distance between the two colors at the ends (50 - 17 = 33, for your first line), pick the red component of the color being checked, see its distance to the lower of those - for the color #300103, the red component would be "48" in decimal - 48 - 17 = 31. use the factor 31/33, and check if the other 2 components are at the same proportional distance from the respective component in (17, 15, 100).
One might use math.close and allow a fluctuation of 1 or 2 units per component, due to rounding errors.
However, if you might have a set of colors that lie on the gray spectrum attending this criteria, and which have no resemblance to the colors at either edge. (say, if you start with (255, 0, 0) and (0, 0, 255), (128, 0, 128) will match, but it has half the saturation than either color.
Using an HSV color space, rather than RGB would probably get you to colors that perceptually attend better as "in between". But then without knowing your real objective it is hard to say if it makes any sense - for the example above, it would be even worse, as it would match green and other full saturated colors.
I will beg your pardon, but I won't fill in the exact code for the RGB interpolation here, it should not be hard - but it will take sometime to get right - and it might not work for you at all.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jsbueno |
