How do I get the color of a particular coordinate in a PDF page?

Q: How do I get the color of a particular coordinate in a PDF page?

A:

The following code will take in page, and x,y coordinates. Where the coordinates are defined as you would see them while looking at the PDF in a PDF reader app, with the origin being the bottom left corner.

`

void GetColorAtCoordinate(Page& pg, double x, double y)
{
Common::Matrix2D mtx = pg.GetDefaultMatrix(false, Page::e_crop); // set first param to true, to make ‘origin’ top left corner of page
const double scale = 1000.0; // this typically won’t make a difference, but in cases of complex blending you will probably get a more accurate value by scaling up.
const size_t buf_w = 1;
const size_t buf_h = 1;
const int bytes_per_pixel = 4; // BGRA buffer
const size_t buf_size = buf_w * buf_h * bytes_per_pixel;
mtx.Translate(-x, -y);
mtx = Common::Matrix2D(scale, 0, 0, scale, 0, 0) * mtx;
std::vector buf;
buf.resize(buf_size);
PDFRasterizer rast(true);
rast.SetAntiAliasing(false);
rast.Rasterize(pg, &buf[0], buf_w, buf_h, buf_w * bytes_per_pixel, bytes_per_pixel, true, mtx, NULL, NULL, NULL, NULL);
// buf now contains 4 values, BGRA
}

`