Skip to content

Commit 7c57634

Browse files
authored
Fixed Color int conversion (#3)
* 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)
1 parent e168b14 commit 7c57634

File tree

3 files changed

+26
-52
lines changed

3 files changed

+26
-52
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ git:
55
depth: 5
66

77
php:
8-
- 5.6
98
- 7.0
10-
- hhvm
119

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

1513
install:
1614
- composer self-update
17-
- composer install --no-dev
15+
- composer install --no-dev

src/Phim/Color.php

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,41 +1319,22 @@ public static function parseHexString($string)
13191319
public static function toInt(ColorInterface $color)
13201320
{
13211321

1322-
if ($color instanceof AlphaColorInterface) {
1323-
1324-
$color = $color->toRgba();
1325-
1326-
return (int)(
1327-
+ ($color->getRed() << 24)
1328-
+ ($color->getGreen() << 16)
1329-
+ ($color->getBlue() << 8)
1330-
+ (int)($color->getAlpha() * 255)
1331-
);
1332-
}
1333-
1334-
$color = $color->toRgb();
1335-
return (int)(
1336-
+ ($color->getRed() << 16)
1337-
+ ($color->getGreen() << 8)
1338-
+ $color->getBlue()
1339-
);
1322+
return unpack('N', pack('C*',
1323+
$color->getRed(),
1324+
$color->getGreen(),
1325+
$color->getBlue(),
1326+
$color instanceof AlphaColorInterface ? (int)($color->getAlpha() * 255) : 1
1327+
))[1];
13401328
}
13411329

13421330
public static function parseInt($int)
13431331
{
13441332

1345-
if ($int > 0xffffff)
1346-
return new RgbaColor(
1347-
(int)(255 & ($int >> 24)),
1348-
(int)(255 & ($int >> 16)),
1349-
(int)(255 & ($int >> 8)),
1350-
(int)((255 & $int) / 255)
1351-
);
1352-
1353-
return new RgbColor(
1333+
return new RgbaColor(
1334+
(int)(255 & ($int >> 24)),
13541335
(int)(255 & ($int >> 16)),
13551336
(int)(255 & ($int >> 8)),
1356-
(int)(255 & ($int))
1337+
(int)((255 & $int) / 255)
13571338
);
13581339
}
13591340

tests/unit/ColorTest.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,37 @@
33
namespace Phim\Test;
44

55
use Phim\Color;
6+
use PHPUnit\Framework\TestCase;
67

7-
class ColorTest extends \PHPUnit_Framework_TestCase
8+
class ColorTest extends TestCase
89
{
910

1011
public function testFunctionStringConversion()
1112
{
1213

1314
$c = Color::get('hsl(50%, 50%, 100%)')->toHsl();
14-
15-
$this->assertEquals(180, $c->getHue());
15+
self::assertEquals(180, $c->getHue());
1616

1717
$c = Color::get('hsl(-180, 50%, 100%)')->toHsl();
18-
19-
$this->assertEquals(180, $c->getHue());
18+
self::assertEquals(180, $c->getHue());
2019

2120
$c = Color::get('hsl(-150%, 50%, 100%)')->toHsl();
22-
23-
$this->assertEquals(180, $c->getHue());
21+
self::assertEquals(180, $c->getHue());
2422

2523
$c = Color::get('hsl(150%, 50%, 100%)')->toHsl();
26-
27-
$this->assertEquals(180, $c->getHue());
28-
$this->assertEquals(.5, $c->getSaturation());
29-
$this->assertEquals(1.0, $c->getLightness());
24+
self::assertEquals(180, $c->getHue());
25+
self::assertEquals(.5, $c->getSaturation());
26+
self::assertEquals(1.0, $c->getLightness());
3027

3128
$c = Color::get('rgb(34.23%, 20%, 120)')->toRgb();
32-
33-
$this->assertEquals(87, $c->getRed());
34-
$this->assertEquals(51, $c->getGreen());
35-
$this->assertEquals(120, $c->getBlue());
29+
self::assertEquals(87, $c->getRed());
30+
self::assertEquals(51, $c->getGreen());
31+
self::assertEquals(120, $c->getBlue());
3632

3733
$c = Color::get('hsla(0.872664626rad, .4, 90%, 22.3%)')->toHsla();
38-
39-
$this->assertEquals(50, $c->getHue());
40-
$this->assertEquals(.4, $c->getSaturation());
41-
$this->assertEquals(.9, $c->getLightness());
42-
$this->assertEquals(.223, $c->getAlpha());
34+
self::assertEquals(50, $c->getHue());
35+
self::assertEquals(.4, $c->getSaturation());
36+
self::assertEquals(.9, $c->getLightness());
37+
self::assertEquals(.223, $c->getAlpha());
4338
}
44-
}
39+
}

0 commit comments

Comments
 (0)