Skip to main content

Find Color - Color Offset Mode (screen.find_color)

Declaration​

x, y = screen.find_color({
[find_all = boolean,]
[max_results = integer_value,]
[max_miss = integer_value,]
{x0, y0, {color0[, offset0]}},
{dx*, dy*, {color*[, offset*]}},
{dx*, dy*, {color*[, offset*]}},
...
}[, left, top, right, bottom ])

Parameters​

  • find_all
    Boolean, optional. When true, returns a table of all match positions within the region, format {{x1, y1}, {x2, y2}, ...}. Default false.
  • max_results
    Integer, optional. The maximum number of results when find_all is true. Up to 1000, default 100.
  • max_miss
    Integer, optional. Maximum number of non-matching points allowed. Default 0 (all points must match).
  • x0, y0
    Integer. Starting anchor coordinate. It does not limit the color search to this fixed point; it only provides a relative coordinate for offset positions. If you do not need to use offsets, use 0, 0.
  • color0
    Integer. Color to match at the starting point.
  • offset0
    Integer. Max color offset (or bias). A value greater than 0xff000000 activates inverse matching (matches colors that do NOT match the specified value).
  • dx*, dy*
    Integer. Offset coordinate relative to the starting point.
  • color*
    Integer. Expected color at the offset point.
  • offset*
    Integer. Max color offset (or bias) at the offset point. A value greater than 0xff000000 activates inverse matching.
  • left, top, right, bottom
    Integer, optional. Search region. Default is full screen.

Returns​

  • x, y
    Integer. The coordinate of the first matched multi-point structure. Returns -1, -1 on failure.

Description​

Find the position of the first fully matched multi-point color structure using color-offset mode.
Coordinate parameters and return values follow the coordinate system initialized via screen.init.

Color offset (bias) represents a range of colors: a color with an offset covers all colors within its red, green, and blue offset ranges.
For 0x456789 with offset 0x123456, the red offset is ±0x12, green is ±0x34, and blue is ±0x56, i.e. red 0x45 ± 0x12, green 0x67 ± 0x34, blue 0x89 ± 0x56, as shown below:

NegativePositive
0x45 - 0x12 = 0x330x45 + 0x12 = 0x57
0x67 - 0x34 = 0x330x67 + 0x34 = 0x9B
0x89 - 0x56 = 0x330x89 + 0x56 = 0xDF

The pair {0x456789, 0x123456} above represents all colors from 0x333333 to 0x579BDF.
Numbers starting with 0x are hexadecimal.

Example​

x, y = screen.find_color({
{ 0, 0, {0xec1c23, 0x000000}},
{ 12, -3, {0xffffff, 0x101010}},
{ 5, -18, {0x00adee, 0x123456}},
{ -1, -10, {0xffc823, 0x101001}},
{ 2, -34, {0xa78217, 0x101001}},
{ 12, -55, {0xd0d2d2, 0x101001}},
}, 0, 0, 100, 100)
--
--[[
In the region from 0, 0 (top left) to 100, 100 (bottom right), find the first point
that exactly matches 0xec1c23 (offset 0), whose color at relative coordinate (12, -3)
differs from 0xffffff by a color offset of less than 0x101010, whose color at relative coordinate (5, -18)
differs from 0x00adee by a color offset of less than 0x123456, and whose remaining points match in the same way.
--]]
--
-- Equivalent form with absolute coords:
--
x, y = screen.find_color({
{ 509, 488, {0xec1c23, 0x000000}},
{ 521, 485, {0xffffff, 0x101010}},
{ 514, 470, {0x00adee, 0x123456}},
{ 508, 478, {0xffc823, 0x101001}},
{ 511, 454, {0xa78217, 0x101001}},
{ 521, 433, {0xd0d2d2, 0x101001}},
}, 0, 0, 100, 100)
--
--[[
In the region from 0, 0 (top left) to 100, 100 (bottom right), find the first point
that exactly matches 0xec1c23 (offset 0), whose color at relative coordinate (521-509, 485-488)
differs from 0xffffff by a color offset of less than 0x101010, whose color at relative coordinate (514-509, 470-488)
differs from 0x00adee by a color offset of less than 0x123456, and whose remaining points match in the same way.
--]]
--
-- One-line compact form:
--
x, y = screen.find_color({ {0,0,{0xec1c23,0x000000} },{12,-3, {0xffffff,0x101010} },{5,-18, {0x00adee,0x123456} },{-1,-10,{0xffc823,0x101001} },{2,-34,{0xa78217,0x101001} },{12,-55,{0xd0d2d2,0x101001} }, },0,0,100,100)
--
-- Inverse matching demo (run on iPhone 5C home screen for results):
x, y = screen.find_color({
{ 516, 288, {0xffffff, 0x101010} },
{ 519, 286, {0xffffff, 0x101010} },
{ 521, 289, {0xffffff, 0x101010} },
{ 516, 296, {0xffffff, 0x101010} },
{ 522, 297, {0xffffff, 0x101010} },
{ 520, 295, {0xffffff, 0xff101010} },
{ 515, 291, {0xffffff, 0xff101010} },
{ 518, 284, {0xffffff, 0xff101010} },
{ 523, 298, {0xffffff, 0xff101010} },
{ 514, 298, {0xffffff, 0xff101010} },
{ 514, 296, {0xffffff, 0xff101010} },
})
--
-- Find-all demo (returns all positions):
results = screen.find_color({
{ 527, 278, {0xde1d26, 0x101010} },
{ 524, 285, {0x007aff, 0x101010} },
{ 555, 292, {0xe4ddc9, 0x101010} },
{ 536, 314, {0xffde02, 0x101010} },
{ 502, 291, {0xffde02, 0x101010} },
{ 502, 283, {0xe4ddc9, 0x101010} },
find_all = true,
})