function generateColorGradient($color1, $color2, $steps) {
$r1 = hexdec(substr($color1, 0, 2));
$g1 = hexdec(substr($color1, 2, 2));
$b1 = hexdec(substr($color1, 4, 2));
$r2 = hexdec(substr($color2, 0, 2));
$g2 = hexdec(substr($color2, 2, 2));
$b2 = hexdec(substr($color2, 4, 2));
$stepSize = 1 / ($steps - 1);
$gradient = [];
for ($i = 0; $i < $steps; $i++) {
$r = round($r1 + ($r2 - $r1) * ($i * $stepSize));
$g = round($g1 + ($g2 - $g1) * ($i * $stepSize));
$b = round($b1 + ($b2 - $b1) * ($i * $stepSize));
$gradient[] = str_pad(dechex($r), 2, '0', STR_PAD_LEFT) .
str_pad(dechex($g), 2, '0', STR_PAD_LEFT) .
str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
}
return $gradient;
}
$color1 = "FF0000"; // Start color in RGB format
$color2 = "0000FF"; // End color in RGB format
$steps = 5; // Number of steps in the gradient
$gradient = generateColorGradient($color1, $color2, $steps);
foreach ($gradient as $color) {
echo $color . "<br>";
}
This code defines a function generateColorGradient() that accepts two hex color codes and the number of steps required in the gradient. It converts the hex color codes to RGB values, calculates the step size, and then generates the gradient by interpolating between the RGB values at each step. The resulting colors are stored in an array and returned.
In the example usage, the start color is FF0000 (red) and the end color is 0000FF (blue). The gradient will have 5 steps. The function is called with these values, and each color in the gradient is displayed.