Remove a object in the array of colors

Hi! i’m working with a code, and i need to remove the color once it has been used.
is a walker who takes on a color each time he begins his path. I need to make sure that the color not to repeat
This is the class Paleta, i just separate the cold colors of the warm colors, also an array with both of them.
Thanks!


color [] colores = {#d25b17, #e7bc4b, #942720,#1e2744, #304e34, #68494f};
//ARRAY WARM
color [] calidos = {#d25b17, #e7bc4b,  #942720};
//ARRAY COLD
color [] frios = {#1e2744, #304e34, #68494f};

//PALETA FONDO

color [] fondo = {#c63435, #ec603c, #ffc13c ,#4c6045 ,#264468 , #41415b};

class Paleta {

  PImage imagen;
  Paleta( ) {

  }
  
  color darColor() {
  return colores[int(random(6))];
}

  color darCalido() { //RETORNA UN COLOR AL AZAR DEL ARREGLO
    return calidos[int(random(3))];
  }
  color darFrio() { //RETORNA UN COLOR AL AZAR DEL ARREGLO
    return frios[int(random(3))];
  }
  
  color darFondo() {
  return fondo[int(random(6))];
}

I suggest to create a couple of twin arrays, then checking if the new color is already in the list and, if so, discard it. Here’s my approach based on arrays of integers - just change int to color and it should work:

//an array of six numbers
int[] colores = {1, 2, 3, 4, 5, 6};

//a sub-array of three numbers
int[] calidos = {1, 2, 3};
//and its copy of the same lentgh
int[] calidosCopy = new int[calidos.length];

//a second sub-array of other three numbers
int[] frios = {4, 5, 6};
//and its copy
int[] friosCopy = new int[frios.length];

//another array of other six numbers
int[] fondo = {7, 8, 9, 10, 11, 12};

//a random number from zero to three(excluded)
int randomThree = int(random(3));

//a random number from zero to six(excluded)
int randomSix = int (random(6));

//one of the six numbers from the first array
int darColores = colores[randomSix];

//if one of the numbers of the first sub-array
//IS NOT EQUAL TO
//the one of the six numbers previously found,
//copy it to the first sub-array copy
for (int i = 0; i < calidos.length; i++) {
  if (calidos[i] != darColores) {
    calidosCopy[i] = calidos[i];
  }
}

//then extract a random number from the first sub-array copy
int darCalidos = calidosCopy[randomThree];

//if one of the numbers of the second sub-array
//IS NOT EQUAL TO
//the one of the six numbers previously found,
//copy it to the second sub-array copy
for (int i = 0; i < frios.length; i++) {
  if (frios[i] != darColores) {
    friosCopy[i] = frios[i];
  }
}

//then extract a random number from the second sub-array copy
int darFrios = friosCopy[randomThree];

int darFondo = fondo[randomSix];

println(darColores);
println(darCalidos);
println(darFrios);
println(darFondo);
1 Like

Hello!
I’m not sure but …
:thinking:
If you use arrayList instead of array you have more flexibility with adding and removing items from an arrayList. And an arraList will resize dynamically.

You can read about the arrayList here:

https://processing.org/reference/ArrayList.html

And a really good HOW-TO here:

Hopefully this helps.
:nerd_face:

2 Likes

Hi everyone! Thank you all for your answer , I could solve it. I leave you the code! :smiley:

ArrayList<Integer> listC = new ArrayList();
ArrayList<Integer> listCa = new ArrayList();
ArrayList<Integer> listF = new ArrayList();
import java.util.ArrayList;
import java.util.Random;


//PALETA FONDO

color [] fondo = {#c63435, #ec603c, #ffc13c, #4c6045, #264468, #41415b};
Random randomGenerator;
class Paleta {

  PImage imagen;
  Paleta( ) {

// AÑADIMOS TODOS LOS COLORES
    listC.add(color (210, 91, 23)); // AMARILLO;
    listC.add(color (210, 91, 23)); // NARANJA
    listC.add(color (148, 39, 32)); // ROJO
    listC.add(color (30, 39, 68));  // AZUL
    listC.add(color (48, 78, 52)); // VERDE
    listC.add(color (104, 73, 99)); // MORADO

//AÑADIMOS COLORES CALIDOS
    listCa.add(color (210, 91, 23)); // AMARILLO;
    listCa.add(color (210, 91, 23)); // NARANJA
    listCa.add(color (148, 39, 32)); // ROJO


// AÑADIMOS COLORES FRIOS
    listF.add(color (30, 39, 68));  // AZUL
    listF.add(color (48, 78, 52)); // VERDE
    listF.add(color (104, 73, 99)); // MORADO
  }

  void eliminar (color col) {

    listC.remove((col));
  }
  
  
   void eliminarCalido (color col) {

    listCa.remove((col));
  }
  
  
   void eliminarFrio (color col) {

    listF.remove((col));
  }
  
  color  darColor(int _id) {
    return (color) (Integer) listC.get(_id);
  }



  color darCalido(int _id) { //RETORNA UN COLOR AL AZAR DEL ARREGLO
    return (color) (Integer) listCa.get(_id);
  }
  
  
  color darFrio(int _id) { //RETORNA UN COLOR AL AZAR DEL ARREGLO
    return (color) (Integer) listF.get(_id);
  }

  color darFondo() {
    return fondo[int(random(6))];
  }
}

The efficient way is to just shuffle the list.

Then you can just use an index from 0 to n and increase it by 1 esch step. So you use every color without duplicates