Varying Colors in Processing
-
03-07-2019 - |
Question
I've been working on porting some of my Processing code over to regular Java in NetBeans. So far so well, most everything works great, except for when I go to use non-grayscale colors.
I have a script that draws a spiral pattern, and should vary the colors in the spiral based on a modulus check. The script seems to hang, however, and I can't really explain why.
If anyone has some experience with Processing and Java, and you could tell me where my mistake is, I'd really love to know.
For the sake of peer-review, here's my little program:
package spirals;
import processing.core.*;
public class Main extends PApplet
{
float x, y;
int i = 1, dia = 1;
float angle = 0.0f, orbit = 0f;
float speed = 0.05f;
//color palette
int gray = 0x0444444;
int blue = 0x07cb5f7;
int pink = 0x0f77cb5;
int green = 0x0b5f77c;
public Main(){}
public static void main( String[] args )
{
PApplet.main( new String[] { "spirals.Main" } );
}
public void setup()
{
background( gray );
size( 400, 400 );
noStroke();
smooth();
}
public void draw()
{
if( i % 11 == 0 )
fill( green );
else if( i % 13 == 0 )
fill( blue );
else if( i % 17 == 0 )
fill( pink );
else
fill( gray );
orbit += 0.1f; //ever so slightly increase the orbit
angle += speed % ( width * height );
float sinval = sin( angle );
float cosval = cos( angle );
//calculate the (x, y) to produce an orbit
x = ( width / 2 ) + ( cosval * orbit );
y = ( height / 2 ) + ( sinval * orbit );
dia %= 11; //keep the diameter within bounds.
ellipse( x, y, dia, dia );
dia++;
i++;
}
}
Solution
Have you considered adding debugging statements (System.out.println) and looking at the Java Console?
There may be a massive amount of output and definitive slowdown, but you can at least see what happens when nothing seems to happen.
What I do think is a logic error is the filling if statement; every iteratation you decide the color of that iteration and fill with that color. Only iterations with i == 11, 13, or 17 get filled with a color. And the next iteration that color is overwritten by gray. I would think it tends to flicker, possibly to fast to see.
Didn't you want something like
public class Main extends PApplet
{
...
int currentColor = gray;
public Main(){}
...
public void draw()
{
if( i % 11 == 0 )
currentColor = green;
else if( i % 13 == 0 )
currentColor = blue;
else if( i % 17 == 0 )
currentColor = pink;
else {
// Use current color
}
fill(currentColor);
...
}
In that way you start with gray, go to green, blue, pink, green, blue, pink etc. If you also want to see gray at some point you'd have to add something like
else if ( i % 19 ) {
currentColor = gray;
}
Hope this helps.
OTHER TIPS
Thanks for all of the help, but I think my end-goal is being a little misinterpreted.
Here's an image I generated using the Processing PDE:
http://www.deviantart.com/download/97026288/spiral_render_5_by_ishkur88.png
My desired output tends to look similar to that, differing in the colorations and general shape of the spiral.
As the previous poster mentioned: you're using a non gray color only every 11th, 13th and 17th iteration.
Thanks for pointing this out, but I already know! I actually designed it that way!
Reason standing is so that if I didn't have a gray default the output would be insanely chaotic looking and very unpleasing to the eye (my eye at least).
If there were a way I could just skip rendering that circle entirely, I would certainly like that more.
To see whats happening here add
stroke(255);
at the beginning of the draw. You'll see all the wanted circles draw, but without color. As the previous poster mentioned: you're using a non gray color only every 11th, 13th and 17th iteration.
I think that your color values are the main issue here. As from the reference page
From a technical standpoint, colors are 32 bits of information ordered as AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB where the A's contain the alpha value, the R's are the red/hue value, G's are green/saturation, and B's are blue/brightness.
If you look at your values you'll see a very low alpha value, which is maybe impossible to distinguish from the background.
Not sure if you have still an issue. You mention hanging. It is a shot in the dark, but I remember fry repeating that size() call must be the first instruction in setup(). So perhaps moving down the background() call might help. Couldn't hurt anyway.