Skip to content

Instantly share code, notes, and snippets.

@tecnom1k3
Created October 11, 2021 22:28
Show Gist options
  • Save tecnom1k3/7a98ba0c79ec7814ed4afac2c79ce973 to your computer and use it in GitHub Desktop.
Save tecnom1k3/7a98ba0c79ec7814ed4afac2c79ce973 to your computer and use it in GitHub Desktop.
color generator
<?php
// 3 ranges, values from 0 to 254 (0:ff) each
//cycle all possible values (0:0:0 to ff:ff:ff)
//we have N positions
// each position will get a color at a time
// position 0 = 0:0:0
// position 1 = 0:0:1
// ...
// position n-1 = color combination that is able to fit
// clear first position and replace with second, swapping the rest of the values until the las position is empty
// add the next color in sequence at the last position
function nextColor($red, $green, $blue, $step = 25)
{
$blue += $step;
if ($blue > 255) {
$blue = 0;
$green += $step;
}
if ($green > 255) {
$green = 0;
$red += $step;
}
if ($red > 255) {
$red = 0;
}
return [$red, $green, $blue];
}
$arrPositions = [];
$numPositions = 5;
$currRun = 0;
$totalRuns = 1000;
//INITIALIZE
$red = 0;
$green = 0;
$blue = 0;
$format = '#%02X%02X%02X';
$arrPositions[] = sprintf($format, $red, $green, $blue);
for ($i = 1; $i < $numPositions; $i++) {
list($red, $green, $blue) = nextColor($red, $green, $blue);
$arrPositions[] = sprintf($format, $red, $green, $blue);
}
print_r($arrPositions);
for ($currRun = 0; $currRun < $totalRuns; $currRun++) {
array_shift($arrPositions);
list($red, $green, $blue) = nextColor($red, $green, $blue);
$arrPositions[] = sprintf($format, $red, $green, $blue);
print_r($arrPositions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment