Color picker returns unexpected RGB values

I’m using LVGL on a TFT display with an Arduino. I’m trying to get the RGB values from the color picker widget. The code seems to be working, but it’s returning unexpected values. I can’t seem to figure out what I’m missing or how I’m interpreting the data incorrectly.

lv_color_t selectedColor = lv_colorwheel_get_rgb(ui_colorWheel);
  uint8_t red = selectedColor.ch.red;
  uint8_t green = selectedColor.ch.green;
  uint8_t blue = selectedColor.ch.blue;
  Serial.print("Color changed to RGB: ");
  Serial.println(red);
  Serial.println(green);
  Serial.println(blue);

When the color wheel selection is basically red (hard to get it exact), the output is:

Color changed to RGB: 31
3
0

I would expect something more like 255 for the red value. I know it’s red because later in the code I set the text color to the color wheel selection. What am I missing?

Setting it to yellow-ish, the output is:

Color changed to RGB: 30
63
0

I’m not understanding something. Any help is appreciated! Thanks

I get the same problem , does it should be 0 -255, it seems that the big value is 31
Here is the code I get rgb value :
uint16_t out[3];
lv_color_t c = lv_colorwheel_get_rgb(ui_Colorwheel3);
out[0] = LV_COLOR_GET_R(c);
out[1] = LV_COLOR_GET_G(c);
out[2] = LV_COLOR_GET_B(c);

Hello,

I am not sure what “LV_COLOR_GET_x” does… though I can guess.

But why not just get the values from the struct directly? By the way, the lv_color_t struct consists of the following: Colors — LVGL documentation

So it is just three bytes, I figure the problem here is possible casting.

Try this:

uint8_t out[3];
lv_color_t c = lv_colorwheel_get_rgb(ui_Colorwheel3);
out[0] = c.r;
out[1] = c.g;
out[2] = c.b;

Here is my LVGL version :
#define LVGL_VERSION_MAJOR 8

#define LVGL_VERSION_MINOR 3

#define LVGL_VERSION_PATCH 6

#define LVGL_VERSION_INFO “”

uint8_t out[3];
lv_color_t c = lv_colorwheel_get_rgb(ui_Colorwheel3);
out[0] = c.r;
out[1] = c.g;
out[2] = c.b;
or
out[0] = c.red;
out[1] = c.green;
out[2] = c.blue;

I have try and cannot compile:
E:/project/TFT_BW8625/TFT_BW8625/main/ui.c:848:20: error: ‘lv_color_t’ {aka ‘union ’} has no member named ‘r’
out[0] = c.r;
^
E:/project/TFT_BW8625/TFT_BW8625/main/ui.c:849:20: error: ‘lv_color_t’ {aka ‘union ’} has no member named ‘g’
out[1] = c.g;
^
E:/project/TFT_BW8625/TFT_BW8625/main/ui.c:850:20: error: ‘lv_color_t’ {aka ‘union ’} has no member named ‘b’
out[2] = c.b;
attached file is lv_color.h file.
lv_color.h (22.8 KB)

Excuse me, I notice now that it should be out[0] = c.red etc.
That should work, I see it in you code snippet also but have you tried that?

yes , have tried and cannot compile . You can check my lv_color.h , it doesn’t have member red green and blue . Below is the log :

E:/project/TFT_BW8625/TFT_BW8625/main/ui.c:848:20: error: ‘lv_color_t’ {aka ‘union ’} has no member named ‘red’
out[0] = c.red;
^
E:/project/TFT_BW8625/TFT_BW8625/main/ui.c:849:20: error: ‘lv_color_t’ {aka ‘union ’} has no member named ‘green’
out[1] = c.green;
^
E:/project/TFT_BW8625/TFT_BW8625/main/ui.c:850:20: error: ‘lv_color_t’ {aka ‘union ’} has no member named ‘blue’
out[2] = c.blue;
^

Hello,

Very strange. Perhaps it should be out[0] = c.ch.red etc? That’s what I can gather from this post: How to extract RGB values from colorpicker widget? - #3 by skartalov

Sadly I only have V9 running at the moment in which this color picker does not exist anymore. If this does not help then I do not have any more ideas.

Thanks .
It can use
out[0] = c.ch.red;
out[1] = c.ch.green_h;
out[2] = c.ch.blue;
to get color value ,but it is the same value as what I have got before:

What is your color depth? I think it has to do with that. For instance if you use 16-bit color the values here will be as follows:

red: 5 bits      (max.   11111  = 31)
green: 6 bits    (max.   111111 = 63)
blue: 5 bits     (max.   11111  = 31)

You may have to convert it to 32-bit color. You might try lv_color_to32.
See this bit of the documentation (convert color): Colors — LVGL documentation

Thanks ,it works ok now :
if(event_code == LV_EVENT_VALUE_CHANGED){
uint16_t out[3];
lv_color32_t c32;
lv_color_t c = lv_colorwheel_get_rgb(ui_Colorwheel3);
c32.full = lv_color_to32(c);
out[0] = c32.ch.red;
out[1] = c32.ch.green;
out[2] = c32.ch.blue;
ESP_LOGW(TAG_, “RGB: %d / %d / %d \r\n”, out[0], out[1], out[2] );
}

That’s a relief you found a clean way to convert lv_color_t to lv_color32_t and extract the 32-bit RGB values correctly! Dealing with different color depths (like 16-bit vs. 32-bit) in embedded systems is always where the weird, unexpected values pop up. The lv_color_to32(c) conversion is definitely the key step there.Just a small suggestion for workflow: Since you are now working with 32-bit color spaces (which often correspond to standard screens), if you ever need to match a specific color from an external source (like a designer’s image or a web reference) and get the exact RGB numbers to plug into your code, I highly recommend using colorpickerimage.org.It’s a simple, free online tool where you can upload an image, click anywhere on it, and it gives you the precise decimal RGB values and HEX codes you need. Super useful for making sure the colors you get on your hardware match your intended design exactly!
Glad you got it working!