cancel
Showing results for 
Search instead for 
Did you mean: 

Read color value from coordinates.

A.Qasim
Associate II

Greetings,

I am trying to read the color value of a pixel in hex form. The motivation behind this is the function available in simulator shortcuts as it states,

F1

Enables/disables the display of pointer coordinates as well as RGB color of the pixel at that coordinate (in hexadecimal).

Link: https://support.touchgfx.com/4.19/docs/development/ui-development/working-with-touchgfx/simulator

I have tried many ways but got stuck at retrieving the value tft_framebuffer24 present in HALSDL2.cpp. Can anybody guide me on how can we retrieve the pixel color values at specific coordinates of my screen?

1 ACCEPTED SOLUTION

Accepted Solutions
Osman SOYKURT
ST Employee

Hello A.Qasim,

You can implement that using the handleClickEvent. You need first to get the pixel position, then get the pointer to the framebuffer on the screen based on the coordinates of your clicked pixel. Once you'll get it, you can read the color which is stored in BGR format.

Here the code you need :

void MainView::handleClickEvent(const ClickEvent& event)
{
    //get click position x,y
    int x = event.getX();
    int y = event.getY();
 
    touchgfx_printf("click position : %d %d  \n", x , y );
 
    //get pointer to framebuffer on screen, coordinate x,y
    const uint8_t* fb = (uint8_t*) HAL::getInstance()->getTFTFrameBuffer();
    const uint32_t offset = y*HAL::lcd().framebufferStride() + x * 3;
    fb += offset;
 
 
 
    //pixels are stored in BGR format
    const uint8_t b = *fb;
    const uint8_t g = *(fb+1);
    const uint8_t r = *(fb+2);
 
    
    touchgfx_printf("color : %d %d %d  \n",r,g,b);
}

Hope this helps :)

/Osman

Osman SOYKURT
ST Software Developer | TouchGFX

View solution in original post

5 REPLIES 5
HP
Senior III

you should have access to the starting address of your framebuffer.

When you have that and you know that each pixel fill up 24bits* you should be able to find the offset inside the framebuffer to read the 3 bytes

*There might be an alpha value as well here, so the offset could be 32bits instead. (it's been a while)

A.Qasim
Associate II

Thanks for the insight HP. Well i am struggling to get the framebuffer thing can you post some code or documentation hint for accessing the framebuffer.

Looking forward to it.​

That depends on your setup - if you use a 'standard' setup your framebuffer is located in SDRAM and you should have the start address in the Cube view.

Ususally it is at the beginning of the SDRAM which is 0xC0000000 (can't remember how many zeroes goes here ;) )

I'm not sure how double or triple buffering affects the results - you might have to look at two locations and compare them. but that should be easy since the two buffers are located right nexxt to each other.

Thanks these instructions duly Noted.​ But currently I'm working purely on simulator. So any idea about getting framebuffer in simulator.

Osman SOYKURT
ST Employee

Hello A.Qasim,

You can implement that using the handleClickEvent. You need first to get the pixel position, then get the pointer to the framebuffer on the screen based on the coordinates of your clicked pixel. Once you'll get it, you can read the color which is stored in BGR format.

Here the code you need :

void MainView::handleClickEvent(const ClickEvent& event)
{
    //get click position x,y
    int x = event.getX();
    int y = event.getY();
 
    touchgfx_printf("click position : %d %d  \n", x , y );
 
    //get pointer to framebuffer on screen, coordinate x,y
    const uint8_t* fb = (uint8_t*) HAL::getInstance()->getTFTFrameBuffer();
    const uint32_t offset = y*HAL::lcd().framebufferStride() + x * 3;
    fb += offset;
 
 
 
    //pixels are stored in BGR format
    const uint8_t b = *fb;
    const uint8_t g = *(fb+1);
    const uint8_t r = *(fb+2);
 
    
    touchgfx_printf("color : %d %d %d  \n",r,g,b);
}

Hope this helps :)

/Osman

Osman SOYKURT
ST Software Developer | TouchGFX