cancel
Showing results for 
Search instead for 
Did you mean: 

Define color variables for TouchGFX UI

Tales Somensi
Associate II

Hi everyone.

I've been trying to create an header file with some default colors for an user interface. Apparently, TouchGFX has a struct called colortype that is used as a type for colors. I created an .hpp file, included touchgfx/Color.hpp and tried to define variables with type "colortype". It doesn't build, I get the compiler message " 'colortype' does not name a type". What would be a proper way of defining some global default colors into a header file to be included into other ui files?

10 REPLIES 10
Alexandre RENOUX
Principal

Hi,

From my understanding, it is not that straightforward to do so. The best way in my opinion is to declare your colors as follows :

colortype BLUE = Color::getColorFrom24BitRGB(0, 174, 239);

This declaration should be either in your view .hpp file and you will be able to call it in your functions in the .cpp file of this view, or you define them in the Model and create getters to retrieve the colors from any view.

/Alexandre

Martin KJELDSEN
Chief III

Color::getColorFrom24BitRGB() returns a colortype and .setColor() takes a colortype. You could do something like the following and keep defines in a seperate header file that you include where you need it.

//defines.hpp
 #include <touchgfx/Color.hpp>
 
#define BLUE Color::getColorFrom24BitRGB(0, 0, 255)
//myview.hpp
#include <defines.hpp>
...
box.setColor(BLUE);

Tales Somensi
Associate II

Thanks for you fast replies. It was enough to solve the issue.

Tales Somensi
Associate II

Another question on the same topic. Let's say I have a theme. So eventually, I need to change system colors. For that, I need color variables. I could successfully create some #defines as you guys pointed, but when I create a variable with type 'colortype' the compiler returns that same error I told you before. What would be a proper way of declaring my variables and assign my defined colors to it?

I tryed:

// system_colors.hpp
 
#include <touchgfx/Color.hpp>
 
#define COLOR_BLUE Color::getColorFrom24BitRGB(0, 130, 240)
 
colortype syscolor_test = COLOR_BLUE;

Alexandre RENOUX
Principal

Hi,

I am not sure why you want to create a colortype here.

COLOR_BLUE already defines a colortype. A way would be to do the same as with the first line :

#define COLOR_BLUE Color::getColorFrom24BitRGB(0, 130, 240)
#define SYSCOLOR COLOR_BLUE

/Alexandre

Hi, Alexandre.

I don't want it to be a constant because I need to change it's color value on run time, so it has to be a variable.

I understand what you want to do now. Then in this case, I think using the Model to save the color would be the solution I would go for.

Because I do not think that you will be able to declare a colortype in the .hpp file like this as in the end getColorFrom24BitRGB() cannot be called before HAL has been initialized.

static colortype getColorFrom24BitRGB(uint8_t red, uint8_t green, uint8_t blue)
    {
        assert(HAL::getInstance() && "Cannot set color before HAL is initialized");
        return HAL::lcd().getColorFrom24BitRGB(red, green, blue);
    }

I got it. I'll do that. Thanks!

FToff.1
Associate II

Hi,

any chance to have similar capability (to have some kind of color "define") not only by code but also in the designer ?

e.g. : have a page to put color pre-defined values with a label, to recall then on the widgets color configuration.

It will be very useful for adapt GUI in several applications that changes only for color palette used.

Thanks for reply