Color change in processing

I worked on porting some of my processing code to plain Java in NetBeans. So far so good, most work fine except when I use colors without shades of gray.

I have a script that draws a spiral pattern and needs to change colors in a spiral based on a module check. However, the script seems to be hanging, and I cannot explain why.

If anyone has experience with processing and Java, and you could tell me where my error is, I would love to know.

For reviewing, here is 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++; } } 
+4
source share
4 answers

Have you considered adding debugging instructions (System.out.println) and viewing the Java console?

There may be a huge amount of output and a final slowdown, but you can at least see what happens when nothing happens.

I think the logical error is the expression fill if; every iteration you define the color of this iteration and fill it with color. Only iterations with i == 11, 13 or 17 are filled with color. And the next iteration that the color will be overwritten in gray. I would think that this tends to flicker, possibly fasting fast.

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); ... } 

So you start with gray, go to green, blue, pink, green, blue, pink, etc. If you also want to see gray at some point you need to add something like

  else if ( i % 19 ) { currentColor = gray; } 

Hope this helps.

+2
source

Thanks for all the help, but I think my final goal is a little misinterpreted.

Here is the image that I generated using PDE processing:

http://www.deviantart.com/download/97026288/spiral_render_5_by_ishkur88.png

My desired result tends to look similar to what is different in color and the overall shape of the spiral.

As mentioned in a previous post: you use non-gray color only every 11th, 13th and 17th iteration.

Thanks for pointing this out, but I already know! I really designed it that way!

The reason is that if I did not have a gray default, the output would be insanely chaotic and very unpleasant for the eyes (at least my eye).

If I could just skip rendering the entire circle, I would like it more.

+1
source

To see what happens here, add

 stroke(255); 

at the beginning of the draw. You will see all the draws of the draws, but without color. As mentioned in a previous post: you use non-gray color only every 11th, 13th and 17th iteration.

I think your color values ​​are the main issue here. As from the man page

Technically speaking, colors represent 32 bits of information ordered as AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBBB, where A contains the alpha value, R the red / tint value, G the green / saturated, and B the blue / luminance.

If you look at your values, you will see a very low alpha, which may not be distinguishable from the background.

0
source

Not sure if you still have a problem. You say it hangs. This is a shot in the dark, but I remember how fry repeated that calling size () should be the first statement in setup (). So maybe moving around the background () might help. In any case, it does not hurt.

0
source

Source: https://habr.com/ru/post/1277425/


All Articles