random background-color on div without the same color repeating twice consecutively

Asked

Viewed 1,770 times

2

Well I want to create a sequence of DIV, only that I want each DIV have in your background-color a previously settled random color, type black, yellow and green, but I do not want these colors to repeat one after the other.

Type :yellow >> yellow >> black>> green >> black

I want it to look something like this :: yellow >> black >> yellow >> green >> black

Currently I use this code to generate random colors pro background-color, but this code repeats the colors one after the other, since the random color is generated in the refresh of the page.

<?php
$cor = array();
$cor[1] = "#CFF";
$cor[2] = "#9FF";
$cor[3] = "#600";
$cor[4] = "#FF0";
$cor[5] = "#C69";
$cor[6] = "#0F0";
$contador = count($cor);
$aleatorio = rand(1,$contador);
?>

<style>
div{margin-left:15px; width:100px; height:100px; float:left}
</style>

<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>
<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>
<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>
<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>
<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>
<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>

In this case my problem is not to repeat the colors, but to repeat the same color one after the other. That is why I quoted the above pattern " yellow >> yellow >> black >> green >> black", in which case equal colors repeat one after the other. The colors can even repeat COUNTLESS times, but I don’t want it to repeat one after the other, I want it to be something like :: Yellow >> black >> Yellow >> black

  • I didn’t understand the problem, is to repeat the colors in the same load (each div should be with a different color) or in different loads (all Ivs of the same color, but changing with each refresh)?

  • @luigibertaco the problem is not to repeat the colors, but to repeat the same color one after the other. So I quoted that pattern in the example " yellow >> yellow >> black >> green >> black". In case the colors can repeat NUMEROUS times, but I don’t want it to repeat after each other, type :: Yellow >> yellow >> black >> black

  • then the @Bacco answer solves your problem.

  • 1

    @ivanveloso if you are curious to see, I gave a beautiful optimized code. The logic is the same, but I left cleaner and objective. Jai delete this comment here.

2 answers

4


If you want to switch colors between the divs, it is easier to create a function:

<?php
   function corAleatoria() {
      static $corAnterior = 0;
      static $cor = array( '#CFF', '#9FF', '#600', '#FF0', '#C69', '#0F0' );

      $aleatorio = rand( $corAnterior?1:0, count( $cor ) - 1 );
      if( $aleatorio >= $corAnterior ) $aleatorio++;
      $corAnterior = $aleatorio;
      return $cor[$aleatorio - 1];
   }
?>

<style>
   div{margin-left:15px; width:100px; height:100px; float:left}
</style>

<div style="background-color: <?php echo corAleatoria(); ?>"></div>
<div style="background-color: <?php echo corAleatoria(); ?>"></div>
<div style="background-color: <?php echo corAleatoria(); ?>"></div>
<div style="background-color: <?php echo corAleatoria(); ?>"></div>
<div style="background-color: <?php echo corAleatoria(); ?>"></div>
<div style="background-color: <?php echo corAleatoria(); ?>"></div>

So every time you’re called to corAleatoria(), a string will be returned with the color drawn and not repeated.

If in the future divs in loop, can simplify the process in this way:

<style>
   div{margin-left:15px; width:100px; height:100px; float:left}
</style>

<?php
   $corAnterior = 0;
   $cor = array( '#CFF', '#9FF', '#600', '#FF0', '#C69', '#0F0' );
   $max = count( $cor ) - 1;

   for( $i = 0; $i < 10; $i++ ) {
      $aleatorio = rand( $corAnterior?1:0, $max );
      if( $aleatorio >= $corAnterior ) $aleatorio++;
      $corAnterior = $aleatorio;

      echo '<div style="background-color: '.$cor[$aleatorio - 1].'"></div>';
   }
?>

2

Can solve in a simpler way:

<?php
$cor = array();
$cor[1] = "#CFF";
$cor[2] = "#9FF";
$cor[3] = "#600";
$cor[4] = "#FF0";
$cor[5] = "#C69";
$cor[6] = "#0F0";

//altera de forma aleatória os membros da array.
shuffle($cor);
?>

<style>
div{margin-left:15px; width:100px; height:100px; float:left}
</style>

<div style="background-color:<?php echo $cor[0] ?>"></div>
<div style="background-color:<?php echo $cor[1] ?>"></div>
<div style="background-color:<?php echo $cor[2] ?>"></div>
<div style="background-color:<?php echo $cor[3] ?>"></div>
<div style="background-color:<?php echo $cor[4] ?>"></div>
<div style="background-color:<?php echo $cor[5] ?>"></div>
  • 1

    The only problem I see with this solution is that if you increase the number of divs has to increase the number of colors. But if the number of Divs is equal, it is a good solution.

  • at first glance had not noticed this factor to which @Bacco referred. Its code is simpler, that is, but it is limited, and as Bacco said, as the number of div’s increases I will also have to increase the number of colors, which makes it unviable in some cases. Anyway thanks for the help!

  • If your idea is to have an indeterminate number of div, you should then create them dynamically through a function that accepts as a parameter the number of Divs you want to create. I don’t see any point in so many function calls.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.