LVGL retrieving the color of the image based on the coordinates in C language

248 Views Asked by At

I am working on a project based on ESP32-S3 microcontroller. I have a touch ST7789 display wired to the microcontroller and I use LVGL library to display an color palette image.

LVGL version used : 8.3.6

Ideally, I would like to be able to determine the color of the specific location on the image. Look at the example below:

enter image description here

As you can see from the image above, the cursor pointing to the coordinates: X: 126 Y: 74

And as you can see it is pointing to light blue/cyan color, I need to know what color the cursor is pointing at. In this case, I would expect the following result (color might not be 100% accurate)

R:16 G:199 B:219

Is there any option to do this on LVGL? If not, how would I do this in C?

UPDATE: I have discovered lvgl functions:

lv_img_buf_set_px_color

and

lv_img_buf_get_px_color

However, I cant get them to work.

For this simple experiment, I have a simple red rectangle as an png image to cover my whole display: ![image|690x298](upload://2tr1fK4tmXLj1BCv7yRXKxUMFcN.png)

I have tried to do the following:


LV_IMG_DECLARE(red_square)
static lv_obj_t * ui_redsquare;

void display_red_square(){

    ui_redsquare = lv_img_create(lv_scr_act());
    lv_img_set_src(ui_redsquare, &red_square);
    lv_obj_set_width(ui_redsquare, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_redsquare, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_align(ui_redsquare, LV_ALIGN_CENTER);
    lv_obj_add_flag(ui_redsquare, LV_OBJ_FLAG_ADV_HITTEST);     /// Flags
    lv_obj_clear_flag(ui_redsquare, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
}


void set_img_color(){

    lv_color_t c = lv_color_make(0, 255, 0);
    lv_img_buf_set_px_color(ui_redsquare,50,50,c);
    lv_img_buf_set_px_color(ui_redsquare,51,51,c);
    lv_img_buf_set_px_color(ui_redsquare,52,52,c);
    lv_img_buf_set_px_color(ui_redsquare,53,53,c);
    lv_img_buf_set_px_color(ui_redsquare,54,54,c);
    lv_img_buf_set_px_color(ui_redsquare,55,55,c);
    lv_img_buf_set_px_color(ui_redsquare,56,56,c);
    lv_img_buf_set_px_color(ui_redsquare,57,57,c);
    lv_img_buf_set_px_color(ui_redsquare,58,58,c);
    lv_img_buf_set_px_color(ui_redsquare,59,59,c);
}

in my main I do;

    lvgl_setup();
    display_red_square();
    set_img_color()

The red rectangle is displayed properly on my display but I was expecting some of the display to change the color to green, but nothing happened.

I have also tried the following:

LV_IMG_DECLARE(red_square)
static lv_obj_t * ui_redsquare;

void display_red_square(){
    ui_redsquare = lv_img_create(lv_scr_act());
    lv_img_set_src(ui_redsquare, &red_square);
    lv_obj_set_width(ui_redsquare, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_redsquare, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_align(ui_redsquare, LV_ALIGN_CENTER);
    lv_obj_add_flag(ui_redsquare, LV_OBJ_FLAG_ADV_HITTEST);     /// Flags
    lv_obj_clear_flag(ui_redsquare, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
}

void get_img_color(){
    lv_color_t pixel_color = lv_img_buf_get_px_color(ui_redsquare, 50, 50,lv_color_make(0, 0, 0));
    uint8_t red = LV_COLOR_GET_R(pixel_color);
    uint8_t green = LV_COLOR_GET_G(pixel_color);
    uint8_t blue = LV_COLOR_GET_B(pixel_color);
    printf("Pixel color: R:%d, G:%d, B:%d \n", red, green, blue);
}

and in my main.c I do:

    lvgl_setup();
    display_red_square();
    get_img_color();

But the printf prints:

Pixel color: R:0, G:0, B:0 
0

There are 0 best solutions below