I Have Done This Before

I have gone over this before so it pains me to have to backtrack but this code is meant to output 3 rectangles, each of a different color, sitting on a bookshelf - but nothing prints out - I checked my array and it looks OK so the fault must lie with the for loop but I don’t know what is amiss with that

fill(173, 117, 33); //draws bookshelf
rect(0, 120, width, 10);
var library = [ //array of 3 objects
  {
    title: "I Know Why The Caged Bird Sings",
    author: "R.Michener",
    stars: 4
  },
  {
    title: "Alaric the Goth",
    author: "Boin",
    stars: 4,
  },
  {
    title: "Caesar Invades Gaul",
    author: "Goldsworthy",
    stars: 4
  }
];
for (var i = 0; i < library.length; i++)
{
  fill(100 + 50i, 100 + 50i, 100 + 50i);
  rect(10 + 120 * i, 20, 90, 100);
}

See something missing?

fill(100 + 50i, 100 + 50i, 100 + 50i);

Oh yes - now that I have the incriminating evidence right in my face - of course - I totally blame my high school education for this where I was taught that the following are all equivalent

i50 i x 50 i(50) i*50

by now you would think that I know that in JS, only the last form is allowed so I will NEVER make this error again😞 but when I have done the multiplication of i in the correct form, as in the code below, it still doesn’t print so there’s still an error in my approach

fill(173, 117, 33); //draws bookshelf
rect(0, 120, width, 10);
var library = [ //array of 3 objects
{ 	title:”I Know Why The Caged Bird Sings”,
	author:”R. Michener”,
	stars:4	},
{ 	title:”Alaric the Goth”,
	author:”Boin”,
	stars:4	},
{ 	title:”Caesar Invades Gaul”,
	author:”Goldsworthy”,
	stars:4	}
	];
for(var i = 0;  i < library.length;  i++) 	
    {fill(100 + 50*i,100 + 50*i,100 + 50*i);
rect(10 + 120*i,20,90,100);
}

This looks like it now works as it should. Is there something you expect it to be doing that its not?

The above code is already doing this with grayscale colors. To get other colors you’d have to change your fill call to support different colors. (This could get a little complicated depending on what you’re after.)

You’ve also gotten text in the boxes in another thread so I think you’re close to being there.

Screen Shot 2020-09-12 at 11.36.09 AM

fill(100, 100, 100);

rect(10,10,40,40);

fill(150, 150, 150);

rect(50,50,40,40);

fill(200, 200, 200);

rect(90,90,40,40);

fill(197, 237, 156);

rect(10,140,40,40);

fill(232, 177, 232);

rect(60,140,40,40);

fill(169, 177, 245);

rect(110,140,40,40);

so I have been looking at colors since you told me that

<< The above code is already doing this with grayscale colors. To get other colors you’d have to change your fill call to support different colors. (This could get a little complicated depending on what you’re after.) >>

because I don’t even know what grayscale colors are as opposed to just plain colors. So I thought that I would print them out and the result for the grayscale colors are the 3 diagonal rectangles on top. So yes, I can see why they are grayscale because they look sort of gray. What I would like are colors that look more like the 3 horizontal rectangles but I can’t figure out how to finesse the

