Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate smooth hex gradient table

Tags:

loops

php

hex

generate list of hex colours?

Hi there,

Currently I am trying to generate a list of 50 hex colours that create a roughly smooth gradient from white to black, with all the colours in between.

How would I go about doing this in php?

like image 482
Hailwood Avatar asked Dec 22 '22 20:12

Hailwood


2 Answers

with all the colours in between

You can find a path from white to black but you will have a difficult time including all colours - colour space is 3-dimensional, not linear.

You could look at this for some ideas: http://www.exorithm.com/algorithm/view/create_gradient

like image 33
Mike C Avatar answered Jan 12 '23 01:01

Mike C


Well,

Although The colours could prob be in a better order,

Here was my 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 as $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 as $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 as $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 as $Gradient)
    echo '<div style="background-color: #' . $Gradient . '">' . htmlentities('<option value="' . $Gradient . '">' . $Gradient . '</option>') . '</div>';
$count += count($Gradients);

echo 'count: ' . $count;
like image 197
Hailwood Avatar answered Jan 12 '23 00:01

Hailwood