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 › ints into hex colour
Page Index Toggle Pages: 1
ints into hex colour (Read 422 times)
ints into hex colour
May 15th, 2008, 8:22pm
 
Hi,

I've just started with Processing, so forgive me if I ask a stupid question.

I want to colour a line based on its length.

void draw() {
 stroke(i);
 smooth();
 line(oldX, oldY, newX, newY);
 // make new coords into old coords
 oldX = newX;
 oldY = newY;
}

-New X and Y values come in via OSC - It works, drawing a scribble as X and Y values pour in from Pure Data.

At first I tried different shades of grey with 0 - 255 values correlated to line length but there wasn't enough variation, so I thought I'd go for all 16777215 colours.

My maximum line length is 1131 so I have done
16777215 = X
1131 = Y
Z = X/Y
Line length * Z = i

so 'i' ends up being a number between 0 and 16mil. I have tried stroke(i) but it doesn't work, I looked into hex() and  unhex() but I can't get it to work. I know the solution is probably really simple but I just can't get it!

My code is

int lnlength = int(sqrt(sq(b)+sq(c))); //pythag equals line length

float d = sqrt(sq(X)+sq(Y)); // Max line length
int mxln = int(d); //made integer

int e = 16777215 / mxln; // colour value jump per line length increase
int f = 16777215-(lnlength * e); // colour number inverted
String g = hex(f); //make string from int
int h = unhex(g);
i = h;
// String j = hex(g); // colour based on length of line
// i = unhex(j); //convert to R G B as in documentation

and it prints

### Received an osc message
Total Line count is 92, reset at 1000
Old X is 84, Old Y is 715
New X is 92, New Y is 700
Line Length is 17
Max Line Length 1131
Colour number 16525054

I hope some one can please help?
Re: ints into hex colour
Reply #1 - May 16th, 2008, 2:40pm
 
Interesting idea.
I was blocked too, until I read the lines "To create a color with an alpha value using hexadecimal notation, it's necessary to use the "0x" prefix[...]. Note the alpha value is first in the hexadecimal notation and last in the RGB notation." in the color() page.
Then I remembered that a null alpha value is totally transparent, you have to set alpha to FF to see the color.
So I came up with this little sketch to test the idea:

Quote:
float R;
int prevMouseX = -1, prevMouseY = -1;

void setup()
{
 size(800, 800);
 R = 0xFFFFFF / sqrt(sq(width) + sq(height));
 background(#ABCDEF);
 strokeWeight(4);
}

void draw()
{
}

void mousePressed()
{
 float len = sqrt(sq(prevMouseX - mouseX) + sq(prevMouseY - mouseY));
 color col = 0xFF000000 + int(len * R); // Alpha = opaque
 print(hex(col) + " ");
 stroke(col);
 if (prevMouseX >= 0)
   line(prevMouseX, prevMouseY, mouseX, mouseY);
 else
   line(mouseX, mouseY, mouseX, mouseY);
 prevMouseX = mouseX; prevMouseY = mouseY;
}
Re: ints into hex colour
Reply #2 - May 16th, 2008, 5:28pm
 
Fantastic that works!

I think the '0x....' thing threw me, didn't think with the prefix it would stay an int to apply math to for some reason. What does the prefix mean? Does it just make the value negative? If I print 'col' I just get a negative value so I tried without the hex stuff and it works the same, so I just needed to make my colour number negative!

float R;
int prevMouseX = -1, prevMouseY = -1;

void setup()
{
 size(800, 800);
 R = 16777215 / sqrt(sq(width) + sq(height));
 background(#000000);
 strokeWeight(1);
}

void draw()
{
}

void mousePressed()
{
 float len = sqrt(sq(prevMouseX - mouseX) + sq(prevMouseY - mouseY));
 int col = (16777215 + int(len * R))* -1; // Alpha = opaque
 print(col + " ");
 stroke(col);
 if (prevMouseX >= 0)
   line(prevMouseX, prevMouseY, mouseX, mouseY);
 else
   line(mouseX, mouseY, mouseX, mouseY);
 prevMouseX = mouseX; prevMouseY = mouseY;
}

Works too!

Many thanks for your help!
Re: ints into hex colour
Reply #3 - May 17th, 2008, 11:11am
 
The 0xFF000000 just sets the alpha value (in the high bits of the word) to 255. By making the number negative, you achieve the same thing, but the value of the colors is different.
Which has no importance since the colors are semi-random anyway.
Re: ints into hex colour
Reply #4 - May 19th, 2008, 7:02pm
 
Discovered colorMode and now have,

R = sqrt(sq(width) + sq(height));

colorMode(HSB, R);

lnlength = int(sqrt(sq(oldX - newX)+sq(oldY - newY)));

stroke(lnlength, R, R);

Which gives a nice effect.
Page Index Toggle Pages: 1