{fill(100 - 50*i,200 - 50*i, 50*i);

to get this result

This is where it gets complicated :wink:

Colors represented in RGB, which is what fill() is using, uses 3 color components to create a full color. The 3 components are Red, Green, and Blue, hence the R, G, and B. In 24 bit color, this allows for 256 values for each component, or values of 0 - 255. 24 bit color means there are 24 bits involved or 2^24 which is 16777216. This is the same as 256 * 256 * 256. Each color given 8 bits, or 2^8, which is 256.

For fill() each argument is a component value of the RGB color. The first is Red, the second is Green, and the third is Blue. If you set any one of these values to its max value 255 and the others to 0, you’ll get a color that is all that color

fill(255, 0, 0) // full red
fill(0, 255, 0) // full green
fill(0, 0, 255) // full blue

If you don’t give any component any color, it makes black

fill(0, 0, 0) // full black

And combinations of the values gives other colors

fill(255, 255, 0) // yellow (red + green)
fill(0, 255, 255) // cyan (green + blue)
fill(255, 0, 255) // magenta (red + blue)

All full colors make white

fill(255, 255, 255) // full white

And of course anything in between 0 and 255 for each component, if the same, will make grays

fill(50,50,50) // dark gray
fill(128, 128, 128) // middle gray
fill(200,200,200) // light gray

If you have a loop with a value of i that goes from 0 to some other value, and you want to convert that to colors, you would need to figure out how to do that. There is no easy way to say, hey, i is now one greater, so give me the next color, because there i no standard for what that next color would be and everyone’s idea for what the next color should be could be different.

Since you already have an idea of what your colors should be, I’d suggest putting those values in an array and then getting them out in your loop. Just like you can get your titles out from your other array of objects, you can get colors out of a color array.

var colors = [
  { r: 201, g: 235,  b: 160 },
  { r: 229, g: 179,  b: 231 },
  { r: 169, g: 180,  b: 243 }
]

for(var i = 0; i < 3; i++) {
  fill(colors[i].r, colors[i].g, colors[i].b)
  // ...

Here the r, g, and b properties of the objects in the colors array map to the arguments used in fill(). Then in the loop, you get the current color for that loop iteration using colors[i] and reference those r, g, and b properties for each argument of the fill() call.

fill(255, 255, 0) // yellow (red + green)
fill(0, 255, 255) // cyan (green + blue)
fill(255, 0, 255) // magenta (red + blue)

Screen Shot 2020-09-13 at 9.34.33 AM

So I ran the above 3 lines of code and decided that yes - those are very attractive colors and they are the ones I want for my books - there’s no way on earth that I am going to get those 3 lines of code with this

for(var i = 0; i < library.length; i++)
{fill(100 + 50i,100 + 50i,100 + 50i);

approach so I really admire your solution of placing the exact fill parameters in an array and then calling them out with the i variable - I thought that an alternative approach would be to simply list the desired fill as a fourth property of the object - like this

fill(173, 117, 33); //draws bookshelf
rect(0, 120, width, 10);
var library = [ //array of 3 objects

{ title:”I Know Why The Caged Bird Sings”,
author:”R. Michener”,
stars:4,
color: fill(255, 255, 0) }, // yellow

{ title:”Alaric the Goth”,
author:”Boin”,
stars:4,
fill(0, 255, 255) }, // cyan

{ title:”Caesar Invades Gaul”,
author:”Goldsworthy”,
stars:4,
fill(255, 0, 255) } // magenta
];

for(var i = 0; i < library.length; i++)
fill(library[i].color);
rect(10 + 120*i,20,90,100);
}

and with this code, I had expected to see
the bookshelf
3 nicely spaced books with the colors I had chosen

but I am not getting anything - so I wonder what is amiss this time?

Your syntax is a little off (the first one is almost right), and you don’t want to call fill() when making the library object - thats what you want to call in your loop. What you want color in your library to be is the values for the color you’d send to fill().

In my previous example I used separate values for r, g, and b. But you can also use a single color value which would be easier to do in this case (probably in that case too, but I was trying to keep things simple). You would use the color() function to make that rather than calling fill(). The return value from color() is something you can throw into a fill() call as one value rather than separate r, g, and b values.

  fill(173, 117, 33); //draws bookshelf
  rect(0, 120, width, 10);
  var library = [ //array of 3 objects

  { 	title:"I Know Why The Caged Bird Sings",
      author:"R. Michener",
      stars:4,
      color: color(255, 255, 0)	}, // yellow

  { 	title:"Alaric the Goth",
      author:"Boin",
      stars:4,
      color: color(0, 255, 255)	}, // cyan

  { 	title:"Caesar Invades Gaul",
      author:"Goldsworthy",
      stars:4,
      color: color(255, 0, 255)	} // magenta
      ];

  for(var i = 0; i < library.length; i++) {	
    fill(library[i].color);
    rect(10 + 120*i,20,90,100);
  }

So I am uncertain as to how to complete the bookshelf + 3 books of different colors on that bookshelf. Using your hints I thought that I would have it right but no - here is the code but it does not print out the graphic that I want

—————————————————————————————————————————-

fill(173, 117, 33); //draws bookshelf
rect(0, 120, width, 10);
var library = [ //array of 3 objects

{ title:“I Know Why The Caged Bird Sings”,
author:“R. Michener”,
stars:4,
color: color(255, 255, 0) }, // yellow

{ title:“Alaric the Goth”,
author:“Boin”,
stars:4,
color: color(0, 255, 255) }, // cyan

{ title:“Caesar Invades Gaul”,
author:“Goldsworthy”,
stars:4,
color: color(255, 0, 255) } // magenta
];

  for(var i = 0; i < library.length; i++) 
fill(library[i].color);
rect(10 + 120*i,20,90,100);

}

// but nothing prints out

————————————————————————————————————————————————

so then I looked up the color function and thought that I could use it like this - which frankly seems to be a more complex way of achieving the result that I want but when you are at your wits end, you’ll try anything :slightly_frowning_face: - so anyhow, here is the second block of code but it doesn’t do it for me either

————————————————————————————————————————————————

var firstBook = color(170, 80, 150);
var secondBook = color(100, 120, 190);
var thirdBook = color(250, 240, 20);
fill(173, 117, 33); //draws bookshelf
rect(0, 120, width, 10);
var library = [ //array of 3 objects
{ title:”I Know Why The Caged Bird Sings”,
author:”R. Michener”,
stars:4,
color: firstBook }

{ title:”Alaric the Goth”,
author:”Boin”,
stars:4,
color: secondBook }

{ title:”Caesar Invades Gaul”,
author:”Goldsworthy”,
stars:4,
color:thirdBook
];

for(var i = 0; i < library.length; i++)
fill(library[i].color);
rect(10 + 120*i,20,90,100);
}

// but nothing prints out