Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 6fbd264fcb
Fetching contributors…

Cannot retrieve contributors at this time

119 lines (80 sloc) 3.641 kb

Last Build: Build Status

How it works

Instantiate an object of the color class with a hex color string $foo = new Color("336699"). That's it! Now, call the methods you need for different color variants.

Available Methods

  • darken( [$amount] ) : Allows you to obtain a darker shade of your color. Optionally you can decide to darken using a desired percentage.
  • lighten( [$amount] ) : Allows you to obtain a lighter shade of your color. Optionally you can decide to lighten using a desired percentage.
  • isLight( [$hex] ) : Determins whether your color (or the provide param) is considered a "light" color. Returns TRUE if color is light.
  • isDark( [$hex] ) : Determins whether your color (or the provide param) is considered a "dark" color. Returns TRUE if color is dark.
  • makeGradient( [$amount] ) : Returns an array with 2 indices light and dark, the initial color will either be selected for light or dark dependings on it's brightness, then the other color will be generated. The optional param allows for a static lighten or darkened amount.
  • complementary() : Returns the color "opposite" or complementary to your color.
  • getHex() : Returns the original hex color.
  • getHsl() : Returns HSL array for your color.

Auto lightens/darkens by 10% for sexily-subtle gradients

/**
 * Using The Class
 */

using phpColors\Color;

// Initialize my color
$myBlue = new Color("#336699");

echo $myBlue->darken();
// 1a334d

echo $myBlue->lighten(); 
// 8cb3d9

echo $myBlue->isLight();
// false

echo $myBlue->isDark();
// true

echo $myBlue->complementary();
// 996633

echo $myBlue->getHex();
// 336699

print_r( $myBlue->getHsl() );
// array( "H"=> 210, "S"=> 0.5, "L"=>0.4 );

print_r($myBlue->makeGradient());
// array( "light"=>"8cb3d9" ,"dark"=>"336699" )

Static Methods

  • hslToHex( $hsl ) : Convert a HSL array to a HEX string.
  • hexToHsl( $hex ) : Convert a HEX string into an HSL array.
/**
 * On The Fly Custom Calculations
 */

using phpColors\Color;

 // Convert my HEX
 $myBlue = Color::hexToHsl("#336699");

 // Get crazy with the HUE
 $myBlue["H"] = 295;

 // Gimme my new color!!
 echo Color::hslToHex($myBlue);
 // 913399

CSS Helpers

  • getCssGradient( [$amount] ) : Generates the CSS3 gradients for safari, chrome, opera, firefox and IE10. Optional percentage amount for lighter/darker shade.

Would like to add support to custom gradient stops


using phpColors\Color;

// Initialize my color
$myBlue = new Color("#336699");

// Get CSS
echo $myBlue->getCssGradient();
/* - Actual output doesn't have comments and is single line

  // fallback background
  background: #336699;

  /* IE Browsers */
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8cb3d9', endColorstr='#336699'); 

  // Safari 4+, Chrome 1-9
  background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#8cb3d9), to(#336699));

  // Safari 5.1+, Mobile Safari, Chrome 10+
  background-image: -webkit-linear-gradient(top, #8cb3d9, #336699);

  // Firefox 3.6+
  background-image: -moz-linear-gradient(top, #8cb3d9, #336699);

  // IE 10+
  background-image: -ms-linear-gradient(top, #8cb3d9, #336699);

  // Opera 11.10+
  background-image: -o-linear-gradient(top, #8cb3d9, #336699);

*/

License: arlo.mit-license.org

Jump to Line
Something went wrong with that request. Please try again.