Skip to main content
A.Qasim
Associate II
April 7, 2022
Solved

Read color value from coordinates.

  • April 7, 2022
  • 2 replies
  • 8070 views

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?

This topic has been closed for replies.
Best answer by Osman SOYKURT

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

2 replies

HP_it
Senior II
April 7, 2022

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
A.QasimAuthor
Associate II
April 7, 2022

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.​

HP_it
Senior II
April 7, 2022

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.

Osman SOYKURT
Osman SOYKURTBest answer
Technical Moderator
April 8, 2022

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 SOYKURTST Software Developer | TouchGFX