```
<?php
$imhsl = imagecreatefrompng("tulip.hsl.png");
$cellSize = imagesy($imhsl);
$cellCount = imagesx($imhsl) / $cellSize;
if ($cellCount != (int) $cellCount) {
die("Invalid width (not a multiple of the height)\n");
}
echo "Cell count: $cellCount\n";
$hsljson = json_decode(file_get_contents("tulip.plant"), true);
$imrgb = imagecreatetruecolor($cellSize, $cellSize);
imagesavealpha($imrgb, true);
imagealphablending($imrgb, false);
imagefilledrectangle($imrgb, 0, 0, $cellSize - 1, $cellSize - 1, imagecolorallocatealpha($imrgb, 255, 255, 255, 127));
imagealphablending($imrgb, true);
for ($cell = 0; $cell < $cellCount; $cell++) {
echo "Processing cell $cell\n";
$hueRange = explode("-", $hsljson["hue"][$cell]);
if (count($hueRange) == 2) {
$hue = randomFloat((double) $hueRange[0], (double) $hueRange[1]);
} else {
$hue = (double) $hueRange[0];
}
for ($x = 0; $x < $cellSize; $x++) {
$ix = $cell * $cellSize + $x;
for ($y = 0; $y < $cellSize; $y++) {
$hsl = imagecolorsforindex($imhsl, imagecolorat($imhsl, $ix, $y));
$rgb = hsl2rgb(array($hue, $hsl["red"] / 255.0, $hsl["green"] / 255.0));
imagesetpixel($imrgb, $x, $y,
imagecolorallocatealpha($imrgb,
(int) ($rgb[0] * 255.0),
(int) ($rgb[1] * 255.0),
(int) ($rgb[2] * 255.0),
$hsl["alpha"]
)
);
}
}
}
imagepng($imrgb, "tulip.rnd.png");
function randomFloat($min = 0, $max = 1) {
return $min + ($max - $min) * mt_rand() / mt_getrandmax();
}
for ($i = 0; $i < 32; $i++) {
echo randomFloat(0, 2) . "\n";
}
// From Drupal
function hsl2rgb($hsl) {
$h = $hsl[0];
$s = $hsl[1];
$l = $hsl[2];
$m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l*$s;
$m1 = $l * 2 - $m2;
return array(
hue2rgb($m1, $m2, $h + 0.33333),
hue2rgb($m1, $m2, $h),
hue2rgb($m1, $m2, $h - 0.33333)
);
}
function hue2rgb($m1, $m2, $h) {
$h = ($h < 0) ? $h + 1 : (($h > 1) ? $h - 1 : $h);
if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6;
if ($h * 2 < 1) return $m2;
if ($h * 3 < 2) return $m1 + ($m2 - $m1) * (0.66666 - $h) * 6;
return $m1;
}
?>
```