2010-05-31 19:59:48 -04:00
|
|
|
from ctypes import *
|
|
|
|
from mmltypes import isiterable
|
|
|
|
from mmltypes import POINT, PPOINT, PINTEGER
|
2010-06-08 13:16:58 -04:00
|
|
|
from mmltypes import RESULT_OK, RESULT_FALSE, RESULT_ERROR
|
2010-09-12 09:01:42 -04:00
|
|
|
from mmltypes import MMLException
|
2010-05-31 19:59:48 -04:00
|
|
|
|
2010-06-12 20:10:15 -04:00
|
|
|
"""
|
|
|
|
The Color Class
|
|
|
|
---------------
|
|
|
|
|
|
|
|
This class does the color finding.
|
|
|
|
"""
|
2010-05-31 19:59:48 -04:00
|
|
|
|
2010-09-12 09:01:42 -04:00
|
|
|
class ColorException(MMLException):
|
2010-05-31 19:59:48 -04:00
|
|
|
def __init__(self, err):
|
2010-09-12 09:01:42 -04:00
|
|
|
MMLException.__init__(self, err)
|
2010-05-31 19:59:48 -04:00
|
|
|
|
2010-06-08 13:16:58 -04:00
|
|
|
|
2010-05-31 19:59:48 -04:00
|
|
|
# FIXME: Complete...
|
|
|
|
class Color(object):
|
|
|
|
'''
|
|
|
|
The Color class.
|
|
|
|
'''
|
|
|
|
|
|
|
|
_mc = None
|
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
def __init__(self, MC, cli):
|
2010-06-08 13:16:58 -04:00
|
|
|
"""
|
|
|
|
Initialise the Color object.
|
|
|
|
"""
|
2010-05-31 19:59:48 -04:00
|
|
|
self._mc = MC
|
2010-09-05 14:43:45 -04:00
|
|
|
self._cli = cli
|
2010-05-31 19:59:48 -04:00
|
|
|
self._initialiseDLLFuncs()
|
|
|
|
|
2010-06-01 07:40:51 -04:00
|
|
|
def find(self, box, color, tol = 0):
|
2010-06-08 13:16:58 -04:00
|
|
|
"""
|
|
|
|
find a color in a box, with a specific tolerance.
|
|
|
|
returns a tuple of the x, y value of a matched color.
|
|
|
|
None if no color was found.
|
|
|
|
"""
|
2010-05-31 19:59:48 -04:00
|
|
|
x, y = (c_int(-1), c_int(-1))
|
2010-06-01 07:40:51 -04:00
|
|
|
if tol is 0:
|
2010-09-05 14:43:45 -04:00
|
|
|
ret = self._mc.dll.find_color(self._cli, byref(x), byref(y),
|
|
|
|
color, *box)
|
2010-06-01 07:40:51 -04:00
|
|
|
else:
|
2010-09-05 14:43:45 -04:00
|
|
|
ret = self._mc.dll.find_color_tolerance(self._cli, byref(x),
|
|
|
|
byref(y), color, tol, *box)
|
2010-06-08 13:18:48 -04:00
|
|
|
|
|
|
|
if ret is RESULT_OK:
|
2010-06-08 13:16:58 -04:00
|
|
|
return (x, y)
|
2010-09-12 08:28:19 -04:00
|
|
|
elif ret is RESULT_ERROR:
|
|
|
|
raise ColorException(self._mc.get_last_error())
|
2010-06-08 13:16:58 -04:00
|
|
|
|
|
|
|
return None
|
2010-05-31 19:59:48 -04:00
|
|
|
|
2010-06-01 07:40:51 -04:00
|
|
|
def findAll(self, box, color, tol = 0):
|
2010-06-08 13:16:58 -04:00
|
|
|
"""
|
|
|
|
find all colors in a box, with a specific tolerance.
|
2010-06-29 12:19:18 -04:00
|
|
|
returned are all the matching points
|
2010-06-08 13:16:58 -04:00
|
|
|
"""
|
2010-09-05 14:43:45 -04:00
|
|
|
ptr, _len = PPOINT(), c_int(42)
|
2010-06-01 07:40:51 -04:00
|
|
|
if tol is 0:
|
2010-09-05 14:43:45 -04:00
|
|
|
self._mc.dll.find_colors(self._cli, byref(ptr), byref(_len),
|
|
|
|
color, *box)
|
2010-06-01 07:40:51 -04:00
|
|
|
else:
|
2010-09-05 14:43:45 -04:00
|
|
|
self._mc.dll.find_colors_tolerance(self._cli, byref(ptr),
|
|
|
|
byref(_len), color, tol, *box)
|
2010-06-01 07:40:51 -04:00
|
|
|
|
2010-09-12 09:01:42 -04:00
|
|
|
# Construct list
|
|
|
|
l = [(ptr[x].x, ptr[x].y) for x in range(_len.value)]
|
|
|
|
|
|
|
|
# Free PPOINT
|
|
|
|
self._mc.free(ptr)
|
|
|
|
|
|
|
|
return l
|
2010-06-01 06:52:32 -04:00
|
|
|
|
2010-05-31 19:59:48 -04:00
|
|
|
def _initialiseDLLFuncs(self):
|
2010-09-05 14:43:45 -04:00
|
|
|
self._mc.dll.find_color.restype = c_int
|
|
|
|
self._mc.dll.find_color.argtypes = [c_ulong, PINTEGER, PINTEGER, c_int,
|
2010-06-08 13:16:58 -04:00
|
|
|
c_int, c_int, c_int, c_int]
|
2010-09-05 14:43:45 -04:00
|
|
|
self._mc.dll.find_color_tolerance.restype = c_int
|
|
|
|
self._mc.dll.find_color_tolerance.argtypes = [c_ulong, PINTEGER,
|
|
|
|
PINTEGER, c_int, c_int, c_int, c_int, c_int, c_int]
|
|
|
|
self._mc.dll.find_colors.restype = c_int
|
|
|
|
self._mc.dll.find_colors.argtypes = [c_ulong, POINTER(PPOINT),
|
|
|
|
POINTER(c_int), c_int, c_int, c_int, c_int, c_int]
|
|
|
|
self._mc.dll.find_colors_tolerance.restype = c_int
|
|
|
|
self._mc.dll.find_colors_tolerance.argtypes = [c_ulong,
|
|
|
|
POINTER(PPOINT), POINTER(c_int), c_int, c_int,
|
2010-06-08 13:16:58 -04:00
|
|
|
c_int, c_int, c_int, c_int]
|
2010-09-12 08:28:19 -04:00
|
|
|
|