#include <regex>
#include <charconv>
// ...
void __stdcall getp(std::string param) {
std::smatch match;
const auto regex_result = std::regex_search(param, match, std::regex(R"eof((\d+)\s+(\d+))eof"));
struct DCResource {
HWND hwnd;
HDC hdc;
DCResource(const HWND hwnd = ::GetActiveWindow()) : hwnd(hwnd), hdc(::GetDC(this->hwnd)) {}
~DCResource() {
::ReleaseDC(hwnd, hdc);
}
inline HDC operator*() {
return hdc;
}
};
int numbers[2];
for (unsigned int i = 0; i < 2; i++) {
bool defaults = true;
if (regex_result) {
const auto str = match[i + 1].str();
if (std::from_chars(str.data(), str.data() + str.size(), numbers[i]).ec == std::errc()) {
defaults = false;
}
}
if (defaults) {
SF->getSAMP()->getChat()->AddChatMessage(D3DCOLOR_RGBA(255, 255, 0, 0),
"<warning>: Defaulting %s to 0", !i ? "X" : "Y");
numbers[i] = 0;
}
}
union {
struct {
unsigned red : 8;
unsigned green : 8;
unsigned blue : 8;
unsigned alpha : 8;
};
COLORREF as_int;
} color{ .as_int = ::GetPixel(*DCResource{}, numbers[0], numbers[1]) };
if (color.as_int == CLR_INVALID) {
SF->getSAMP()->getChat()->AddChatMessage(D3DCOLOR_XRGB(255, 255, 0),
"<error>: Color cannot be retrieved");
}
else {
SF->getSAMP()->getChat()->AddChatMessage(color.as_int | 0xFF000000,
"Color of the point %d %d is the following: %u (0x%x) (R: %u, G: %u, B: %u)",
numbers[0], numbers[1], color.as_int, color.as_int,
color.red,
color.green,
color.blue
);
}
}