Skip to content

Commit

Permalink
Fixed Color int conversion (#3)
Browse files Browse the repository at this point in the history
* Fixed Color int conversion

- Color::parseInt will only return RgbaColor (0xffffff is the same as 0xffffff00 in PHP, so an RGBA color with Alpha 0 would've been seen as an RgbColor (implying full alpha))
- Color::toInt will now use pack/unpack to build the int value to avoid platform difference problems
* Updated PHPUnit tests to work with travis again
* Remove HHVM and PHP5.6 Support (It's still installable, but it won't be tested anymore)
  • Loading branch information
TorbenKoehn authored May 27, 2017
1 parent e168b14 commit 7c57634
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 52 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ git:
depth: 5

php:
- 5.6
- 7.0
- hhvm

before_script:
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then phpenv config-rm xdebug.ini; fi;

install:
- composer self-update
- composer install --no-dev
- composer install --no-dev
37 changes: 9 additions & 28 deletions src/Phim/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -1319,41 +1319,22 @@ public static function parseHexString($string)
public static function toInt(ColorInterface $color)
{

if ($color instanceof AlphaColorInterface) {

$color = $color->toRgba();

return (int)(
+ ($color->getRed() << 24)
+ ($color->getGreen() << 16)
+ ($color->getBlue() << 8)
+ (int)($color->getAlpha() * 255)
);
}

$color = $color->toRgb();
return (int)(
+ ($color->getRed() << 16)
+ ($color->getGreen() << 8)
+ $color->getBlue()
);
return unpack('N', pack('C*',
$color->getRed(),
$color->getGreen(),
$color->getBlue(),
$color instanceof AlphaColorInterface ? (int)($color->getAlpha() * 255) : 1
))[1];
}

public static function parseInt($int)
{

if ($int > 0xffffff)
return new RgbaColor(
(int)(255 & ($int >> 24)),
(int)(255 & ($int >> 16)),
(int)(255 & ($int >> 8)),
(int)((255 & $int) / 255)
);

return new RgbColor(
return new RgbaColor(
(int)(255 & ($int >> 24)),
(int)(255 & ($int >> 16)),
(int)(255 & ($int >> 8)),
(int)(255 & ($int))
(int)((255 & $int) / 255)
);
}

Expand Down
37 changes: 16 additions & 21 deletions tests/unit/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,37 @@
namespace Phim\Test;

use Phim\Color;
use PHPUnit\Framework\TestCase;

class ColorTest extends \PHPUnit_Framework_TestCase
class ColorTest extends TestCase
{

public function testFunctionStringConversion()
{

$c = Color::get('hsl(50%, 50%, 100%)')->toHsl();

$this->assertEquals(180, $c->getHue());
self::assertEquals(180, $c->getHue());

$c = Color::get('hsl(-180, 50%, 100%)')->toHsl();

$this->assertEquals(180, $c->getHue());
self::assertEquals(180, $c->getHue());

$c = Color::get('hsl(-150%, 50%, 100%)')->toHsl();

$this->assertEquals(180, $c->getHue());
self::assertEquals(180, $c->getHue());

$c = Color::get('hsl(150%, 50%, 100%)')->toHsl();

$this->assertEquals(180, $c->getHue());
$this->assertEquals(.5, $c->getSaturation());
$this->assertEquals(1.0, $c->getLightness());
self::assertEquals(180, $c->getHue());
self::assertEquals(.5, $c->getSaturation());
self::assertEquals(1.0, $c->getLightness());

$c = Color::get('rgb(34.23%, 20%, 120)')->toRgb();

$this->assertEquals(87, $c->getRed());
$this->assertEquals(51, $c->getGreen());
$this->assertEquals(120, $c->getBlue());
self::assertEquals(87, $c->getRed());
self::assertEquals(51, $c->getGreen());
self::assertEquals(120, $c->getBlue());

$c = Color::get('hsla(0.872664626rad, .4, 90%, 22.3%)')->toHsla();

$this->assertEquals(50, $c->getHue());
$this->assertEquals(.4, $c->getSaturation());
$this->assertEquals(.9, $c->getLightness());
$this->assertEquals(.223, $c->getAlpha());
self::assertEquals(50, $c->getHue());
self::assertEquals(.4, $c->getSaturation());
self::assertEquals(.9, $c->getLightness());
self::assertEquals(.223, $c->getAlpha());
}
}
}

0 comments on commit 7c57634

Please sign in to comment.