I’m trying to write a GUI library , but I find a strange behavior of color. Here is an example↓
If you write like this, everything works fine.
Code:
Output:
However, if you write like this, something strange happen
Code:
Output:
You could see that if you make the variable test a local variable,everthing works
fine.
But once you make the variable test a global variable or a variable in a class,
(I tested, but dont have a screenshot) ,
the program will simply stuck at the command “Brain.Screen.setPenColor(test);”
Any solution to solve the problem?
It doesn’t get stuck, but test is probably initialized to black (ie. 0)
global constructors can be a bit weird, it probably means that the global “blue” has not yet been initialized.
you could make it a reference
const color &test = color::blue;
or just initialize to an rgb value
color test( 0x0000FF );
and what’s with the unusual parameter passing ? That’s not something I’m familiar with
Brain.Screen.setPenColor(color:test);
2 Likes
I don’t think that’s parameter passing. Rather, it’s a vscode extension that shows the name of the argument you are passing in.
3 Likes
Yes, that maks sense. However, I want to make the varible test changeable,since the orginal code of my library looks like this:
here are some other functions:
You could see that I acually want to provide two ways to set the color. One of them is using the color class. And the other way is to use rgb color, which works all right.
Since I store the color in a varible called button_color, it has to be changeable. So,
“const color &test = color::blue;” this method won’t work.
Could you give me more suggestions on that?
use the other form I suggested, avoid the pre named colors.
just do
color outline_display_color( 0xFFFFFF );
etc.
The hex value is just the rgb value of the color.
This is all the pre defined colors are.
// Bunch of predefined colors
const color color::black ( 0x000000 );
const color color::white ( 0xffffff );
const color color::red ( 0xff0000 );
const color color::green ( 0x00ff00 );
const color color::blue ( 0x0000ff );
const color color::yellow ( 0xffff00 );
const color color::orange ( 0xffa500 );
const color color::purple ( 0xff00ff );
const color color::cyan ( 0x00ffff );
3 Likes
Ok, I see. But guess what? I just find the another way to solve my problem.
Here is my previous constructor code

And if I ues the function called set_button_color to set the button color to default color like this,
evening works fine.
I mean that’s incredible. Why it could actually work?