Replacing FASTLED colour name with an array index

Hi,

I've been playing with the FASTLED library which restricts me from doing something

String colour[] = {"Blue", "Red", "Orange", "Yellow"}; 

for (int row=0;row<10;row++){
  for (int col=0;col<10;col++){
    if (heart2[row][col] == 1){
    leds[XY(col,9-row)] = CRGB::Red;
    }
  }
}
FastLED.show();

Ideally, I would like to replace the word 'Red' with colour[1] but I received an error message "colour is not a member of CRGB". Why can't I do that? Thank you :slight_smile:

uint32_t colour[] = {CRGB::Blue, CRGB::Red, CRGB::Orange, CRGB::Yellow};
leds[XY(col,9-row)] = colour[1];

loocurse:
Ideally, I would like to replace the word 'Red' with colour[1] but I received an error message "colour is not a member of CRGB". Why can't I do that? Thank you :slight_smile:

gfvalvo provided the solution to your problem, now I'll answer this question.

Variable names are not strings. C++ has rules for how variable names work, and this for of dynamic reference is no allowed. Once the code is compiled, there are no variable names left, just memory addresses that the code looks to.

As in gfvalvo's suggestion, you cannot create an array of variable names. You have to create an array of the values that those names refer to, and use those array elements in your code.

You might like to look at the various ways you can set a colour with this libiary:-

You don't have to use the CRGB method.

Thanks so much guys :slight_smile:

I'm sorry, but I must still be missing something. Because when I try to use

gfvalvo:

uint32_t colour[] = {CRGB::Blue, CRGB::Red, CRGB::Orange, CRGB::Yellow};

I still get a compiling error. Can someone fill in the rest of what has to go into creating an array and index of color names for the FastLED library. I'm assuming there's a typedef or something needed that everyone else just knows. Thanks.

Don't you think it would be a good idea if you showed us your complete code and full error message?

ryancasler:
I'm sorry, but I must still be missing something.

Well because the answer isn't quite correct, it should be CRGB colour[] = {CRGB::Blue, CRGB::Red, CRGB::Orange, CRGB::Yellow}; or CRGB colour[] = {CRGB (0,0,255), CRGB (255,0,0)}; CRGB is a struct of 3 bytes (so not a 32-bit value) the separate bytes can be referenced by .r , .g & .b socolour[0].b would equal 255 (in this example

Deva_Rishi:
Well because the answer isn't quite correct ...

Actually, it's absolutely correct. If you look in 'pixeltypes.h', you'll see that the 'CRGB::Blue'-type colors are defined as part of an enum. For example:

Blue=0x0000FF,

So, assigning them to an array of uint32_t is perfectly valid.

Next, if you look at the declaration of the CRGB struct in the same file, you'll see that it contains an overload of 'operator=' that defines assignment by a uint32_t:

inline CRGB& operator= (const uint32_t colorcode) __attribute__((always_inline))

So, assigning an element of a CRGB array the value of an element in a uint32_t array is, again, perfectly valid. And I, of course, have done it many times with the expected results.

@ryancasler's problem is that he/she thought we could some how help them with their code without showing it (or the error message) to us. Probably thinks we have some sort of psychic power, I guess.

1 Like

Actually, it's absolutely correct. If you look in 'pixeltypes.h', you'll see that the 'CRGB::Blue'-type colors are defined as part of an enum. For example:

My Apologies, I forgot about the overloaded version. So may argue that it is wasteful, but i am actually not one of them.

gfvalvo:
Probably thinks we have some sort of psychic power, I guess.

of course we have !