#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Help with a multidimensional array

edited January 2016
``````int drk;
int lgt;
int normal;
int changer;
int changeg;
int changeb;
float r=random(0,255);
float g=random(0,255);
float b=random(0,255);
float[] [] [] arr = new float[r][g][b];
``````
Tagged:

• It shows an error at the array saying "cannot convert from float to int"

• Yes, you cannot have 2.3256 entries in an array, so r, g and b should be int, and you should initialize them with `int(random(255));`

• So change it to int r=int(random(255));

• @PhiLho thanks

• edited January 2016
• RGB should be stored in separate arrays, not in 1 3D array!
• Better yet, make a class to store those 3 properties!

``````/**
* Colour Class (v1.4)
* by GoToLoop (2015/Apr/16)
* forum.Processing.org/two/discussion/10362/help-with-a-multidimensional-array
* forum.Processing.org/two/discussion/14614/arrays-and-rows-colums
*/

class Colour implements Cloneable {
protected byte r, g, b;

Colour() {
}

Colour(color R, color G, color B) {
set(R, G, B);
}

Colour set(color R, color G, color B) {
r = (byte) (R & 0xFF);
g = (byte) (G & 0xFF);
b = (byte) (B & 0xFF);
return this;
}

Colour randomRGB() {
return fromColour((color) random(~#000000 + 1));
}

Colour fromColour(color c) {
return set(c>>020, c>>010, c);
}

color toColour() {
return hashCode() | #000000;
}

color red() {
return r & 0xff;
}

color green() {
return g & 0xff;
}

color blue() {
return b & 0xff;
}

Colour red(color R) {
r = (byte) (R & 0xFF);
return this;
}

Colour green(color G) {
g = (byte) (G & 0xFF);
return this;
}

Colour blue(color B) {
b = (byte) (B & 0xFF);
return this;
}

@ Override Colour clone() {
try {
return (Colour) super.clone();
}
catch (CloneNotSupportedException cause) {
throw new RuntimeException(cause);
}
}

@ Override int hashCode() {
return r<<020 | g<<010 | b;
}

@ Override boolean equals(Object o) {
return o.hashCode() == hashCode();
}

@ Override String toString() {
return '#' + hex(hashCode(), 6) + " - R: " + nf(r & 0xff, 3)
+ ", G: " + nf(g & 0xff, 3) + ", B: " + nf(b & 0xff, 3);
}
}

final Colour c = new Colour().clone();

void setup() {
size(300, 200);
frameRate(1);
}

void draw() {
println(c.randomRGB());
color rgb = c.toColour();
background(rgb);
frame.setTitle(hex(rgb, 6));
}
``````
• There's already a built in color type (actually an integer) - just use that.

• But having seen the whole question, the whole approach is wrong. You don't need an array at all. (And please don't post multiple questions, it just splits up information and creates work)

http://forum.processing.org/two/discussion/10364/getting-a-random-color-checkerboard-with-random-fill

• edited April 2015

here comes a checker board....

regarding 3D array there is a big misunderstanding...

1. you want to store three values rgb, but you can just store the data type color

even when you want to store 3 values, you don't need a 3D array - you just would (!) need three parallel 1D arrays.

I'll explain.

• A 1D array is a list of values. Those values can be color or a class. You need one index to get a value.

• A 2D value is a grid (like a chess board). You still only get one value (not two!), the value being the cell of the grid. But you now need 2 indexes to get the cell position / value.

• A 3D value is a stack of grids (like 8 chess board on top of each other). You still only get one value (not three!), the value being the cell of the grid. But you now need 3 indexes to get the cell position / value: row, column and etage (floor in the stack).

Since you want a grid you can use a 2D array of color:

`color[][] colors = new color[][];`

my example comes without that.

The three parallel 1D arrays I spoke about; instead of a class with three values you can use parallel arrays for each value. But as said it doesn't apply here since you store only one value (color).

;-)

``````int changer;
int changeg;
int changeb;

void draw() {
int r=int(random(0, 255));
int g=int(random(0, 255));
int b=int(random(0, 255));

float[] [] [] colors = new float[r][g][b];

for (int x = 0; x < width; x+=10) {
for (int y = 0; y < height; y+=10) {
noStroke();
// fill(r, g, b);
if ( (x%20==0 && y%20==0) ||
(x%20!=0 && y%20!=0) )
fill(2, 33, 22);
else
fill(222, 33, 22);
rect(x, y, 10, 10);
}
}
}

void setup() {
size(400, 400);
noLoop();
}
``````