1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
1
Rep Power
0

#### Comparing 3 numbers

Hi I'm trying to create a program to tell what color is a certain pixel

I'm using RGB

So I think I got Red, Blue, Green down but for white I need to compared all three to see they are within 10 numbers.

eg.

80 87 78 would be white

100 130 0 would not

I'm going to do a longer
if red-blue >= -10 or red-blue <= 10 and blue-green >= -10 or blue-green <= 10 and green-red >= -10 or green-red <= 10:

Any better way you know would be appreciated. I'm very new to python and have searched a bit and found nothing.

Thanks for the help
2. Using absolute values would simplify it a little bit.
3. I'd an rgb triple to text using a database. Find the name associated with the best match by length of the vector between them. Here's my algorithm:
Code:
```def gapgap(A, B):
'''
>>> 2 == gapgap((1,0), (0, 1))
True
>>> 25 == gapgap((22.5+3,8.25+4), (22.5,8.25))
True
>>> 1 == gapgap((1, 0), (2, 0))
True
'''
return sum((a-b)**2 for (a, b,) in zip(A, B))

class RGB_Conversions:

'''
>>> cvt = RGB_Conversions()
>>> name = cvt( 46, 139,  87,).lower()
>>> assert(('sea' in name) and ('green' in name))
>>> name = cvt( 43, 139,  87,).lower()
>>> assert(('sea' in name) and ('green' in name))
'''

def __init__(self, RGB = '/usr/share/X11/rgb.txt'):
D = dict()
with open(RGB) as inf:
FIELDS = LINE.split()
try:
key = tuple(map(int, FIELDS[:3]))
except ValueError:
pass
else:
D[key] = ' '.join(FIELDS[3:],)
self.color_dictionary = D
self.filename = RGB

def __call__(self, r, g, b):
D = self.color_dictionary
BEST_DP = 3 * 256**2 + 1
for KEY in D:
DP = gapgap(KEY, (r, g, b))
if DP < BEST_DP:
(BEST_KEY, BEST_DP,) = (KEY, DP,)
try:
return D[BEST_KEY]
except:
pass
raise ValueError('Gamma ray burst in sector 9!  {} must be corrupt!  This cannot happen.'.format(self.filename))```
And to run doctest:
\$ python3 -m doctest p.py