Placing arrays into an array

Hi all,

I'm working with arrays to store RGB color values.

I would like to place all color arrays into one array from wich a color can be selected.
Currently I have this array of colors set up like this:

// all colors in one array
int arrColors[10][3]={
 
  {255,156,66},     //white
  {255,0,0},            //red
  {0,255,0},            //green
  {0,0,255},            //blue
  {255,110,0},        //yellow
  {255,0,160},        //purple
  {255,255,255},    //lightblue
  {0,255,255},        //turquoise
  {255,43,148},     //pink
  {255,25,0}          //orange
   
};

However, i would like to create arrays with subsets too.
A sub set would be two or three colors to combine in an effect.

Yes I could define every subset array as above but there must be a better way.
I was thinking of something like:

// arrays for usable/nice colors
int arrWhite[3] = {255,156,66};
int arrRed[3] = {255,0,0};
int arrGreen[3] = {0,255,0};
.....


// all colors in one array
int arrColors[9][3]={
 
  arrWhite,      
  arrRed,       
  arrGreen, 
  arrBlue,
  arrYellow,
  arrPurple,
  arrLightblue,
  arrTurquiose,
  arrPink 
   
};

// colors for red white blue flags
int arrRedWhiteBlue[3][3]={
 
  arrRed,      
  arrWhite,  
  arrBlue
   
};

// colors for red yellow green flags
int arrRedYelloGreen[3][3]={
 
  arrRed,      
  arrYellow,  
  arrGreen
   
};

I can't find a way to get this to work :frowning:
Does anyone have an idea on how to go about this ?

Tanks !

LOL, I made a typo, I said Tanks!

You can have an array of struct

struct color {
    byte r;
    byte g;
    byte b;
};

color all_colors[] = {{0,0,0}, {0, 1, 2}, {0,1,2}};
int color_size = sizeof(all_colors) / sizeof(all_colors[0]); //size of color

Ehm ok,

I'm not sure if I get this right.

What I would like is easily referable color sets for my code.
I'd like an effect function to accept a set of colors to work with.

This code seems to compile just fine:

struct color {
    byte r;
    byte g;
    byte b;
};

color clrRed[] = {255, 0, 0};
color clrGreen[] = {0, 255, 0};
color clrBlue[] = {0, 0, 255};

int arrRGB[3] = {clrRed, clrGreen, clrBlue};

My next question would be how to access the values of R, G and B for elements (color) of arrRGB ?

arrRGB[0][1]
arrRGB[0].r

?

Thanks

Does this do what you want ?

struct color
{
  byte r;
  byte g;
  byte b;
};

color clrRed = {255, 0, 0};
color clrGreen = {0, 255, 0};
color clrBlue = {0, 0, 255};

color arrRGB[3] = {clrRed, clrGreen, clrBlue};

void setup()
{
  Serial.begin(115200);
  Serial.println(arrRGB[0].r);
  Serial.println(arrRGB[1].g);
  Serial.println(arrRGB[2].b);
}

void loop()
{
}

mrExplore:
Ehm ok,

I'm not sure if I get this right.

What I would like is easily referable color sets for my code.
I'd like an effect function to accept a set of colors to work with.

This code seems to compile just fine:

struct color {

byte r;
    byte g;
    byte b;
};

color clrRed[] = {255, 0, 0};
color clrGreen[] = {0, 255, 0};
color clrBlue[] = {0, 0, 255};

int arrRGB[3] = {clrRed, clrGreen, clrBlue};




My next question would be how to access the values of R, G and B for elements (color) of arrRGB ?



arrRGB[0][1]
arrRGB[0].r

?





Thanks

That's certainly not easier, since you can set your struct array at once, you should do it.

color all_colors[] = {{255,0,0}, //red
{0, 255, 0}, //green
{0, 0, 255}, // blue
... //more here

};

So all_colors[0] will have the values for red color (255,0,0). And
all_colors[0].r is the red value
all_color[0].b is the blue value
all_color[0].g is the green value

If you declared all_colors as global variable, you can just use it without passing anything to functions.

[/code]

Thanks for your replies, this does what I need :slight_smile:

Also got me reading on structs and typedef, here's a nice intro:
https://www.norwegiancreations.com/2017/10/getting-started-with-programming-part-8-typedef-and-structs/

Now for the next challenge:

What I'm doing is:

struct color
{
 int R;
 int G;
 int B;
};

// Define colors
color clrRed = {255,0,0};
color clrGreen = {0,255,0};
color clrBlue = {0,0,255};

// Make an array of colors
color arrRedGreenBlue[] = {clrRed, clrGreen, clrBlue};
int intLengtharrRedGreenBlue = sizeof(arrRedGreenBlue) / sizeof(arrRedGreenBlue[0]);

// Make an array to copy colorsets to
// Colorsets wil be placed into this array, from this array random colors wil be chosen for the effects
color arrCurrentColorset[10];

Now I would like to copy the contents from arrRedGreenBlue into arrCurrentColorset.
And this is where my plan fails :frowning:

memcpy( arrCurrentColorset, arrRedGreenBlue, intLengtharrRedGreenBlue );
intLengtharrCurrentColorset = intLengtharrRedGreenBlue;

Does anyone have any idea why ?

Thanks !
(don't worry, no tanks this time :))

int intLengtharrRedGreenBlue = sizeof(arrRedGreenBlue) / sizeof(arrRedGreenBlue[0]);

That value is NOT the length of the array. It is the number of elements in the array.

memcpy( arrCurrentColorset, arrRedGreenBlue, intLengtharrRedGreenBlue );

That function wants the number of bytes to copy (the size of the array), NOT the number of elements in the array.

Ok, I'm just a little bit confused now.

So you say:

length of an array IS NOT number of elements
AND
size of an array IS NOT number of elements

My guess is that you are saying that length and size are both "the number of bytes to copy" ?

So would this work:

memcpy( arrCurrentColorset, arrRedGreenBlue, sizeof(intLengtharrRedGreenBlue) );

The size of the array, in bytes, is sizeof(array). The number of elements in the array is sizeof(array) / sizeof(array[0]).

The memcpy() function wants the number of bytes to copy, not the number of elements to copy.

Ok, thanks for clearing that up.