Skip to content

Instantly share code, notes, and snippets.

@DavidHernandez
Created January 5, 2015 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidHernandez/576da779a11850ea3d87 to your computer and use it in GitHub Desktop.
Save DavidHernandez/576da779a11850ea3d87 to your computer and use it in GitHub Desktop.
This little PHP script helps you when you need to get all the colors between two specific colors. It's usefull if you need to create a progress bar, that changes its color.
#!/usr/bin/php
<?php
/**
* This script receives two colors and the number of steps
* of the progress and returns the different color for each
* step.
*/
if (count($argv) < 3) {
print "Usage: color-progress.php HEX_COLOR_1 HEX_COLOR_2 NUMBER_OF_STEPS.
Example:
color-progress.php 0088cc CCCCCC 4
";
exit;
}
$start = $argv[1];
$end = $argv[2];
$steps = $argv[3];
$color_progress = ColorProgress::fromHex($start, $end, $steps);
print_r($color_progress->getStepColorsInHex());
//print_r($color_progress->getStepColorsInRGB());
class ColorProgress {
private $startingColor;
private $endingColor;
private $steps;
private $stepColors;
private $increments;
public function __construct(Color $start, Color $end, $steps) {
$this->startingColor = $start;
$this->endingColor = $end;
$this->steps = $steps;
$this->calculateIncrements();
$this->calculateStepColors();
}
public static function fromHex($start, $end, $steps) {
$startingColor = Color::fromHex($start);
$endingColor = Color::fromHex($end);
$instance = new self($startingColor, $endingColor, $steps);
return $instance;
}
public static function fromRGB($start, $end, $steps) {
$startingColor = Color::fromRGB($start);
$endingColor = Color::fromRGB($end);
$instance = new self($startingColor, $endingColor, $steps);
return $instance; }
public function getStepColorsInRGB() {
return $this->stepColors;
}
public function getStepColorsInHex() {
$hexStepColors = array();
foreach ($this->stepColors as $stepColor) {
$color = Color::fromRGB($stepColor);
$hexStepColors[] = $color->getHex();
}
return $hexStepColors;
}
private function calculateStepColors() {
for ($i = 1; $i <= $this->steps; $i++) {
$increment = array(
$this->increments[0] * $i,
$this->increments[1] * $i,
$this->increments[2] * $i,
);
$this->stepColors[] = $this->startingColor->incrementRGB($increment);
}
}
private function calculateIncrements() {
$origin = $this->startingColor->getRGB();
$destination = $this->endingColor->getRGB();
$quantity = array();
for ($i = 0; $i < 3; $i++) {
$quantity[$i] = ($destination[$i] - $origin[$i]) / $this->steps;
}
$this->increments = $quantity;
}
}
class Color {
private $red;
private $green;
private $blue;
public function __construct($red, $green, $blue) {
$this->red = $red;
$this->green = $green;
$this->blue = $blue;
}
public static function fromRGB($color) {
$instance = new self($color[0], $color[1], $color[2]);
return $instance;
}
public static function fromHex($color) {
$rgb = str_split($color, 2); $instance = new self(hexdec($rgb[0]), hexdec($rgb[1]), hexdec($rgb[2]));
return $instance;
}
public function isHex($color) {
return ctype_xdigit($color);
}
public function isRGB($color) {
return is_array($color);
}
public function getRGB() {
return array($this->red, $this->green, $this->blue);
}
public function getHex() {
$hexRed = dechex($this->red);
$hexGreen = dechex($this->green);
$hexBlue = dechex($this->blue);
return strtoupper($hexRed . $hexGreen . $hexGreen);
}
public function incrementRGB(array $rgb) {
$result = array();
$result[0] = $this->incrementColor('red', $rgb[0]);
$result[1] = $this->incrementColor('green', $rgb[1]);
$result[2] = $this->incrementColor('blue', $rgb[2]);
return $result;
}
private function incrementColor($color, $quantity) {
$result = $this->$color + $quantity;
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment