From 8bd3fd16b9d85ee758f95a565ba275087b339f81 Mon Sep 17 00:00:00 2001 From: Merlijn Wajer Date: Thu, 9 Dec 2010 13:14:12 +0100 Subject: [PATCH] pyMML: Patch by iluffsyeww for color.py --- Projects/libmml/pymml/mml/color.py | 185 +++++++++++++++++++++++++++-- 1 file changed, 176 insertions(+), 9 deletions(-) diff --git a/Projects/libmml/pymml/mml/color.py b/Projects/libmml/pymml/mml/color.py index bcc050d..5b2e5bf 100644 --- a/Projects/libmml/pymml/mml/color.py +++ b/Projects/libmml/pymml/mml/color.py @@ -30,8 +30,21 @@ class Color(object): """ self._mc = MC self._cli = cli - self._initialiseDLLFuncs() + self._initialise_dll_funcs() + def get(self, pt): + """ + Gets color at pt[0], pt[1]. + Yields integer. + """ + col = c_int(-1) + self._mc.dll.get_color(self._cli, pt[0], pt[1], byref(col)) + if col is RESULT_OK: + return col + elif ret is RESULT_ERROR: + raise ColorException(self._mc.get_last_error()) + return None + def find(self, box, color, tol = 0): """ find a color in a box, with a specific tolerance. @@ -53,7 +66,7 @@ class Color(object): return None - def findAll(self, box, color, tol = 0): + def find_all(self, box, color, tol = 0): """ find all colors in a box, with a specific tolerance. returned are all the matching points @@ -73,19 +86,173 @@ class Color(object): self._mc.free(ptr) return l - - def _initialiseDLLFuncs(self): + + def find_spiral(self, col, box, tol = 0): + """ + Find a color in a box, searching in the direction of a spiral, with a + specific tolerance. + Yields a tuple of x, y values of found color. + """ + x, y = (c_int(-1), c_int(-1)) + if tol is 0: + ret = self._mc.dll.find_color_spiral(self._cli, byref(x), byref(y), + col, *box) + else: + ret = self._mc.dll.find_color_spiral_tolerance(self._cli, byref(x), + byref(y), col, *box, + tol) + if ret is RESULT_OK: + return (x, y) + elif ret is RESULT_ERROR: + raise ColorException(self._mc.get_last_error()) + return None + + def find_area(self, col, box, min_a, tol = 0): + """ + Finds a colored area in box with min area min_a with a specific + tolerance. + Yields a tuple of x, y values of found area. + """ + x, y = (c_int(-1), c_int(-1)) + if tol is 0: + ret = self._mc.dll.find_colored_area(self._cli, byref(x), byref(y), + col, *box, min_a) + else: + ret = self._mc.dll.find_colored_area_tolerance(self._cli, byref(x), + byref(y), col, *box, + min_a, tol) + if ret is RESULT_OK: + return (x, y) + elif ret is RESULT_ERROR: + raise ColorException(self._mc.get_last_error()) + return None + + def count_color(self, count, col, box, tol = 0): + """ + Counts color col in box with tol. + Yields integer of count. + """ + count = 0 + if tol is 0: + ret = self._mc.dll.count_color(self._cli, count, col, *box) + else: + ret = self._mc.dll.count_color_tolerance(self._cli, count, col, + *box, tol) + if ret is RESULT_OK: + return count + elif ret is RESULT_ERROR: + raise ColorException(self._mc.get_last_error()) + return None + + def similar_colors(self, col1, col2, tol = 0): + """ + Compares col1 and col2 with tol. + Yields boolean + """ + ret = self._mc.dll.similar_colors(col1, col2, tol) + if ret is RESULT_OK: + return True + else: + return False + + def set_tolerance_speed(self, ncts = 0): + """ + Sets CTS to ncts. + """ + self._mc.dll.set_tolerance_speed(self._cli, ncts) + + def get_tolerance_speed(self): + """ + Gets CTS. + Yields CTS. + """ + self._mc.dll.get_tolerance_speed(self._cli, out_cts) + return out_cts + + def set_tolerance_speed_2_modifiers(self, hue = 0, sat = 0): + """ + Sets CTS2 modifiers with hue, sat. + """ + self._mc.dll.set_tolerance_speed_2_modifiers(self._cli, hue, sat) + + def get_tolerance_speed_2_modifiers(self): + """ + Gets CTS2 modifiers. + Yields tuple of hue and sat mods. + """ + self._mc.dll.get_tolerance_speed_2_modifiers(self._cli, h, s) + return (h, s) + + def _initialise_dll_funcs(self): self._mc.dll.find_color.restype = c_int self._mc.dll.find_color.argtypes = [c_ulong, PINTEGER, PINTEGER, c_int, - c_int, c_int, c_int, c_int] + c_int, c_int, c_int, c_int] + 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] + 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] + 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, - c_int, c_int, c_int, c_int] + POINTER(PPOINT), + POINTER(c_int), c_int, + c_int, c_int, c_int, + c_int, c_int] + self._mc.dll.find_color_spiral.restype = c_int + self._mc.dll.find_color_spiral.argtypes = [c_ulong, PINTEGER, PINTEGER, + c_int, c_int, c_int, c_int, + c_int] + + self._mc.dll.find_color_spiral_tolerance.restype = c_int + self._mc.dll.find_color_spiral_tolerance.argtypes = [c_ulong, PINTEGER, + PINTEGER, c_int, + c_int, c_int, + c_int, c_int, + c_int] + + self._mc.dll.find_colored_area.restype = c_int + self._mc.dll.find_colored_area.argtypes = [c_ulong, PINTEGER, PINTEGER, + c_int, c_int, c_int, c_int, + c_int, c_int] + + self._mc.dll.find_colored_area_tolerance.restype = c_int + self._mc.dll.find_colored_area_tolerance.argtypes = [c_ulong, PINTEGER, + PINTEGER, c_int, + c_int, c_int, + c_int, c_int, + c_int, c_int] + + self._mc.dll.count_color.restype = c_int + self._mc.dll.count_color.argtypes = [c_ulong, PINTEGER, c_int, c_int, + c_int, c_int, c_int] + + self._mc.dll.count_color_tolerance.restype = c_int + self._mc.dll.count_color_tolerance.argtypes = [c_ulong, PINTEGER, + c_int, c_int, c_int, + c_int, c_int, c_int] + + self._mc.dll.similar_colors.restype = c_int + self._mc.dll.similar_colors.argtypes = [c_ulong, c_int, c_int, c_int] + + self._mc.dll.set_tolerance_speed.restype = c_int + self._mc.dll.set_tolerance_speed.argtypes = [c_ulong, c_int] + + self._mc.dll.get_tolerance_speed.restype = c_int + self._mc.dll.get_tolerance_speed.argtypes = [c_ulong, PINTEGER] + + self._mc.dll.set_tolerance_speed_2_modifiers.restype = c_int + self._mc.dll.set_tolerance_speed_2_modifiers.argtypes = [c_ulong, + c_int, c_int] + + self._mc.dll.get_tolerance_speed_2_modifiers.restype = c_int + self._mc.dll.get_tolerance_speed_2_modifiers.argtypes = [c_ulong, + PINTEGER, + PINTEGER] \ No newline at end of file