How do I create a grey image of a colorful one within LVGL, I can point to?


I have a simple menu with 3 images in a carousel like style.

prev_32px SELECTED_64px next_32px

It’s implemented with a simple structure consisting of pointers to the correct images.

struct entry {
lv_img_dsc_t *prev;
lv_img_dsc_t *selected;
lv_img_dsc_t *next;

struct entry my_menu[] = {
  { &settings_32, &info_64, &usb_32 },
  { &usb_32, &settings_64, &info_32 },
  { &info_32, &usb_64, &settings_32 },

That way I can easily move through the menu. All I have to do is increament or decrement the menu_position and update the images.

But things get tricky now, because not all menu entries are always active. USB for example is only active if a USB stick is present. So I want to “disable” the usb image, by turning it grey.

I can do that with

void lv_obj_set_style_img_recolor(struct _lv_obj_t *obj, lv_color_t value, lv_style_selector_t selector);
void lv_obj_set_style_img_recolor_opa(struct _lv_obj_t *obj, lv_opa_t value, lv_style_selector_t selector);

But doing it this way, the handling gets tricky. Where am I? Am I moving to the left? Or right? etc.
It would be easier to simply replace the colorful usb_32px image with a grey one, but without creating such an image outside of LVGL. So instead of making the image grey when the menu is used, I’d like to change the pointers.

my_menu[0].next = &usb_32_lvgl_grey;

What do you want to achieve?

I want LVGL to create a grey copy of an image, so I can use the grey one like I use the colorful one.

What MCU/Processor/Board and compiler are you using?


What LVGL version are you using?


I found a work around.
I treat the menu normally, but afterwards I check whether an image is using usb_32. If it is, I grey it. Otherwise I “ungrey” it.

if(usb_stick_available == false)
	if(lv_img_get_src(objects.prev) == &usb_32)
		lv_obj_set_style_img_recolor_opa(objects.prev, 255, 0);
		lv_obj_set_style_img_recolor(objects.prev, lv_color_lighten(lv_palette_main(LV_PALETTE_GREY), 0), 0);
		lv_obj_set_style_img_recolor_opa(objects.prev, 0, 0);