We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Confused: how can an int hold a color
Page Index Toggle Pages: 1
Confused: how can an int hold a color? (Read 753 times)
Confused: how can an int hold a color?
Oct 29th, 2009, 1:45pm
 
Hello, The following code is from chapter 14, page 7 of "Processing: Creative Coding and Computational Art":

Quote:
class Bird{
 float offsetX, offsetY, offsetZ;
 float w, h;
 int bodyFill;
 int wingFill;

 float ang1 = 0, ang2 = 0, ang3 = 0, ang4 = 0;
 float radiusX = 120, radiusY = 200, radiusZ = 700;
 float rotX = 15, rotY = 10, rotZ = 5;
 float flapSpeed = .4;
 float rotSpeed = .1;




 Bird(){
   this(0, 0, 0, 60, 80);
 }

 Bird(float offsetX, float offsetY, float offsetZ, float w, float h){
   this.offsetX = offsetX;
   this.offsetY = offsetY;
   this.offsetZ = offsetZ;
   this.h = h;
   this.w = w;
   bodyFill = color(200, 100, 10);
   wingFill = color(200, 200, 20);

 }


What is it that is being passed from color(200, 100, 10) to the int bodyFill? Why have bodyFill as an int datatype instead of a color datatype?
Re: Confused: how can an int hold a color?
Reply #1 - Oct 29th, 2009, 2:18pm
 
Well, you can write:
color bodyFill;
color wingFill;

if you prefer...
Perhaps they used ints because they code with Eclipse...

'color' type in Processing is actually a pseudo-type, which is actually a simple int, ie. 4 bytes. The PDE just replaces them with int where it makes sense, before running a sketch.

A color in a computer is actually just a number, or more exactly, three numbers (and an alpha value), each value measuring the amount of each component (red, green, blue).
Re: Confused: how can an int hold a color?
Reply #2 - Oct 29th, 2009, 2:26pm
 
As the reference says, colors are 32 bits of information ordered as AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB. So int are big enough to store colors.

Indeed, as the javadoc says, the int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).

The color datatype does not exist in Java (Processing is written in Java), so the guys just used the int datatype to make it simpler.

For example, -16 777 216 is opaque black, and -1 is opaque white.
Re: Confused: how can an int hold a color?
Reply #3 - Oct 29th, 2009, 2:52pm
 
ah ok it all makes sense now. Thank you!
Page Index Toggle Pages: 1