Skip to content

Fix issues, add CIE XYBri support, add Initial PHPUnit tests and Travis support. #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
28f1d98
add branch alias
fahmiardi Jun 28, 2014
eefe63c
fixed invalid annoation
dVelopment Aug 14, 2014
5a30b40
Merge pull request #2 from fahmiardi/master
Feb 8, 2015
27348da
Merge pull request #1 from CodeLoversAt/master
Feb 8, 2015
f25ddaa
Assumption of maintainance.
matthewbaggett Feb 8, 2015
cfe9cb6
Add support for Hex Strings and CIE XYBri values.
matthewbaggett Feb 8, 2015
cfa985b
Initial test code.. doesn't work yet.
matthewbaggett Feb 8, 2015
17f161c
caps output
matthewbaggett Feb 8, 2015
eaa0813
Test hex input/output.
matthewbaggett Feb 8, 2015
deb8d7a
Add Travis YML. Remove CIETest for now.
matthewbaggett Feb 8, 2015
85b2dde
Almost-bare phpunit.xml
matthewbaggett Feb 8, 2015
c2467d5
remove short array syntax for 5.3 support
matthewbaggett Feb 8, 2015
a594316
Add composer.lock
matthewbaggett Feb 8, 2015
ea4a4c1
Adding phpunit.xml
matthewbaggett Feb 8, 2015
50eb070
Bad Travis config
matthewbaggett Feb 8, 2015
fd047f6
Add build icon.
matthewbaggett Feb 8, 2015
aa3fbbe
add composer install step.
matthewbaggett Feb 8, 2015
77d2276
Survive /0
matthewbaggett Feb 8, 2015
92cc2db
Actual tests! Need to work out why XYBri has such a sucky accuracy is…
matthewbaggett Feb 8, 2015
60c44ae
Remove Comment.
matthewbaggett Feb 8, 2015
5d4a3ec
Not using $this->color
matthewbaggett Feb 8, 2015
e55d927
This could have never worked. Neat. Tests ftw.
matthewbaggett Feb 8, 2015
9f59463
Improving code coverage
matthewbaggett Feb 8, 2015
3e0f615
Refactor - This is already implemented.
matthewbaggett Feb 8, 2015
d7fe90d
gitignore added
matthewbaggett Feb 8, 2015
829fe2b
Turns out the existing way to do toRgbString() is bad.
matthewbaggett Feb 9, 2015
a55f717
Updated to support code-climate.
matthewbaggett Feb 9, 2015
81d1b29
Put dependency in the wrong place. Oops.
matthewbaggett Feb 9, 2015
d576bd3
Add codeclimate/php-test-reporter to composer.lock
matthewbaggett Feb 9, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
vendor
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: php

php: 5.3

before_script :
- composer install

after_script:
- CODECLIMATE_REPO_TOKEN="34ea4cdfa1a1b0da0a5bf10664b2dccf295c5bde0932bf7dd4e4fd39d590d28d" vendor/bin/test-reporter --stdout > codeclimate.json
- "curl -X POST -d @codeclimate.json -H 'Content-Type: application/json' -H 'User-Agent: Code Climate (PHP Test Reporter v0.1.1)' https://codeclimate.com/test_reports"

script: phpunit --configuration phpunit.xml --coverage-text

notifications:
email:
- [email protected]
139 changes: 121 additions & 18 deletions Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ public function fromRgbHex($red, $green, $blue)
{
return $this->fromRgbInt(hexdec($red), hexdec($green), hexdec($blue));
}

/**
* Init color from a hex RGB string (#00FF00)
*
* @param $hex
* @return Color
*/
public function fromRgbString($hex)
{
$hex = str_replace("#", "", $hex);

if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}

return $this->fromRgbInt($r, $g, $b);
}

/**
* Init color from integer value
Expand All @@ -85,7 +108,37 @@ public function fromInt($intValue)

return $this;
}


/**
* Init color from a CIE XYBri value
*
* @param $x
* @param $y
* @param $brightness
* @return Color
*/
public function fromXYBri($x, $y, $brightness)
{
$_x = ($x * $brightness) / $y;
$_y = $brightness;
$_z = ((1 - $x - $y) * $brightness) / $y;

$r = $_x * 3.2406 + $_y * -1.5372 + $_z * -0.4986;
$g = $_x * -0.9689 + $_y * 1.8758 + $_z * 0.0415;
$b = $_x * 0.0557 + $_y * -0.2040 + $_z * 1.0570;

$r = $r > 0.0031308 ? 1.055 * pow($r, 1 / 2.4) - 0.055 : 12.92 * $r;
$g = $g > 0.0031308 ? 1.055 * pow($g, 1 / 2.4) - 0.055 : 12.92 * $g;
$b = $b > 0.0031308 ? 1.055 * pow($b, 1 / 2.4) - 0.055 : 12.92 * $b;

$r = $r > 0 ? round($r * 255) : 0;
$g = $g > 0 ? round($g * 255) : 0;
$b = $b > 0 ? round($b * 255) : 0;

return $this->fromRgbInt($r, $g, $b);
}


