Skip to content

Instantly share code, notes, and snippets.

@sebacruz
Created June 1, 2016 00:14
Show Gist options
  • Select an option

  • Save sebacruz/e6049f5e176fed5ca6daa5ee95edc509 to your computer and use it in GitHub Desktop.

Select an option

Save sebacruz/e6049f5e176fed5ca6daa5ee95edc509 to your computer and use it in GitHub Desktop.
<?php
class ColorGenerator {
private static $startColor;
private static $endColor;
private static $steps;
private static $colors;
private static function interpolate(
$pBegin, $pEnd, $pStep, $pMax
) {
if ( $pBegin < $pEnd ) {
return ( ( $pEnd - $pBegin ) * ( $pStep / $pMax ) ) + $pBegin;
} else {
return ( ( $pBegin - $pEnd ) * ( 1 - ( $pStep / $pMax ) ) ) + $pEnd;
}
}
public static function generate( $start, $end, $steps )
{
self::$startColor = hexdec( '0x' . str_replace( '#', '', $start ) );
self::$endColor = hexdec( '0x' . str_replace( '#', '', $end ) );
self::$startColor = ( ( self::$startColor >= 0x000000 ) && ( self::$startColor <= 0xffffff ) ) ?
self::$startColor : 0x000000;
self::$endColor = ( ( self::$endColor >= 0x000000 ) && ( self::$endColor <= 0xffffff ) ) ?
self::$endColor : 0xffffff;
self::$steps = ( ( $steps > 0 ) && ( $steps < 256 ) ) ? --$steps : 16;
if (self::$steps > 0) {
$theR0 = ( self::$startColor & 0xff0000 ) >> 16;
$theG0 = ( self::$startColor & 0x00ff00 ) >> 8;
$theB0 = ( self::$startColor & 0x0000ff ) >> 0;
$theR1 = ( self::$endColor & 0xff0000 ) >> 16;
$theG1 = ( self::$endColor & 0x00ff00 ) >> 8;
$theB1 = ( self::$endColor & 0x0000ff ) >> 0;
for ( $i = 0; $i <= self::$steps; $i ++ ) {
$theR = self::interpolate( $theR0, $theR1, $i, self::$steps );
$theG = self::interpolate( $theG0, $theG1, $i, self::$steps );
$theB = self::interpolate( $theB0, $theB1, $i, self::$steps );
$color = ( ( ( $theR << 8 ) | $theG ) << 8 ) | $theB;
self::$colors[] = str_pad( dechex( $color ), 6, '0', STR_PAD_LEFT );
}
} else {
self::$colors[] = str_pad( dechex( self::$startColor ), 6, '0', STR_PAD_LEFT );
}
return self::$colors;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment