From cf5c010fe47b9df2919ecbf248afe33b37c1263a Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Mon, 4 Aug 2025 01:46:01 -0400 Subject: libgfx: draw: Add gfx_get_pix() function Introduce gfx routine to grab the RGB value of a specific pixel at a specific location on the screen. Signed-off-by: Ian Moffett --- lib/libgfx/src/draw.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'lib/libgfx/src/draw.c') diff --git a/lib/libgfx/src/draw.c b/lib/libgfx/src/draw.c index 9ef8630..784110a 100644 --- a/lib/libgfx/src/draw.c +++ b/lib/libgfx/src/draw.c @@ -128,6 +128,34 @@ gfx_plot_point(struct gfx_ctx *ctx, const struct gfx_point *point) return 0; } +/* + * Grab the RGB value of a single pixel on + * the scren. + * + * @ctx: Graphics context pointer + * @x: X position to sample + * @y: Y position to sample + */ +color_t +gfx_get_pix(struct gfx_ctx *ctx, uint32_t x, uint32_t y) +{ + const color_t ERROR_COLOR = GFX_BLACK; + uint32_t index; + + /* The 'ctx' argument is required */ + if (ctx == NULL) { + return ERROR_COLOR; + } + + /* Are we within bounds of the screen */ + if (gfx_pixel_bounds(ctx, x, y) < 0) { + return ERROR_COLOR; + } + + index = gfx_io_index(ctx, x, y); + return ctx->io[index]; +} + /* * Draw a shape onto the screen * -- cgit v1.2.3