/**
* Convert color to hex
*
Expand Down Expand Up @@ -121,7 +174,21 @@ public function toRgbHex()
return dechex($item);
}, $this->toRgbInt());
}


/**
* Convert color to an RGB Hex string (#00FF00)
*
* @return string
*/
public function toRgbString()
{
$hexes = $this->toRgbHex();
array_walk($hexes, function(&$hex, $key){
$hex = str_pad($hex,2,"0",STR_PAD_LEFT);
});
return "#" . strtoupper(implode('', $hexes));
}

/**
* Get Hue/Saturation/Value for the current color
* (float values, slow but accurate)
Expand Down Expand Up @@ -292,7 +359,7 @@ public function toLabCie()

return $lab;
}

/**
* Convert color to integer
*
Expand All @@ -302,7 +369,47 @@ public function toInt()
{
return $this->color;
}


/**
* Convert color to a CIE XYBri array.
*
* @return array
* @throws Exception
*/
public function toXYBri(){
$rgb = $this->toRgbInt();

$r = $rgb['red'];
$g = $rgb['green'];
$b = $rgb['blue'];

$r = $r / 255;
$g = $g / 255;
$b = $b / 255;

if ($r < 0 || $r > 1 || $g < 0 || $g > 1 || $b < 0 || $b > 1) {
throw new \Exception("Invalid RGB array. [{$r},{$b},{$g}]");
}

$rt = ($r > 0.04045) ? pow(($r + 0.055) / (1.0 + 0.055), 2.4) : ($r / 12.92);
$gt = ($g > 0.04045) ? pow(($g + 0.055) / (1.0 + 0.055), 2.4) : ($g / 12.92);
$bt = ($b > 0.04045) ? pow(($b + 0.055) / (1.0 + 0.055), 2.4) : ($b / 12.92);

$cie_x = $rt * 0.649926 + $gt * 0.103455 + $bt * 0.197109;
$cie_y = $rt * 0.234327 + $gt * 0.743075 + $bt * 0.022598;
$cie_z = $rt * 0.0000000 + $gt * 0.053077 + $bt * 1.035763;

if($cie_x + $cie_y + $cie_z == 0){
$hue_x = 0.1;
$hue_y = 0.1;
}else {
$hue_x = $cie_x / ($cie_x + $cie_y + $cie_z);
$hue_y = $cie_y / ($cie_x + $cie_y + $cie_z);
}
return array('x'=>$hue_x,'y'=>$hue_y,'bri'=>$cie_y);
}


/**
* Alias of toString()
*
Expand All @@ -320,18 +427,14 @@ public function __toString()
*/
public function toString()
{
$str = (string)$this->toHex();
if (strlen($str) < 6) {
$str = str_pad($str, 6, '0', STR_PAD_LEFT);
}
return strtoupper("#{$str}");
return $this->toRgbString();
}

/**
* Get the distance between this color and the given color
*
* @param Color $color
*
*
* @param Color $color
*
* @return int
*/
public function getDistanceRgbFrom(Color $color)
Expand All @@ -350,9 +453,9 @@ public function getDistanceRgbFrom(Color $color)

/**
* Get distance from the given color using the Delta E method
*
* @param Color $color
*
*
* @param Color $color
*
* @return float
*/
public function getDistanceLabFrom(Color $color)
Expand All @@ -372,7 +475,7 @@ public function getDistanceLabFrom(Color $color)
/**
* Detect if color is grayscale
*
* @param int @threshold
* @param int $threshold
*
* @return bool
*/
Expand Down Expand Up @@ -401,9 +504,9 @@ public function getClosestMatch(array $colors)
$matchKey = null;
foreach($colors as $key => $color) {
if (false === ($color instanceof Color)) {
$c = new Color($color);
$color = new Color($color);
}
$dist = $this->getDistanceLabFrom($c);
$dist = $this->getDistanceLabFrom($color);
if ($dist < $matchDist) {
$matchDist = $dist;
$matchKey = $key;
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# PHP Color Utility Class

![Travis CI](https://travis-ci.org/matthewbaggett/php-color.svg) [![Code Climate](https://codeclimate.com/github/matthewbaggett/php-color/badges/gpa.svg)](https://codeclimate.com/github/matthewbaggett/php-color) [![Test Coverage](https://codeclimate.com/github/matthewbaggett/php-color/badges/coverage.svg)](https://codeclimate.com/github/matthewbaggett/php-color)

This class is intended to make it easier to convert between colorspaces,
as well as compare one color to another.

Expand Down
23 changes: 20 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
{
"name": "hasbridge/php-color",
"name": "matthewbaggett/php-color",
"description": "Color utility class for PHP 5.3 that allows for easy conversion between RGB, HSV, XYZ, and Lab colorspaces, as well as color comparison",
"license": "MIT",
"autoload": {
"psr-0": {"Color": ""}
"psr-0": {
"Color": ""
}
},
"require": {
"php": "~5.3"
}
},
"require-dev": {
"phpunit/phpunit": "*",
"codeclimate/php-test-reporter": "dev-master"
},
"extra": {
"branch-alias": {
"dev-master": "0.1-dev"
}
},
"repositories": [
{
"type": "git",
"url": "https://github.com/matthewbaggett/php-color"
}
]
}
Loading