Core Function PixelSearch

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
m (1 revision)
 
(7 intermediate revisions by one user not shown)
Line 53: Line 53:
 
Success: Returns a two-element array of pixel's coordinates. (Array[0] = x, Array[1] = y)  
 
Success: Returns a two-element array of pixel's coordinates. (Array[0] = x, Array[1] = y)  
  
Failure: Returns 0.
+
Failure: Returns empty array.
  
 
=== Remarks ===
 
=== Remarks ===
Line 92: Line 92:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
If you not specify a colour value every pixel in your search Rect will be returned as an array see this example:
+
If you do not specify a colour value every pixel in your search Rect will be returned as an array see this example:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$target = PixelSearch ( 0, 0, 300, 300 );
+
$target = PixelSearch ( 0, 0, 100, 100 );
 
foreach($target as $i)
 
foreach($target as $i)
 
{
 
{
Line 102: Line 102:
 
}
 
}
 
println($target);
 
println($target);
// You could do this to do your own scan on the pixels however doing this can be very slow on large rects
+
// You could do this to do your own scan on the pixels
 +
// however doing this can be very slow on large rects
 +
// Note - The $X and $Y is relative to the search rect
 +
// so it will start at 0,0 if you want to get absolute
 +
// coordinates you will need to add the required numbers
 +
// Like this:
 +
List( $X, $Y, $Red, $Green, $Blue ) = $i;
 +
$X += 30;
 +
$Y += 30;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:38, 14 June 2015

PixelSearch ( <left>, <top>, <right>, <bottom>, <colour>, <shade>, <padding>, <step> )

Contents

Description

Searches a rectangle of pixels for the pixel colour provided.

Parameters

left

left coordinate of rectangle.

top

top coordinate of rectangle.

right

right coordinate of rectangle.

bottom

bottom coordinate of rectangle.

colour

Optional but recommended!; Colour value of pixel to find (in decimal or hex).

NOTE - If you not specify a colour value every pixel in your search Rect will be returned as an array see remarks.

shade

Optional; A number between 0 and 255 to indicate the allowed number of shades of variation of the red, green, and blue components of the colour.

Default is 0 (exact match).

padding

Optional; A range of pixels to search around any *found* pixels for example a pad of 1 will cause an area of like 2x2x2 to be scanned instead of just the 1 pixel.

Default is 0 (no padding).

step

Optional; Instead of searching each pixel use a value larger than 1 to skip pixels (for speed). E.g. A value of 2 will only check every other pixel.

Default is 1.

Return Value

Success: Returns a two-element array of pixel's coordinates. (Array[0] = x, Array[1] = y)

Failure: Returns empty array.

Remarks

The search is performed top-to-bottom, left-to-right, and the first match is returned.

RGB is used to define colours by default but this can be changed using the ColourMode option.

Performing a search of a region can be time consuming, so use the smallest region you are able to reduce CPU load.

Note a search of "74, 101, 640, 480" does not mean start at 74x101 and scan a range of 640x480 pixels it means start the rect at 74x101 and stop the rect at 640x480.

See "PixelCoordMode" in Opt

Example

$coord = PixelSearch( 35, 99, 600, 600, 0x0000ff );
If ( $coord )
{
	MsgBox("X and Y are: $coord[0] , $coord[1]");
}
Else
{
	MsgBox("not found");
}
 
// OR
List ($x, $y) = PixelSearch( 35, 99, 600, 600, 0x0000ff );
If ( $x && $y ) // It is true this will fail a match at coordinate 0,0 but its unlikely that it would matter
{
	MsgBox("X and Y are: $x, $y");
}
Else
{
	MsgBox("not found");
}

If you do not specify a colour value every pixel in your search Rect will be returned as an array see this example:

$target = PixelSearch ( 0, 0, 100, 100 );
foreach($target as $i)
{
	List( $X, $Y, $Red, $Green, $Blue ) = $i;
	println("X '$X' Y '$Y,' Red '$Red' Green '$Green' Blue '$Blue'");
}
println($target);
// You could do this to do your own scan on the pixels
// however doing this can be very slow on large rects
// Note - The $X and $Y is relative to the search rect
// so it will start at 0,0 if you want to get absolute
// coordinates you will need to add the required numbers
// Like this:
List( $X, $Y, $Red, $Green, $Blue ) = $i;
$X += 30;
$Y += 30;
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox