MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey-v1.0 / PixelGetColor

Method PixelGetColor

Source/script2.cpp:4544–4575  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4542
4543
4544ResultType Line::PixelGetColor(int aX, int aY, bool aUseRGB)
4545// This has been adapted from the AutoIt3 source.
4546{
4547 Var *output_var = ResolveVarOfArg(0);
4548 if (!output_var)
4549 return FAIL;
4550 g_ErrorLevel->Assign(ERRORLEVEL_ERROR); // Set default ErrorLevel.
4551 output_var->Assign(); // Init to empty string regardless of whether we succeed here.
4552 if (!(g.CoordMode & COORD_MODE_PIXEL)) // Using relative vs. screen coordinates.
4553 {
4554 // Convert from relative to absolute (screen) coordinates:
4555 RECT rect;
4556 if (!GetWindowRect(GetForegroundWindow(), &rect))
4557 return OK; // Let ErrorLevel tell the story.
4558 aX += rect.left;
4559 aY += rect.top;
4560 }
4561 HDC hdc = GetDC(NULL);
4562 if (!hdc)
4563 return OK; // Let ErrorLevel tell the story.
4564 // Assign the value as an 32-bit int to match Window Spy reports color values.
4565 // Update for v1.0.21: Assigning in hex format seems much better, since it's easy to
4566 // look at a hex BGR value to get some idea of the hue. In addition, the result
4567 // is zero padded to make it easier to convert to RGB and more consistent in
4568 // appearance:
4569 COLORREF color = GetPixel(hdc, aX, aY);
4570 char buf[32];
4571 sprintf(buf, "0x%06X", aUseRGB ? bgr_to_rgb(color) : color);
4572 ReleaseDC(NULL, hdc);
4573 g_ErrorLevel->Assign(ERRORLEVEL_NONE); // Indicate success.
4574 return output_var->Assign(buf);
4575}
4576
4577
4578

Callers

nothing calls this directly

Calls 1

AssignMethod · 0.80

Tested by

no test coverage detected