SRL Archive - MML Library Development

lib/pyMML color methods.


i luffs yeww

2010-12-02 07:07:04


diff --git a/Projects/libmml/libmml.lpr b/Projects/libmml/libmml.lpr
index 5639350..2c14f68 100644
--- a/Projects/libmml/libmml.lpr
+++ b/Projects/libmml/libmml.lpr
@@ -235,25 +235,31 @@ end;

{ Colour }

-function get_color(C: TClient; x, y: integer; var color: integer): integer;
+function get_color(C: TClient; x, y: Integer;
+ out color: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
-
try
- color := C.IOManager.GetColor(x, y);
- except on e : exception do
begin
+ color := C.IOManager.GetColor(x, y);
+ if color > -1 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ end;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
result := RESULT_ERROR;
- set_last_error(e.Message);
end;
end;
end;

-{ Find color on client C in area (x1,y1,x2,y2) and return coordinate (if any) in x, y }
-function find_color(C: TClient; var x, y: integer; color, x1, y1, x2, y2: integer): integer; cdecl;
+function find_color(C: TClient; var x, y: Integer;
+ color, x1, y1, x2, y2: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
@@ -267,14 +273,14 @@ begin
result := RESULT_FALSE;
except on e : Exception do
begin
- result := RESULT_ERROR;
set_last_error(e.Message);
+ result := RESULT_ERROR;
end;
end;
end;

-
-function find_color_tolerance(C: TClient; var x, y: integer; color, tol, x1, y1, x2, y2: integer): integer; cdecl;
+function find_color_tolerance(C: TClient; var x, y: Integer; color: Integer;
+ tol, x1, y1, x2, y2: Integer): Integer; cdecl;

begin
if not validate_client(C) then
@@ -289,13 +295,37 @@ begin
result := RESULT_FALSE;
except on e : Exception do
begin
- result := RESULT_ERROR;
set_last_error(e.Message);
+ result := RESULT_ERROR;
end;
end;
end;

-function find_colors(C: TClient; var ptr: PPoint; var len: integer; color, x1, y1, x2, y2: integer): integer; cdecl;
+function find_color_tolerance_optimised(C: TClient; var x, y: Integer;
+ var len: Integer; col: Integer;
+ x1, y1, x2, y2: Integer;
+ tol: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ if C.MFinder.FindColorToleranceOptimised(x, y, col, x1, y1, x2, y2,
+ tol) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function find_colors(C: TClient; var ptr: PPoint; var len: Integer;
+ color, x1, y1, x2, y2: Integer): Integer; cdecl;
var
TPA: TPointArray;
begin
@@ -309,19 +339,22 @@ begin
C.MFinder.FindColors(TPA, color, x1, y1, x2, y2);
except on e : Exception do
begin
- result := RESULT_ERROR;
set_last_error(e.Message);
+ result := RESULT_ERROR;
end;
end;

len := Length(TPA);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
- result := RESULT_OK;
+ if len > 0 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
setlength(tpa, 0);
end;

function find_colors_tolerance(C: TClient; var ptr: PPoint; var len: Integer;
- color, tol, x1, y1, x2, y2: integer): integer; cdecl;
+ color, tol, x1, y1, x2, y2: Integer): Integer; cdecl;
var
TPA: TPointArray;
begin
@@ -334,13 +367,248 @@ begin
C.MFinder.FindColorsTolerance(TPA, color, x1, y1, x2, y2, tol);
except on e : Exception do
begin
- result := RESULT_ERROR;
set_last_error(e.Message);
+ result := RESULT_ERROR;
+ end;
+ end;
+
+ len := Length(TPA);
+ ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
+ if len > 0 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ setlength(TPA, 0);
+end;
+
+function find_colors_tolerance_optimised(C: TClient; var ptr: PPoint;
+ var len: Integer; col: Integer;
+ x1, y1, x2, y2: Integer;
+ tol: Integer): Integer; cdecl;
+var
+ TPA: TPointArray;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ C.MFinder.FindColorsToleranceOptimised(TPA, col, x1, y1, x2, y2, tol);
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
end;
end;

len := Length(TPA);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
+ if len > 0 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+end;
+
+function similar_colors(C: TClient; col1, col2, tol: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ if C.MFinder.SimilarColors(col1, col2, tol) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+end;
+
+function count_color(C: TClient; out count: Integer;
+ Color, xs, ys, xe, ye: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ begin
+ count := C.MFinder.CountColor(Color, xs, ys, xe, ye);
+ if count > 0 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ end;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function count_color_tolerance(C: TClient; out count: Integer; col: Integer;
+ xs, ys, xe, ye, tol: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ count := C.MFinder.CountColorTolerance(col, xs, ys, xe, ye, tol);
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+ if count > 0 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+end;
+
+function find_color_spiral(C: TClient; var x, y: Integer;
+ col, xs, ys, xe, ye: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ if C.MFinder.FindColorSpiral(x, y, col, xs, ys, xe, ye) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function find_color_spiral_tolerance(C: TClient; var x, y: Integer;
+ col, xs, ys, xe, ye: Integer;
+ tol: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ if C.MFinder.FindColorSpiralTolerance(x, y, col, xs, ys, xe, ye, tol) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function find_colored_area(C: TClient; var x, y: Integer;
+ col, xs, ys, xe, ye, minA: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ if C.MFinder.FindColoredArea(x, y, col, xs, ys, xe, ye, minA) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function find_colored_area_tolerance(C: TClient; var x, y: Integer;
+ col, xs, ys, xe, ye, minA: Integer;
+ tol: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ if C.MFinder.FindColoredAreaTolerance(x, y, col,
+ xs, ys, xe, ye, minA, tol) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function set_tolerance_speed(C: TClient; nCTS: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ begin
+ C.MFinder.SetToleranceSpeed(nCTS);
+ result := RESULT_OK;
+ end;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function get_tolerance_speed(C: TClient; out cts: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ cts := C.MFinder.GetToleranceSpeed;
+ result := RESULT_OK;
+end;
+
+function set_tolerance_speed_2_modifiers(C: TClient;
+ nHue, nSat: Extended): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ begin
+ C.MFinder.SetToleranceSpeed2Modifiers(nHue, nSat);
+ result := RESULT_OK;
+ end;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function get_tolerance_speed_2_modifiers(C: TClient; out hueMod: Extended;
+ out satMod: Extended): Integer; cdecl;
+var
+ h, s: Extended;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ C.MFinder.GetToleranceSpeed2Modifiers(h, s);
+ hueMod := h;
+ satMod := s;
result := RESULT_OK;
end;


diff --git a/Projects/libmml/pymml/mmlcolor.py b/Projects/libmml/pymml/mmlcolor.py
index d620c68..7b8626c 100644
--- a/Projects/libmml/pymml/mmlcolor.py
+++ b/Projects/libmml/pymml/mmlcolor.py
@@ -30,9 +30,22 @@ class Color(object):
"""
self._mc = MC
self._cli = cli
- self._initialiseDLLFuncs()
+ self._initialise_dll_funcs()

- def find(self, box, color, tol = 0):
+ 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, color, box, tol = 0):
"""
find a color in a box, with a specific tolerance.
returns a tuple of the x, y value of a matched color.
@@ -53,7 +66,7 @@ class Color(object):

return None

- def findAll(self, box, color, tol = 0):
+ def find_all(self, color, box, 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]

Haven't tested anything for either, but they should work.

Nava2

2010-12-02 09:51:01

Doesn't look like this:
         self._mc = MC
self._cli = cli
- self._initialiseDLLFuncs()
+ self._initialise_dll_funcs()

Will compile. :P

Looks awesome though! Well done.

Wizzup?

2010-12-02 10:02:09

From: Nava2
Doesn't look like this:
         self._mc = MC
self._cli = cli
- self._initialiseDLLFuncs()
+ self._initialise_dll_funcs()

Will compile. :P

Looks awesome though! Well done.


It probably will if he renames the other function too, which he did.

Wizzup?

2010-12-02 10:09:04

In get_color you use find_color. Also, you always need to check for the result returned. You don't do this in get_color.

i luffs yeww

2010-12-02 10:24:43

I'm sure there are some mistakes. :p I still didn't sleep but I'm on my way to school. I was gonna look over it again after a nap.

Nava2

2010-12-02 10:26:18

From: i luffs yeww
I'm sure there are some mistakes. :p I still didn't sleep but I'm on my way to school. I was gonna look over it again after a nap.


My comment was more about the spacing, not the actual names. :)

i luffs yeww

2010-12-02 13:27:43

Uhh.. The spacing's fine. :p Cuntpaste that into gedit (:3 I know you're a KDE fan) and it is fine. It's just that it's not correct spacing in the forum. :< I wish that'd be fixed soon.

Nava2

2010-12-02 13:31:43

From: i luffs yeww
Uhh.. The spacing's fine. :p Cuntpaste that into gedit (:3 I know you're a KDE fan) and it is fine. It's just that it's not correct spacing in the forum. :< I wish that'd be fixed soon.


I didn't really check it, I forgot that the highlight tags haven't been monospaced yet. :<

Also, as far as text editors go, I use Kate for that stuff (copy, paste, etc). vim for anything else.. (vim's c&p buffer blows in several respects. :().

i luffs yeww

2010-12-02 13:42:57

I've actually been wanting to try out vim (I've tried it but didn't do much; I was confus). I saw a short video Wizzup? posted showing it do like everything for him and it was awesome. :3

And to make this not entirely spam, I changed get_color in pyMML to work (called find_color instead of get_color :x it was the last one I did this morning around 5:30 and I was quite tired. Also adding one other check in get_color; might be about an hour and a half since the editing sometimes screws everything up in FF :/) (or it should, anyway). Thanks for pointing that out, Wizzup?. :)

Weekend's coming up so I'll do as much work as I can on whatever I can. I've also a much better understanding of how this all works, so I think I'll be able to tackle SPyRL much more effectively. ;) I'd like to get a full script running with SMART and SPyRL and everything by the end of this weekend (I have tomorrow off, too). With as much as I can possibly add. Hopefully reflection (bits and pieces) as well!

Wizzup?

2010-12-02 14:06:23

From: Nava2
(vim's c&p buffer blows in several respects. :().


How? Perhaps you don't know :setpaste ?

i luffs yeww

2010-12-02 15:39:53

Fixed Color.get completely. :)

i luffs yeww

2010-12-03 08:09:58


diff --git a/Projects/libmml/libmml.lpr b/Projects/libmml/libmml.lpr
index 5639350..2c14f68 100644
--- a/Projects/libmml/libmml.lpr
+++ b/Projects/libmml/libmml.lpr
@@ -235,25 +235,31 @@ end;

{ Colour }

-function get_color(C: TClient; x, y: integer; var color: integer): integer;
+function get_color(C: TClient; x, y: Integer;
+ out color: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
-
try
- color := C.IOManager.GetColor(x, y);
- except on e : exception do
begin
+ color := C.IOManager.GetColor(x, y);
+ if color > -1 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ end;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
result := RESULT_ERROR;
- set_last_error(e.Message);
end;
end;
end;

-{ Find color on client C in area (x1,y1,x2,y2) and return coordinate (if any) in x, y }
-function find_color(C: TClient; var x, y: integer; color, x1, y1, x2, y2: integer): integer; cdecl;
+function find_color(C: TClient; var x, y: Integer;
+ color, x1, y1, x2, y2: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
@@ -267,14 +273,14 @@ begin
result := RESULT_FALSE;
except on e : Exception do
begin
- result := RESULT_ERROR;
set_last_error(e.Message);
+ result := RESULT_ERROR;
end;
end;
end;

-
-function find_color_tolerance(C: TClient; var x, y: integer; color, tol, x1, y1, x2, y2: integer): integer; cdecl;
+function find_color_tolerance(C: TClient; var x, y: Integer; color: Integer;
+ tol, x1, y1, x2, y2: Integer): Integer; cdecl;

begin
if not validate_client(C) then
@@ -289,13 +295,37 @@ begin
result := RESULT_FALSE;
except on e : Exception do
begin
- result := RESULT_ERROR;
set_last_error(e.Message);
+ result := RESULT_ERROR;
end;
end;
end;

-function find_colors(C: TClient; var ptr: PPoint; var len: integer; color, x1, y1, x2, y2: integer): integer; cdecl;
+function find_color_tolerance_optimised(C: TClient; var x, y: Integer;
+ var len: Integer; col: Integer;
+ x1, y1, x2, y2: Integer;
+ tol: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ if C.MFinder.FindColorToleranceOptimised(x, y, col, x1, y1, x2, y2,
+ tol) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function find_colors(C: TClient; var ptr: PPoint; var len: Integer;
+ color, x1, y1, x2, y2: Integer): Integer; cdecl;
var
TPA: TPointArray;
begin
@@ -309,19 +339,22 @@ begin
C.MFinder.FindColors(TPA, color, x1, y1, x2, y2);
except on e : Exception do
begin
- result := RESULT_ERROR;
set_last_error(e.Message);
+ result := RESULT_ERROR;
end;
end;

len := Length(TPA);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
- result := RESULT_OK;
+ if len > 0 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
setlength(tpa, 0);
end;

function find_colors_tolerance(C: TClient; var ptr: PPoint; var len: Integer;
- color, tol, x1, y1, x2, y2: integer): integer; cdecl;
+ color, tol, x1, y1, x2, y2: Integer): Integer; cdecl;
var
TPA: TPointArray;
begin
@@ -334,13 +367,248 @@ begin
C.MFinder.FindColorsTolerance(TPA, color, x1, y1, x2, y2, tol);
except on e : Exception do
begin
- result := RESULT_ERROR;
set_last_error(e.Message);
+ result := RESULT_ERROR;
+ end;
+ end;
+
+ len := Length(TPA);
+ ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
+ if len > 0 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ setlength(TPA, 0);
+end;
+
+function find_colors_tolerance_optimised(C: TClient; var ptr: PPoint;
+ var len: Integer; col: Integer;
+ x1, y1, x2, y2: Integer;
+ tol: Integer): Integer; cdecl;
+var
+ TPA: TPointArray;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ C.MFinder.FindColorsToleranceOptimised(TPA, col, x1, y1, x2, y2, tol);
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
end;
end;

len := Length(TPA);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
+ if len > 0 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+end;
+
+function similar_colors(C: TClient; col1, col2, tol: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ if C.MFinder.SimilarColors(col1, col2, tol) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+end;
+
+function count_color(C: TClient; out count: Integer;
+ Color, xs, ys, xe, ye: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ begin
+ count := C.MFinder.CountColor(Color, xs, ys, xe, ye);
+ if count > 0 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ end;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function count_color_tolerance(C: TClient; out count: Integer; col: Integer;
+ xs, ys, xe, ye, tol: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ count := C.MFinder.CountColorTolerance(col, xs, ys, xe, ye, tol);
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+ if count > 0 then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+end;
+
+function find_color_spiral(C: TClient; var x, y: Integer;
+ col, xs, ys, xe, ye: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ if C.MFinder.FindColorSpiral(x, y, col, xs, ys, xe, ye) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function find_color_spiral_tolerance(C: TClient; var x, y: Integer;
+ col, xs, ys, xe, ye: Integer;
+ tol: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ if C.MFinder.FindColorSpiralTolerance(x, y, col, xs, ys, xe, ye, tol) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function find_colored_area(C: TClient; var x, y: Integer;
+ col, xs, ys, xe, ye, minA: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ if C.MFinder.FindColoredArea(x, y, col, xs, ys, xe, ye, minA) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function find_colored_area_tolerance(C: TClient; var x, y: Integer;
+ col, xs, ys, xe, ye, minA: Integer;
+ tol: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ if C.MFinder.FindColoredAreaTolerance(x, y, col,
+ xs, ys, xe, ye, minA, tol) then
+ result := RESULT_OK
+ else
+ result := RESULT_FALSE;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function set_tolerance_speed(C: TClient; nCTS: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ begin
+ C.MFinder.SetToleranceSpeed(nCTS);
+ result := RESULT_OK;
+ end;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function get_tolerance_speed(C: TClient; out cts: Integer): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ cts := C.MFinder.GetToleranceSpeed;
+ result := RESULT_OK;
+end;
+
+function set_tolerance_speed_2_modifiers(C: TClient;
+ nHue, nSat: Extended): Integer; cdecl;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ try
+ begin
+ C.MFinder.SetToleranceSpeed2Modifiers(nHue, nSat);
+ result := RESULT_OK;
+ end;
+ except on e : Exception do
+ begin
+ set_last_error(e.message);
+ result := RESULT_ERROR;
+ end;
+ end;
+end;
+
+function get_tolerance_speed_2_modifiers(C: TClient; out hueMod: Extended;
+ out satMod: Extended): Integer; cdecl;
+var
+ h, s: Extended;
+begin
+ if not validate_client(C) then
+ begin
+ exit(RESULT_ERROR);
+ end;
+ C.MFinder.GetToleranceSpeed2Modifiers(h, s);
+ hueMod := h;
+ satMod := s;
result := RESULT_OK;
end;

Wizzup?

2010-12-03 08:30:42

Merged in master.

i luffs yeww

2010-12-03 08:40:21

:< Didn't update pyMML.

Wizzup?

2010-12-04 05:45:46

You didn't use the latest pymml version. :(

i luffs yeww

2010-12-04 13:21:31

diff --git a/Projects/libmml/pymml/mml/color.py b/Projects/libmml/pymml/mml/color.py
index bcc050d..7b45732 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]

http://paste.villavu.com/show/151/

^ lodgeit. Just in case.

And, just even more in case..


from ctypes import *
from mtypes import isiterable
from mtypes import POINT, PPOINT, PINTEGER
from mtypes import RESULT_OK, RESULT_FALSE, RESULT_ERROR
from mtypes import MMLException

"""
The Color Class
---------------

This class does the color finding.
"""

class ColorException(MMLException):
def __init__(self, err):
MMLException.__init__(self, err)


# FIXME: Complete...
class Color(object):
'''
The Color class.
'''

_mc = None

def __init__(self, MC, cli):
"""
Initialise the Color object.
"""
self._mc = MC
self._cli = cli
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.
returns a tuple of the x, y value of a matched color.
None if no color was found.
"""
x, y = (c_int(-1), c_int(-1))
if tol is 0:
ret = self._mc.dll.find_color(self._cli, byref(x), byref(y),
color, *box)
else:
ret = self._mc.dll.find_color_tolerance(self._cli, byref(x),
byref(y), color, tol, *box)

if ret is RESULT_OK:
return (x, y)
elif ret is RESULT_ERROR:
raise ColorException(self._mc.get_last_error())

return None

def find_all(self, box, color, tol = 0):
"""
find all colors in a box, with a specific tolerance.
returned are all the matching points
"""
ptr, _len = PPOINT(), c_int(42)
if tol is 0:
self._mc.dll.find_colors(self._cli, byref(ptr), byref(_len),
color, *box)
else:
self._mc.dll.find_colors_tolerance(self._cli, byref(ptr),
byref(_len), color, tol, *box)

# Construct list
l = [(ptr[x].x, ptr[x].y) for x in range(_len.value)]

# Free PPOINT
self._mc.free(ptr)

return l

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]

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, 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]

http://paste.villavu.com/show/152/

^ color.py.

:p You never know.

i luffs yeww

2010-12-09 02:26:55


from ctypes import *
from mtypes import isiterable
from mtypes import POINT, PPOINT, PINTEGER
from mtypes import RESULT_OK, RESULT_FALSE, RESULT_ERROR
from mtypes import MMLException

"""
The Color Class
---------------

This class does the color finding.
"""

class ColorException(MMLException):
def __init__(self, err):
MMLException.__init__(self, err)


# FIXME: Complete...
class Color(object):
'''
The Color class.
'''

_mc = None

def __init__(self, MC, cli):
"""
Initialise the Color object.
"""
self._mc = MC
self._cli = cli
self._initialise_dll_funcs()

def get(self, pt):
"""
Gets color at pt[0], pt[1].
Yields integer.
"""
col = c_int(-1)
if isiterable(pt):
if len(pt) != 2:
raise ColorException("Invalid point")
pt = (0, 0)
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, color, box = (), tol = 0):
"""
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.
"""
x, y = (c_int(-1), c_int(-1))
if isiterable(box):
if len(box) != 4:
raise ColorException("Invalid search area")
box = (0, 0, 0, 0)
if tol is 0:
ret = self._mc.dll.find_color(self._cli, byref(x), byref(y),
color, *box)
else:
ret = self._mc.dll.find_color_tolerance(self._cli, byref(x),
byref(y), color, tol, *box)

if ret is RESULT_OK:
return (x, y)
elif ret is RESULT_ERROR:
raise ColorException(self._mc.get_last_error())

return None

def find_all(self, color, box = (), tol = 0):
"""
find all colors in a box, with a specific tolerance.
returned are all the matching points
"""
ptr, _len = PPOINT(), c_int(42)
if isiterable(box):
if len(box) != 4:
raise ColorException("Invalid search area")
box = (0, 0, 0, 0)
if tol is 0:
ret = self._mc.dll.find_colors(self._cli, byref(ptr), byref(_len),
color, *box)
else:
ret = self._mc.dll.find_colors_tolerance(self._cli, byref(ptr),
byref(_len), color, tol, *box)

# Construct list
l = [(ptr[x].x, ptr[x].y) for x in range(_len.value)]

# Free PPOINT
self._mc.free(ptr)

if ret is RESULT_OK:
return l
elif ret is RESULT_ERROR:
raise ColorException(self._mc.get_last_error())

return None

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 isiterable(box):
if len(box) != 4:
raise ColorException("Invalid search area")
box = (0, 0, 0, 0)
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 = 0, 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 isiterable(box):
if len(box) != 4:
raise ColorException("Invalid search area")
box = (0, 0, 0, 0)
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]

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, 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]

^ my color.py

http://paste.villavu.com/show/163/
^ lodgeit.py diff

Decided to start working on SPyRL a little now, as I can't test anything in lib/pyMML and I'd like to at least compile it.. and I'm a bit tired of it for now.