# Particles processing

**URL:** https://discourse.processing.org/t/particles-processing/45483
**Category:** Libraries
**Created:** [December 18, 2024, 10:42am UTC](https://discourse.processing.org/t/particles-processing/45483 "2024-12-18T10:42:50Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Sorayamasd](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@Sorayamasd](https://discourse.processing.org/u/Sorayamasd)
#### Post date: [December 18, 2024, 10:42am UTC](https://discourse.processing.org/t/particles-processing/45483/1 "2024-12-18T10:42:50Z")

</div>

Ive seen and modifies this code but i cant make it to do what i want, i want that every time that the mouse is pressed the colors black and wite are inverted:

import ddf.minim.\*;

Minim minim;  
AudioInput mic;

int n = 2345; // Número de partículas  
PVector ps = new PVector[n]; // Array para las posiciones  
PVector ds = new PVector[n]; // Array para las velocidades  
PVector cs = {new PVector(-300, 0), new PVector(300, 0)}; // Cuerpos que atraen las partículas  
color particleColors = new color[n]; // Array para los colores de las partículas

// Lista de colores personalizados (rosa, lila, aquamarina, amarillo)  
color customColors = {  
color(255, 105, 180), // Rosa  
color(230, 230, 250), // Lila  
color(127, 255, 212), // Aquamarina  
color(255, 255, 0) // Amarillo};

void setup() {  
size(1000, 800);  
minim = new Minim(this);  
mic = minim.getLineIn(Minim.MONO, 512); // Usamos MONO para el micrófono

// Inicialización de las partículas  
for (int i = 0; i \< n; i++) {  
ds[i] = new PVector(); // Inicializamos las velocidades en cero  
PVector v = PVector.random2D();  
v.mult(380);  
ps[i] = v; // Asignamos una posición aleatoria a las partículas

```
// Inicializamos el color de cada partícula (por ejemplo rosa)
particleColors[i] = customColors[0]; // Comenzamos con el color rosa

```

}  
background(0);  
}

void draw() {  
fill(0, 8);  
noStroke();  
rect(0, 0, 1000, 800);  
translate(500, 400); // Traslación para centrar el sistema de coordenadas

// Obtener el nivel de audio  
float level = mic.mix.level() \* 100;

// Ajustar la sensibilidad de la variación de color  
int redColor = 255; // El rojo se mantiene constante en blanco  
int greenColor = int(map(level, 0, 10, 255, 210)); // Menos sensibilidad en el verde  
int blueColor = int(map(level, 0, 10, 255, 230)); // Menos sensibilidad en el azul

// Actualizar las partículas con el color afectado por el sonido  
for (int i = 0; i \< n; i++) {  
particleColors[i] = color(redColor, greenColor, blueColor); // Color basado en el sonido  
}

// Ajustar la velocidad de las partículas  
float speed = map(level, 0, 10, 0.005, 0.02); // Rango más controlado

stroke(255); // Establecemos el color de la línea de las partículas

for (int i = 0; i \< n; i++) {  
// Aplicar fuerzas de atracción de las partículas  
for (int j = 0; j \< cs.length; j++) {  
PVector accel = PVector.sub(cs[j], ps[i]);  
accel.setMag(speed); // Ajustar la velocidad de cada partícula  
ds[i].add(accel); // Aumentamos la velocidad según la aceleración  
}

```
// Actualizamos las posiciones de las partículas
ps[i].add(ds[i]);

// Cambiamos el color de la partícula basado en el audio
stroke(particleColors[i]);

// Dibujar las partículas
point(ps[i].x, ps[i].y);}}

```

// Función para cerrar el micrófono correctamente al finalizar  
void stop() { mic.close(); minim.stop(); super.stop();}

// Detecta la tecla “p” y cambia el color de todas las partículas a colores aleatorios  
void keyPressed() {  
if (key == ‘p’ || key == ‘P’) {  
// Cambiamos el color de todas las partículas a un color aleatorio de la lista customColors  
for (int i = 0; i \< n; i++) {  
// Seleccionamos un color aleatorio de customColors  
int randomColorIndex = int(random(customColors.length));  
particleColors[i] = customColors[randomColorIndex]; }}}  
Thanks!!

---

<div class="post-metadata">

### Author: ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)
#### Post date: [December 18, 2024, 12:55pm UTC](https://discourse.processing.org/t/particles-processing/45483/2 "2024-12-18T12:55:32Z")

</div>

Heya,

Can you please format the code correctly, to make it easier for everybody to parse. Check the guideline on how to do this:

[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

I’ve seen several issues in the code you posted, but none of them seem to be relevant to your “black/white toggle”. I’ve also not found the place you want to implement the toggle.

For example, pasting your code into Processing IDE creates a commented closing bracket, leading to a syntax error.

 ![Screenshot 2024-12-18 at 13.48.49](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/e/3ecb11ac1be6f59e67342f1c99ba2081537b2d3f.png)

And then theres a type mismatch here;

```auto
PVector ps = new PVector[n]; // Array para las posiciones

```

Can you reduce down your ask into the bare minimum and leave out any non-related code for now?

A very basic mouse-toggle might look something like this:

```auto
boolean inverted = false; // holds state

void setup() {
  size(400, 400); 
  background(255); // start with white background
}

void draw() {
  if (inverted) {
    background(0); // black background
    fill(255); // white fill
  } else {
    background(255); // white background
    fill(0); // black fill
  }

  ellipse(width/2, height/2, 100, 100);
}

void mousePressed() {
  inverted = !inverted; // toggle the inverted state
}

```
