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.