Ready-to-use Code ExampleColorSnap

Screen Color Check

Reads the pixel color at specific coordinates and compares it with a target color. Color.pixel() reads a color from the screen, Color.isSimilar() compares two colors within a deltaE threshold (default 10). Used for state detection and screen analysis.

Completion, parameter hints and type checking. Installation instructions and downloads are hosted on GitHub.

Copyable Example Code

You can paste this into the code editor; still verify target image, region, color, text, and timing values in your own device flow.

-- Screen color check
-- checkX, checkY: the pixel; targetColor: HEX; tolerance: deltaE threshold

local checkX, checkY = 540, 960
local targetColor = "#22BB57"
local tolerance = 20

Snap.screenRefresh()

-- Read the pixel color
local pixelColor = Color.pixel(checkX, checkY)
if not pixelColor then
  toast("Could not read the pixel")
  return
end
toast("Pixel color: " .. Color.valueString(pixelColor))

-- Compare with the target color
if Color.isSimilar(pixelColor, targetColor, tolerance) then
  toast("Color matches: the button is active")
  click(Point(checkX, checkY))
else
  toast("Color differs: the button is inactive")
end

-- Read the RGB channels
local r = Color.red(pixelColor)
local g = Color.green(pixelColor)
local b = Color.blue(pixelColor)
toast("RGB: " .. r .. ", " .. g .. ", " .. b)

Implementation and Adaptation Notes

  • Before running, replace sample values such as image names, text, colors, coordinates, and file paths with your own macro values.
  • For examples that use Region or coordinates, retest the target area on different resolutions and DPI values.
  • Tune mScore, timeout, and scan rate in a test macro first, then move the verified values into the production macro.