Modify a ColorRGB channel in C#

Sorry to bother again with questions of the same nature, I am retaking c# after a while and I am self-taught then there are many things that I probably don’t know very well

I coded a basic Cellular Automata in C#
now I am trying to add some modifications according to colors and shades. For that I am trying to, depending on some variables, change the R channel of the RGB color asigned. The problem is that I don’t know how to access the R value and modify it. And I can’t understand the indications in Rhino Commons.

Could anyone be so kind of describe the right method for this?
I would appreciate it a lot!

What’s the type of colors? Is it System.Drawing.Color or Rhino.Display.ColorRGBA?

Post here the full C#.

Ok
its a bit embarrassing. I have no professional training and I coded this by my self so probably there are many ways to make it better.
basically I created an array of points and an array of colors. according to the evaluation of the neighbourhood the array of colors stored one color or other. In this case dead is gray and alive is red
What I am trying to do is, if the point is alive, depending on the size of alive neighbours in the neighbourhood, modify the shade of red. For that I am trying to just change the R value of each color in the array.

private void RunScript(Rectangle3d iArea, int cols, int rows, int seed, bool iReset, ref object A, ref object B, ref object C)
{

Point3d[,] initialGrid = new Point3d[cols, rows];
Color [,] initialColors = new Color[cols, rows];
int [,] initialStates = new int[cols, rows];
int [,] myNeighbourhood = new int[cols, rows];



Color red = Color.FromArgb(255, 0, 0);
Color dead = Color.FromArgb(50, 50, 50);


Random myRandomGenerator = new Random(seed);

double areaHeight = iArea.Height;
double areaWidth = iArea.Width;



for(int i = 0;i < cols;i++){
  for(int j = 0;j < rows;j++){

    double pixelSizeC = areaWidth / cols;
    double pixelSizeR = areaHeight / rows;

    initialGrid[i, j] = new Point3d (i * pixelSizeC, j * pixelSizeR, 0);
    initialColors[i, j] = dead;
    initialStates[i, j] = 0;

    double state = myRandomGenerator.NextDouble();

    if (state < 0.50){
      initialColors[i, j] = red;
    }


    if (initialColors[i, j] != dead){

      initialStates[i, j] = 1;

    }
  }
}

if(iReset){

  grid = initialGrid;
  colors = initialColors;
  myState = initialStates;
  NeighboursCount = myNeighbourhood;

}

int [,] futureState = new int[cols, rows];

for(int i = 0;i < cols;i++){
  for(int j = 0;j < rows;j++){

    int count = 0;

    if( myState[(i + cols - 1) % cols, ( j + rows + 1) % rows] != 0)
      count++;
    if( myState[(i + cols) % cols, ( j + rows + 1) % rows] != 0)
      count++;
    if( myState[(i + cols + 1) % cols, ( j + rows + 1) % rows] != 0)
      count++;
    if( myState[(i + cols + 1) % cols, ( j + rows) % rows] == 1)
      count++;
    if( myState[(i + cols + 1) % cols, ( j + rows - 1) % rows] != 0)
      count++;
    if( myState[(i + cols) % cols, ( j + rows - 1) % rows] != 0)
      count++;
    if( myState[(i + cols - 1) % cols, ( j + rows - 1) % rows] != 0)
      count++;
    if( myState[(i + cols - 1) % cols, ( j + rows) % rows] != 0)
      count++;

    NeighboursCount[i, j] = count;


    if(myState[i, j] == 1 && count < 2){
      // dead;
      futureState[i, j] = 0;
    }

    if (myState[i, j] == 1 && count <= 3 && count >= 2){
      // alive;
      futureState[i, j] = 1;
    }

    if (myState[i, j] == 1 && count > 3){
      // dead;
      futureState[i, j] = 0;
    }

    if (myState[i, j] == 0 && count == 3){
      //alive;
      futureState[i, j] = 1;
    }

  }
}


myState = futureState;

for(int i = 0;i < cols;i++){
  for(int j = 0;j < rows;j++){

    if(myState[i, j] == 0){
      colors[i, j] = dead;
    }
    if(myState[i, j] == 1 && NeighboursCount[i, j] == 1 ){
      colors[i, j].R = 100;
    }
    if(myState[i, j] == 1 && NeighboursCount[i, j] == 2 ){
      colors[i, j].R = 200;
    }
    if(myState[i, j] == 1 && NeighboursCount[i, j] > 2 ){
      colors[i, j].R = 255;
    }

  }
}




A = grid;
B = colors;
C = myState;

}

//

Point3d[,] grid;
Color[,] colors;
int [,] myState;
int [,] NeighboursCount;

Here is the file

CA - color variations.gh (20.2 KB)

Well … see attached images:


or

BTW: A value > 255 is a no no (thus I used 100, 127, 255)

BTW: Found a couple of minutes for the usual changes:

CA_EntryLevel_V1.gh (13.5 KB)

1 Like

THANKS SO MUCH @PeterFotiadis !
works perfectly. and thanks for the modifications!

Hello
I didn’t read everything but if you look at the help on Color struct you will see that A, B, R and G are read only, they have a get method not a set method. And there is no method in the color to change that so you have to make a new instance/object.


No method to change a color, just method to make a new struct.
image

1 Like

BTW
I think I would need more theory to understand all the modifications. I knew It would be better to set the functions of current state and future state but I hadn’t the self-trust to start like that from the beginning ^^’

thanks again!

Notify if at some later time you’ll need far more complex (and visually appealing) examples on that matter (but require a higher level of C#).

BTW: well … in most of cases we fake things. For instance you may think that the attached is some sort of CA and/or a Particle System doing this (and that) … but in fact is plain Recursion on some “tree” like expansion plus a naive/primitive distortion of the lines that makes you believe … blah, blah.

So the moral of the case is this: don’t get trapped in the rabbit hole of things.