php - generate smooth hex gradient table -
generate list of hex colours?
hi there,
currently trying generate list of 50 hex colours create roughly smooth gradient white black, colours in between.
how go doing in php?
well,
although colours prob in better order,
here working.
<?php function gradient($hexfrom, $hexto, $colorsteps) { $fromrgb['r'] = hexdec(substr($hexfrom, 0, 2)); $fromrgb['g'] = hexdec(substr($hexfrom, 2, 2)); $fromrgb['b'] = hexdec(substr($hexfrom, 4, 2)); $torgb['r'] = hexdec(substr($hexto, 0, 2)); $torgb['g'] = hexdec(substr($hexto, 2, 2)); $torgb['b'] = hexdec(substr($hexto, 4, 2)); $steprgb['r'] = ($fromrgb['r'] - $torgb['r']) / ($colorsteps - 1); $steprgb['g'] = ($fromrgb['g'] - $torgb['g']) / ($colorsteps - 1); $steprgb['b'] = ($fromrgb['b'] - $torgb['b']) / ($colorsteps - 1); $gradientcolors = array(); for($i = 0; $i <= $colorsteps; $i++) { $rgb['r'] = floor($fromrgb['r'] - ($steprgb['r'] * $i)); $rgb['g'] = floor($fromrgb['g'] - ($steprgb['g'] * $i)); $rgb['b'] = floor($fromrgb['b'] - ($steprgb['b'] * $i)); $hexrgb['r'] = sprintf('%02x', ($rgb['r'])); $hexrgb['g'] = sprintf('%02x', ($rgb['g'])); $hexrgb['b'] = sprintf('%02x', ($rgb['b'])); $gradientcolors[] = implode(null, $hexrgb); } $gradientcolors = array_filter($gradientcolors, "len"); return $gradientcolors; } function len($val){ return (strlen($val) == 6 ? true : false ); } $count = 0; $steps = 9; $gradients = gradient("ffffff", "ff0000", $steps); foreach($gradients $gradient) echo '<div style="background-color: #' . strtoupper($gradient) . '">' . htmlentities('<option value="' . strtoupper($gradient) . '">' . strtoupper($gradient) . '</option>') . '</div>'; $count += count($gradients); $gradients = gradient("df1f00", "00ff00", $steps); foreach($gradients $gradient) echo '<div style="background-color: #' . strtoupper($gradient) . '">' . htmlentities('<option value="' . strtoupper($gradient) . '">' . strtoupper($gradient) . '</option>') . '</div>'; $count += count($gradients); $gradients = gradient("00df1f", "0000ff", $steps); foreach($gradients $gradient) echo '<div style="background-color: #' . strtoupper($gradient) . '">' . htmlentities('<option value="' . strtoupper($gradient) . '">' . strtoupper($gradient) . '</option>') . '</div>'; $count += count($gradients); $gradients = gradient("0000df", "000000", $steps); foreach($gradients $gradient) echo '<div style="background-color: #' . $gradient . '">' . htmlentities('<option value="' . $gradient . '">' . $gradient . '</option>') . '</div>'; $count += count($gradients); echo 'count: ' . $count;
Comments
Post a Comment