Skip to content

Commit

Permalink
Support back to PHP 5.5
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealmond committed Jan 25, 2017
1 parent 9470348 commit d4f4558
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: php

php:
- '5.5'
- '5.6'
- '7.0'
- '7.1'

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.0.3 - 2017-01-24

### Changed
- Support back to PHP 5.5 (this will probably change for a v0.1.0 release)

## 0.0.2 - 2017-01-24

### Fixed
Expand Down
30 changes: 6 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# color

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
[![Quality Score][ico-code-quality]][link-code-quality]
[![Total Downloads][ico-downloads]][link-downloads]
[![Latest Version on Packagist](https://img.shields.io/packagist/v/mikealmond/color.svg?style=flat-square)](https://packagist.org/packages/mikealmond/color)
[![Build Status](https://img.shields.io/travis/mikealmond/color/master.svg?style=flat-square)](https://travis-ci.org/mikealmond/color)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/mikealmond/color.svg?style=flat-square)](https://scrutinizer-ci.com/g/mikealmond/color/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/g/mikealmond/color.svg?style=flat-square)](https://scrutinizer-ci.com/g/mikealmond/color)


This library will allow you to alter colors, check readability, and generate different palettes based on a base color.
Expand Down Expand Up @@ -66,25 +64,9 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Credits

- [Mike Almond][link-author]
- [All Contributors][link-contributors]
- [Mike Almond](https://github.com/mikealmond)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

[ico-version]: https://img.shields.io/packagist/v/mikealmond/color.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/mikealmond/color/master.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/mikealmond/color.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/mikealmond/color.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/mikealmond/color.svg?style=flat-square


[link-packagist]: https://packagist.org/packages/mikealmond/color
[link-travis]: https://travis-ci.org/mikealmond/color
[link-scrutinizer]: https://scrutinizer-ci.com/g/mikealmond/color/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/mikealmond/color
[link-downloads]: https://packagist.org/packages/mikealmond/color
[link-author]: https://github.com/mikealmond
[link-contributors]: ../../contributors
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}
],
"require": {
"php" : "~7.0"
"php" : "~5.5|~7.0"
},
"require-dev": {
"phpunit/phpunit" : "~4.0||~5.0",
Expand Down
6 changes: 3 additions & 3 deletions examples/css-colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<?php
$palette = $generator->monochromatic(5);
foreach ($palette as $color) {
printf('<div style="width:250px;height:15px;background-color:%s;color:%s;float:left;text-align:center; padding: 20px;">%s</div>',
printf('<div style="width:250px;height:15px;background-color:%s;color:%s;float:left;text-align:center;padding:20px;">%s</div>',
CssGenerator::hex($color),
CssGenerator::hex($color->getMatchingTextColor()),
CssGenerator::hasName($color) ? CssGenerator::name($color) : CssGenerator::hex($color)
Expand All @@ -35,7 +35,7 @@
$palette = $generator->tetrad(30);

foreach ($palette as $color) {
printf('<div style="width:250px;height:15px;background-color:%s;color:%s;float:left;text-align:center; padding: 20px;">%s</div>',
printf('<div style="width:250px;height:15px;background-color:%s;color:%s;float:left;text-align:center;padding:20px;">%s</div>',
CssGenerator::hex($color),
CssGenerator::hex($color->getMatchingTextColor()),
CssGenerator::hasName($color) ? CssGenerator::name($color) : CssGenerator::hex($color)
Expand All @@ -49,7 +49,7 @@
<div style="clear:both;margin:100px auto; width:876px;">
<?php
foreach (X11Colors::$colors as $hex => $colorNames) {
printf('<div style="width:250px;height:15px;background-color:%s;color:%s;border:1px solid %s;float:left;text-align:center; padding: 20px;">%s</div>',
printf('<div style="width:250px;height:15px;background-color:%s;color:%s;border:1px solid %s;float:left;text-align:center;padding:20px;">%s</div>',
CssGenerator::hex(Color::fromHex($hex)),
CssGenerator::hex(Color::fromHex($hex)->getMatchingTextColor()),
CssGenerator::hex(Color::fromHex($hex)->darken(100)),
Expand Down
4 changes: 2 additions & 2 deletions examples/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

require __DIR__ . '/../vendor/autoload.php';

$baseColor = Color::fromHex($_GET['color'] ?? 'C91414');
$baseColor = Color::fromHex(isset($_GET['color']) ? $_GET['color'] : 'C91414');
$generator = new PaletteGenerator($baseColor);
$distance = ($_GET['distance'] ?? 45);
$distance = isset($_GET['distance']) ? $_GET['distance'] : 45;

echo '<div style="float:left;margin:20px;">';

Expand Down
64 changes: 33 additions & 31 deletions src/Color.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types = 1);

namespace MikeAlmond\Color;

use MikeAlmond\Color\Exceptions\ColorException;
Expand Down Expand Up @@ -30,7 +28,7 @@ class Color implements \JsonSerializable
* @param $green
* @param $blue
*/
private function __construct(int $red, int $green, int $blue)
private function __construct($red, $green, $blue)
{
$this->colors = [
'r' => $red,
Expand All @@ -44,13 +42,15 @@ private function __construct(int $red, int $green, int $blue)
*
* @return Color
*/
public static function fromHex(string $color): Color
public static function fromHex($color)
{
if (!Validator::isValidHex($color)) {
throw new InvalidColorException('Invalid hex value');
}

return new self(...array_values(self::hexToRgb($color)));
$rgb = self::hexToRgb($color);

return new self($rgb['r'], $rgb['g'], $rgb['b']);
}

/**
Expand All @@ -60,7 +60,7 @@ public static function fromHex(string $color): Color
*
* @return Color
*/
public static function fromRgb(int $red, int $green, int $blue): Color
public static function fromRgb( $red, $green, $blue)
{
if (!Validator::isValidRgb($red, $green, $blue)) {
throw new InvalidColorException('Invalid RGB values');
Expand All @@ -76,21 +76,23 @@ public static function fromRgb(int $red, int $green, int $blue): Color
*
* @return Color
*/
public static function fromHsl($hue, $saturation, $lightness): Color
public static function fromHsl($hue, $saturation, $lightness)
{
if (!Validator::isValidHsl($hue, $saturation, $lightness)) {
throw new InvalidColorException('Invalid HSL value');
}

return new self(...array_values(self::hslToRgb($hue, $saturation, $lightness)));
$rgb = self::hslToRgb($hue, $saturation, $lightness);

return new self($rgb['r'], $rgb['g'], $rgb['b']);
}

/**
* @param $color
*
* @return Color
*/
public static function fromCssColor(string $color): Color
public static function fromCssColor($color)
{
return self::fromHex(X11Colors::search($color));
}
Expand All @@ -100,7 +102,7 @@ public static function fromCssColor(string $color): Color
*
* @return array
*/
private static function hexToRgb(string $color): array
private static function hexToRgb($color)
{
$color = ltrim($color, '#');

Expand Down Expand Up @@ -131,7 +133,7 @@ private static function hexToRgb(string $color): array
*
* @return mixed
*/
private static function hueToRgb($p, $q, $hue): float
private static function hueToRgb($p, $q, $hue)
{
if ($hue < 0) {
$hue += 1;
Expand Down Expand Up @@ -167,7 +169,7 @@ private static function hueToRgb($p, $q, $hue): float
*
* @return array
*/
private static function hslToRgb($hue, $saturation, $lightness): array
private static function hslToRgb($hue, $saturation, $lightness)
{
// If saturation is 0, the given color is grey and only
// lightness is relevant.
Expand Down Expand Up @@ -203,15 +205,15 @@ private static function hslToRgb($hue, $saturation, $lightness): array
*
* @return bool
*/
public function isReadable(Color $color): bool
public function isReadable(Color $color)
{
return $this->brightnessDifference($color) >= 100;
}

/**
* @return bool
*/
public function isDark(): bool
public function isDark()
{
return $this->getBrightness() < 136;
}
Expand All @@ -221,7 +223,7 @@ public function isDark(): bool
*
* @return bool
*/
public function equals(Color $color): bool
public function equals(Color $color)
{
return $this->getHex() == $color->getHex();
}
Expand All @@ -230,7 +232,7 @@ public function equals(Color $color): bool
*
* @return float
*/
public function getLuminosity(): float
public function getLuminosity()
{
return 0.2126 * pow($this->colors['r'] / 255, 2.2)
+ 0.7152 * pow($this->colors['g'] / 255, 2.2)
Expand All @@ -243,7 +245,7 @@ public function getLuminosity(): float
*
* @return float
*/
public function getBrightness(): float
public function getBrightness()
{
return (($this->colors['r'] * 299) + ($this->colors['g'] * 587) + ($this->colors['b'] * 114)) / 1000;
}
Expand All @@ -256,7 +258,7 @@ public function getBrightness(): float
*
* @return int
*/
public function colorDifference(Color $color): int
public function colorDifference(Color $color)
{
$color2 = $color->getRgb();

Expand All @@ -273,7 +275,7 @@ public function colorDifference(Color $color): int
*
* @return float
*/
public function brightnessDifference(Color $color): float
public function brightnessDifference(Color $color)
{
$difference = $this->getBrightness() - $color->getBrightness();

Expand All @@ -292,7 +294,7 @@ public function brightnessDifference(Color $color): float
*
* @return float
*/
public function luminosityContrast(Color $color): float
public function luminosityContrast(Color $color)
{
$colorLuminosity1 = $this->getLuminosity() + 0.05;
$colorLuminosity2 = $color->getLuminosity() + 0.05;
Expand All @@ -303,7 +305,7 @@ public function luminosityContrast(Color $color): float
/**
* @return string
*/
public function getHex(): string
public function getHex()
{
return sprintf(
'%02X%02X%02X',
Expand All @@ -316,7 +318,7 @@ public function getHex(): string
/**
* @return int[]
*/
public function getRgb(): array
public function getRgb()
{
return $this->colors;
}
Expand All @@ -327,7 +329,7 @@ public function getRgb(): array
*
* @return array
*/
public function getHsl(): array
public function getHsl()
{
$red = $this->colors['r'] / 255;
$green = $this->colors['g'] / 255;
Expand Down Expand Up @@ -408,37 +410,37 @@ public function getMatchingTextColor()
*
* @return Color
*/
public function darken($percentage): Color
public function darken($percentage)
{
$colors = $this->getHsl();
$colors['l'] -= $colors['l'] * ($percentage / 100);

$darkerColor = self::hslToRgb($colors['h'], $colors['s'], max(round($colors['l'], 5), 0));

return new self(...array_values($darkerColor));
return new self($darkerColor['r'], $darkerColor['g'], $darkerColor['b']);
}

/**
* @param $percentage
*
* @return Color
*/
public function lighten($percentage): Color
public function lighten($percentage)
{
$colors = $this->getHsl();
$colors['l'] += $colors['l'] * ($percentage / 100);

$lighterColor = self::hslToRgb($colors['h'], $colors['s'], min(round($colors['l'], 5), 1));

return new self(...array_values($lighterColor));
return new self($lighterColor['r'], $lighterColor['g'], $lighterColor['b']);
}

/**
* @param $degrees
*
* @return Color
*/
public function adjustHue($degrees = 30): Color
public function adjustHue($degrees = 30)
{
if (!Validator::isValidAdjustment($degrees)) {
throw new ColorException('You must specify a proper value between 360 and -360');
Expand All @@ -457,21 +459,21 @@ public function adjustHue($degrees = 30): Color

$adjustedColor = self::hslToRgb($hue / 360, $colors['s'], $colors['l']);

return new self(...array_values($adjustedColor));
return new self($adjustedColor['r'], $adjustedColor['g'], $adjustedColor['b']);
}

/**
* @return string
*/
public function __toString(): string
public function __toString()
{
return $this->getHex();
}

/**
* @return array
*/
public function jsonSerialize(): array
public function jsonSerialize()
{
return $this->colors;
}
Expand Down
Loading

0 comments on commit d4f4558

Please sign in to comment.