Color

Store a color, sample colors from the screen, or compare colors. ARGB contains opacity (A), red (R), green (G), and blue (B); compare(), distance_rgb(), and delta_e() ignore opacity.

Syntax

Color(value, /)

Constructor parameters

NameDescription
valueString | Number
Hex string (`#AARRGGBB`/`#RRGGBB`) or raw ARGB number.
Required · Positional only
No default

Returns

Example

cursor = Point(540, 920)
picked = Color.get(cursor)
if picked == "#FFFFFFFF":
    click(cursor, random=6)

Fields

NameDescription
argbNumber
Raw ARGB integer value.
aInt
Opacity from 0 (transparent) to 255 (opaque).
rInt
Red channel 0-255.
gInt
Green channel 0-255.
bInt
Blue channel 0-255.
hexString
Hex string `#AARRGGBB`.
Constructor example
c1 = Color("#FFFFFFFF")
c2 = Color(4294967295)

Methods

7 methods

Color.get

Sample screen colors once: Point returns one Color, Region returns rows of Colors, and list[Point] returns a Color list in the same order. All points use the same screen image. Missing screen capture or samples return default without retrying; choose a default you can distinguish from the color you need.

Syntax

Color.get(target, interval=3, default="#00000000")

Parameters

NameDescription
targetPoint | Region | list[Point]
Sampling target.
Required · Positional or named
No default
intervalNumber
Spacing between sampled points in a Region's coordinate units; values below 1 use 1. A smaller value returns more colors. Ignored for Point and list[Point].
Optional · Positional or named
Default: 3
defaultColor | String | Number
Fallback color returned when screen capture or pixel sampling is unavailable.
Optional · Positional or named
Default: #00000000

Returns

Color | list[list[Color]] | list[Color] — Colors in the target's shape, with unavailable samples replaced by default. For a Region, read grid[row][column], from top to bottom and left to right. An empty point list returns an empty list.

Example

c = Color.get(Point(100, 200), default="#FF000000")
grid = Color.get(Region(120, 240, 90, 60), interval=3, default="#00000000")
batch = Color.get([Point(10, 10), Point(20, 20)])

Color.compare

Compare two colors by per-channel RGB tolerance. Match passes only when |R1-R2|, |G1-G2|, and |B1-B2| are all <= tolerance.

Syntax

Color.compare(a, b, tolerance=0)

Parameters

NameDescription
aColor | String | Number
First color.
Required · Positional or named
No default
bColor | String | Number
Second color.
Required · Positional or named
No default
toleranceNumber
Maximum difference allowed per RGB channel. 0 requires equal channels; 255 accepts any RGB color. Negative values use 0. Increase gradually using real screen samples.
Optional · Positional or named
Default: 0

Returns

Boolean — true when all three RGB channels are within tolerance; false otherwise.

Example

base = Color("#FF4CAF50")
sample = Color.get(Point(620, 340))
is_same = Color.compare(base, sample, tolerance=8)  # same-ish UI shade

Color.distance_rgb

Measure the combined red, green, and blue difference between two colors. Smaller values mean closer RGB values; transparency is ignored.

Syntax

Color.distance_rgb(a, b)

Parameters

NameDescription
aColor | String | Number
First color.
Required · Positional or named
No default
bColor | String | Number
Second color.
Required · Positional or named
No default

Returns

Number — Distance from 0 to about 441.67; 0 means identical RGB channels. Choose a threshold using samples from your screen.

Example

actual = Color.get(Point(620, 340))
rgb_gap = Color.distance_rgb("#FF4CAF50", actual)
if rgb_gap <= 15:
    print("close in RGB")

Color.delta_e

Estimate how different two colors look using CIE76 Delta E, a color difference measure. Smaller values mean more similar colors; transparency is ignored.

Syntax

Color.delta_e(a, b)

Parameters

NameDescription
aColor | String | Number
First color.
Required · Positional or named
No default
bColor | String | Number
Second color.
Required · Positional or named
No default

Returns

Number — A nonnegative difference score; 0 means identical RGB channels. Test a threshold on your own screen, since perceived similarity varies.

Example

actual = Color.get(Point(620, 340))
d = Color.delta_e("#FF4CAF50", actual)
if d <= 3:
    print("looks similar to human eye")

Color.compare

Check whether this color is close to another color. Each RGB channel must differ by no more than tolerance; transparency is ignored.

Syntax

color.compare(other, tolerance=0)

Parameters

NameDescription
otherColor | String | Number
Color to compare.
Required · Positional or named
No default
toleranceNumber
Maximum difference allowed per RGB channel. 0 requires equal channels; 255 accepts any RGB color. Negative values use 0. Increase gradually using real screen samples.
Optional · Positional or named
Default: 0

Returns

Boolean — true when all three RGB channels are within tolerance; false otherwise.

Example

picked = Color.get(Point(540, 920))
if picked.compare("#FFFFFFFF", tolerance=5):
    click(Point(540, 920))

Color.distance_rgb

Measure the combined RGB difference from this color to another, ignoring transparency.

Syntax

color.distance_rgb(other)

Parameters

NameDescription
otherColor | String | Number
Color to compare.
Required · Positional or named
No default

Returns

Number — Distance from 0 to about 441.67; 0 means identical RGB channels. Choose a threshold using samples from your screen.

Example

base = Color("#FF4CAF50")
actual = Color.get(Point(620, 340))
rgb_gap = base.distance_rgb(actual)
if rgb_gap <= 20:
    print("close enough in RGB")

Color.delta_e

Estimate how different this color looks from another using CIE76 Delta E, ignoring transparency.

Syntax

color.delta_e(other)

Parameters

NameDescription
otherColor | String | Number
Color to compare.
Required · Positional or named
No default

Returns

Number — A nonnegative difference score; 0 means identical RGB channels. Test a threshold on your own screen, since perceived similarity varies.

Example

base = Color("#FF4CAF50")
actual = Color.get(Point(620, 340))
perceptual_gap = base.delta_e(actual)
if perceptual_gap <= 2:
    print("looks almost the same")