Skip to content

Month: September 2018

PHP Generate Color in Rainbow

I had to create a script that generated rainbow between 2 to 768 colors in the rainbow. It returns an array that can be referenced. It’s ideal for 5 to 700 results.

<?PHP
// Generate Rainbow Points.
function generatecolorcodes($numberofcolor)
{
	// Find numbers to pick.
	if ($numberofcolor > 0 && $numberofcolor <= 768)
	{
		$divis = round(768 / $numberofcolor);
		$s = 0;
		while ($s < $numberofcolor)
		{
			$cwheel = $s * $divis;
			if ($cwheel >= 0 && $cwheel < 256)
			{
				$red = 255 - $cwheel;
				$green = 0 + $cwheel;
				$blue = 0;
			}
			if ($cwheel >= 256 && $cwheel < 513)
			{
				$red = 0;
				$green = 255 - ($cwheel - 255);
				$blue = 0 + ($cwheel - 255);
			}
			if ($cwheel >= 513 && $cwheel < 768)
			{
				$red = 0 + ($cwheel - 512);
				$green = 0;
				$blue = 255 - ($cwheel - 512);
			}
			$red = dechex($red);
			$green = dechex($green);
			$blue = dechex($blue);
			if (strlen($blue)<2) {$blue = '0'.$blue;}
			if (strlen($green)<2){$green = '0'.$green;}
			if (strlen($red)<2){$red = '0'.$red;}
			$outarray[$s] = "#" . $red . $green . $blue;
			$s++;
		}
	}
	return $outarray;
}
?>