1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-29 12:32:14 -05:00
Simba/Projects/MMLLib/pymml/mmlcolor.py

84 lines
2.4 KiB
Python
Raw Normal View History

2010-05-31 19:59:48 -04:00
from ctypes import *
from mmltypes import isiterable
from mmltypes import POINT, PPOINT, PINTEGER
from mmltypes import PascalArray
2010-06-08 13:16:58 -04:00
from mmltypes import RESULT_OK, RESULT_FALSE, RESULT_ERROR
2010-05-31 19:59:48 -04:00
"""
The Color Class
---------------
This class does the color finding.
"""
2010-05-31 19:59:48 -04:00
class ColorException(Exception):
def __init__(self, err):
Exception.__init__(self, err)
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
def __init__(self, MC):
2010-06-08 13:16:58 -04:00
"""
Initialise the Color object.
"""
2010-05-31 19:59:48 -04:00
self._mc = MC
self._initialiseDLLFuncs()
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))
if tol is 0:
ret = self._mc.dll.findColor(byref(x), byref(y), color, *box)
else:
ret = self._mc.dll.findColorTolerance(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)
return None
2010-05-31 19:59:48 -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
"""
ptr = PPOINT()
if tol is 0:
self._mc.dll.findColors(byref(ptr), color, *box)
else:
2010-06-08 13:16:58 -04:00
self._mc.dll.findColorsTolerance(byref(ptr), color, tol, *box)
arr = PascalArray(POINT, ptr, self._mc)
2010-06-08 13:16:58 -04:00
# print 'Length:', len(arr)
# for i in range(len(arr)):
# print i, arr[i].x, arr[i].y
2010-06-08 13:16:58 -04:00
# FIXME return python list?
return arr
2010-05-31 19:59:48 -04:00
def _initialiseDLLFuncs(self):
2010-05-31 20:03:25 -04:00
self._mc.dll.findColor.restype = c_int
2010-05-31 19:59:48 -04:00
self._mc.dll.findColor.argtypes = [PINTEGER, PINTEGER, c_int, c_int,
c_int, c_int, c_int]
2010-06-08 13:16:58 -04:00
self._mc.dll.findColorTolerance.restype = c_int
self._mc.dll.findColorTolerance.argtypes = [PINTEGER, PINTEGER, c_int, c_int,
c_int, c_int, c_int, c_int]
self._mc.dll.findColors.restype = c_int
self._mc.dll.findColors.argtypes = [POINTER(PPOINT), c_int, c_int,
c_int, c_int, c_int]
2010-06-08 13:16:58 -04:00
self._mc.dll.findColorsTolerance.restype = c_int
self._mc.dll.findColorsTolerance.argtypes = [POINTER(PPOINT), c_int, c_int,
c_int, c_int, c_int, c_int]