Added colour library tests.

This commit is contained in:
flash 2023-01-02 22:22:37 +00:00
parent 05a940667c
commit a24cfb8e6f
6 changed files with 546 additions and 17 deletions

View file

@ -1 +1 @@
0.2301.20324
0.2301.22222

View file

@ -6,6 +6,7 @@
namespace Index\Colour;
use Stringable;
abstract class Colour implements Stringable {
public abstract function getRed(): int;
public abstract function getGreen(): int;
@ -16,18 +17,20 @@ abstract class Colour implements Stringable {
public abstract function __toString(): string;
private const READABILITY_THRESHOLD = 186;
private const READABILITY_THRESHOLD = 186.0;
private const LUMINANCE_WEIGHT_RED = .299;
private const LUMINANCE_WEIGHT_GREEN = .587;
private const LUMINANCE_WEIGHT_BLUE = .114;
// luminance shit might need further review
public function getLuminance(): float {
return self::LUMINANCE_WEIGHT_RED * $this->getRed()
+ self::LUMINANCE_WEIGHT_GREEN * $this->getGreen()
+ self::LUMINANCE_WEIGHT_BLUE * $this->getBlue();
}
public function isBright(): bool {
public function isLight(): bool {
return $this->getLuminance() > self::READABILITY_THRESHOLD;
}
@ -57,7 +60,7 @@ abstract class Colour implements Stringable {
if($value === '' || $value === 'inherit')
return self::$none;
if(ctype_alpha($value))
if(ctype_alpha($value) && ColourNamed::isValidName($value))
return new ColourNamed($value);
if($value[0] === '#') {
@ -134,11 +137,17 @@ abstract class Colour implements Stringable {
if($parts !== 3 && $parts !== 4)
return self::$none;
if(!str_ends_with($value[1], '%') || !str_ends_with($value[2], '%'))
return self::$none;
for($i = 0; $i < $parts; ++$i)
$value[$i] = trim($value[$i]);
$value[1] = (float)substr($value[1], 0, -1);
$value[2] = (float)substr($value[2], 0, -1);
if(str_ends_with($value[1], '%'))
$value[1] = substr($value[1], 0, -1);
if(str_ends_with($value[2], '%'))
$value[2] = substr($value[2], 0, -1);
$value[1] = (float)$value[1];
$value[2] = (float)$value[2];
if($value[1] < 0 || $value[1] > 100 || $value[2] < 0 || $value[2] > 100)
return self::$none;
@ -154,7 +163,7 @@ abstract class Colour implements Stringable {
} elseif(str_ends_with($value[0], 'grad')) {
$value[0] = 0.9 * (float)substr($value[0], 0, -4);
} elseif(str_ends_with($value[0], 'rad')) {
$value[0] = rad2deg((float)substr($value[0], 0, -3));
$value[0] = round(rad2deg((float)substr($value[0], 0, -3)));
} elseif(str_ends_with($value[0], 'turn')) {
$value[0] = 360.0 * ((float)substr($value[0], 0, -4));
} else {

View file

@ -16,8 +16,6 @@ class ColourHSL extends Colour {
private int $blue;
public function __construct(float $hue, float $saturation, float $lightness, float $alpha) {
$hue %= 360;
$this->hue = $hue;
$this->saturation = max(0.0, min(1.0, $saturation));
$this->lightness = max(0.0, min(1.0, $lightness));
@ -87,9 +85,16 @@ class ColourHSL extends Colour {
}
public function __toString(): string {
if($this->alpha < 1.0)
return sprintf('hsla(%.2Fdeg, %d%%, %d%%, %.3F)', $this->hue, $this->saturation * 100, $this->lightness * 100, $this->alpha);
return sprintf('hsl(%.2Fdeg, %d%%, %d%%)', $this->hue, $this->saturation * 100, $this->lightness * 100);
$hue = (string)round($this->hue, 2);
$sat = (string)round($this->saturation * 100);
$lig = (string)round($this->lightness * 100);
if($this->alpha < 1.0) {
$alpha = (string)round($this->alpha, 3);
return sprintf('hsla(%sdeg, %s%%, %s%%, %s)', $hue, $sat, $lig, $alpha);
}
return sprintf('hsl(%sdeg, %s%%, %s%%)', $hue, $sat, $lig);
}
public static function convert(Colour $colour): ColourHSL {

View file

@ -49,7 +49,7 @@ class ColourNamed extends Colour {
}
public function getAlpha(): float {
return $this->transparent ? 0.0 : 1.0;;
return $this->transparent ? 0.0 : 1.0;
}
public function shouldInherit(): bool {
@ -60,6 +60,14 @@ class ColourNamed extends Colour {
return $this->name;
}
public static function getNames(): array {
return array_keys(self::COLOURS);
}
public static function isValidName(string $name): bool {
return array_key_exists($name, self::COLOURS);
}
private const COLOURS = [
// CSS Level 1
'black' => 0x000000,

View file

@ -39,8 +39,10 @@ class ColourRGB extends Colour {
}
public function __toString(): string {
if($this->alpha < 1.0)
return sprintf('rgba(%d, %d, %d, %.3F)', $this->red, $this->green, $this->blue, $this->alpha);
if($this->alpha < 1.0) {
$alpha = (string)round($this->alpha, 3);
return sprintf('rgba(%d, %d, %d, %s)', $this->red, $this->green, $this->blue, $alpha);
}
return sprintf('#%02x%02x%02x', $this->red, $this->green, $this->blue);
}

505
tests/ColourTest.php Normal file
View file

@ -0,0 +1,505 @@
<?php
// ColourTest.php
// Created: 2023-01-02
// Updated: 2023-01-02
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use Index\Colour\Colour;
use Index\Colour\ColourHSL;
use Index\Colour\ColourNamed;
use Index\Colour\ColourNull;
use Index\Colour\ColourRGB;
/**
* @covers Colour
* @covers ColourHSL
* @covers ColourNamed
* @covers ColourNull
* @covers ColourRGB
*/
final class ColourTest extends TestCase {
public function testNone(): void {
$none = Colour::none();
$this->assertInstanceOf(ColourNull::class, $none);
$this->assertTrue($none->shouldInherit());
$this->assertEquals('inherit', $none);
}
public function testLuminance(): void {
for($i = 0; $i < 0xB; ++$i) {
$num = ($i << 4) | $i;
$colour = new ColourRGB($num, $num, $num, 1);
$this->assertTrue($colour->isDark());
$this->assertFalse($colour->isLight());
}
for($i = 0xB; $i < 0x10; ++$i) {
$num = ($i << 4) | $i;
$colour = new ColourRGB($num, $num, $num, 1);
$this->assertFalse($colour->isDark());
$this->assertTrue($colour->isLight());
}
}
public function testNamed(): void {
$names = [
['red', 0xFF, 0, 0, false],
['orange', 0xFF, 0xA5, 0, false],
['tan', 0xD2, 0xB4, 0x8C, false],
['rebeccapurple', 0x66, 0x33, 0x99, false],
['transparent', 0, 0, 0, true],
['xthiswillneverexist', 0, 0, 0, true],
];
foreach($names as $name) {
$colour = new ColourNamed($name[0]);
$this->assertEquals($name[0], $colour->getName());
$this->assertEquals($name[1], $colour->getRed());
$this->assertEquals($name[2], $colour->getGreen());
$this->assertEquals($name[3], $colour->getBlue());
if($name[4]) {
$this->assertTrue($colour->isTransparent());
$this->assertEquals(0.0, $colour->getAlpha());
} else {
$this->assertFalse($colour->isTransparent());
$this->assertEquals(1.0, $colour->getAlpha());
}
$this->assertFalse($colour->shouldInherit());
$this->assertInstanceOf(ColourNamed::class, $colour);
$this->assertEquals($name[0], $name[0]);
}
}
public function testRGB(): void {
$colour = new ColourRGB(31, 120, 50, 1.0);
$this->assertEquals(31, $colour->getRed());
$this->assertEquals(120, $colour->getGreen());
$this->assertEquals(50, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('#1f7832', $colour);
$colour = ColourRGB::fromRawARGB(0xCCFF7A7F);
$this->assertEquals(255, $colour->getRed());
$this->assertEquals(122, $colour->getGreen());
$this->assertEquals(127, $colour->getBlue());
$this->assertEquals(.8, $colour->getAlpha());
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('rgba(255, 122, 127, 0.8)', $colour);
$colour = ColourRGB::fromRawRGB(0x4D3380);
$this->assertEquals(77, $colour->getRed());
$this->assertEquals(51, $colour->getGreen());
$this->assertEquals(128, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('#4d3380', $colour);
$colour = ColourRGB::fromRawRGBA(0xFF7A7F33);
$this->assertEquals(255, $colour->getRed());
$this->assertEquals(122, $colour->getGreen());
$this->assertEquals(127, $colour->getBlue());
$this->assertEquals(.2, $colour->getAlpha());
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('rgba(255, 122, 127, 0.2)', $colour);
}
public function testConvertToRGB(): void {
$colourA = new ColourRGB(31, 120, 50, 1.0);
$colourB = ColourRGB::convert($colourA);
$this->assertSame($colourA, $colourB); // converting to the same type should passthru
$colourA = Colour::none();
$colourB = ColourRGB::convert($colourA); // converting none to RGB makes very little sense, but you can do it if you want...
$this->assertEquals(0, $colourB->getRed());
$this->assertEquals(0, $colourB->getGreen());
$this->assertEquals(0, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha());
$this->assertInstanceOf(ColourRGB::class, $colourB);
$this->assertFalse($colourB->shouldInherit()); // it does lose the inherit flag, which is intentional
$this->assertFalse($colourB->isLight());
$this->assertTrue($colourB->isDark());
$this->assertEquals('#000000', $colourB);
$colourA = new ColourNamed('steelblue');
$colourB = ColourRGB::convert($colourA);
$this->assertEquals(0x46, $colourB->getRed());
$this->assertEquals(0x82, $colourB->getGreen());
$this->assertEquals(0xB4, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha());
$this->assertInstanceOf(ColourRGB::class, $colourB);
$this->assertFalse($colourB->shouldInherit());
$this->assertFalse($colourB->isLight());
$this->assertTrue($colourB->isDark());
$this->assertEquals('#4682b4', $colourB);
$colourA = new ColourHSL(50, .8, .4, 1.0);
$colourB = ColourRGB::convert($colourA);
$this->assertEquals(0xB8, $colourB->getRed());
$this->assertEquals(0x9C, $colourB->getGreen());
$this->assertEquals(0x14, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha());
$this->assertInstanceOf(ColourRGB::class, $colourB);
$this->assertFalse($colourB->shouldInherit());
$this->assertFalse($colourB->isLight());
$this->assertTrue($colourB->isDark());
$this->assertEquals('#b89c14', $colourB);
}
public function testHSL(): void {
$colour = new ColourHSL(50, .8, .4, 1.0);
$this->assertEquals(50, $colour->getHue());
$this->assertEquals(.8, $colour->getSaturation());
$this->assertEquals(.4, $colour->getLightness());
$this->assertEquals(0xB8, $colour->getRed());
$this->assertEquals(0x9C, $colour->getGreen());
$this->assertEquals(0x14, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertInstanceOf(ColourHSL::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('hsl(50deg, 80%, 40%)', $colour);
$colour = new ColourHSL(150, .3, .6, 1.0);
$this->assertEquals(150, $colour->getHue());
$this->assertEquals(.3, $colour->getSaturation());
$this->assertEquals(.6, $colour->getLightness());
$this->assertEquals(0x7A, $colour->getRed());
$this->assertEquals(0xB8, $colour->getGreen());
$this->assertEquals(0x99, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertInstanceOf(ColourHSL::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('hsl(150deg, 30%, 60%)', $colour);
$colour = new ColourHSL(108, .6, .45, .7);
$this->assertEquals(108, $colour->getHue());
$this->assertEquals(.6, $colour->getSaturation());
$this->assertEquals(.45, $colour->getLightness());
$this->assertEquals(0x49, $colour->getRed());
$this->assertEquals(0xB8, $colour->getGreen());
$this->assertEquals(0x2E, $colour->getBlue());
$this->assertEquals(.7, $colour->getAlpha());
$this->assertInstanceOf(ColourHSL::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('hsla(108deg, 60%, 45%, 0.7)', $colour);
$colour = new ColourHSL(0, .8, .5, .25);
$this->assertEquals(0, $colour->getHue());
$this->assertEquals(.8, $colour->getSaturation());
$this->assertEquals(.5, $colour->getLightness());
$this->assertEquals(0xE6, $colour->getRed());
$this->assertEquals(0x1A, $colour->getGreen());
$this->assertEquals(0x1A, $colour->getBlue());
$this->assertEquals(.25, $colour->getAlpha());
$this->assertInstanceOf(ColourHSL::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('hsla(0deg, 80%, 50%, 0.25)', $colour);
}
public function testConvertToHSL(): void {
$colourA = new ColourHSL(50, .8, .4, 1.0);
$colourB = ColourHSL::convert($colourA);
$this->assertSame($colourA, $colourB); // converting to the same type should passthru
$colourA = Colour::none();
$colourB = ColourHSL::convert($colourA); // converting none to HSL makes very little sense, but you can do it if you want...
$this->assertEquals(0, $colourB->getHue());
$this->assertEquals(0, $colourB->getSaturation());
$this->assertEquals(0, $colourB->getLightness());
$this->assertEquals(0, $colourB->getRed());
$this->assertEquals(0, $colourB->getGreen());
$this->assertEquals(0, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha());
$this->assertInstanceOf(ColourHSL::class, $colourB);
$this->assertFalse($colourB->shouldInherit()); // it does lose the inherit flag, which is intentional
$this->assertFalse($colourB->isLight());
$this->assertTrue($colourB->isDark());
$this->assertEquals('hsl(0deg, 0%, 0%)', $colourB);
$colourA = new ColourNamed('yellowgreen');
$colourB = ColourHSL::convert($colourA);
$this->assertEquals(79.742, $colourB->getHue());
$this->assertEquals(.608, $colourB->getSaturation());
$this->assertEquals(.5, $colourB->getLightness());
$this->assertEquals(0x9A, $colourB->getRed());
$this->assertEquals(0xCD, $colourB->getGreen());
$this->assertEquals(0x32, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha());
$this->assertInstanceOf(ColourHSL::class, $colourB);
$this->assertFalse($colourB->shouldInherit());
$this->assertFalse($colourB->isLight());
$this->assertTrue($colourB->isDark());
$this->assertEquals('hsl(79.74deg, 61%, 50%)', $colourB);
$colourA = new ColourRGB(31, 120, 50, 1.0);
$colourB = ColourHSL::convert($colourA);
$this->assertEquals(132.809, $colourB->getHue());
$this->assertEquals(.589, $colourB->getSaturation());
$this->assertEquals(.296, $colourB->getLightness());
$this->assertEquals(0x1F, $colourB->getRed());
$this->assertEquals(0x78, $colourB->getGreen());
$this->assertEquals(0x32, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha());
$this->assertInstanceOf(ColourHSL::class, $colourB);
$this->assertFalse($colourB->shouldInherit());
$this->assertFalse($colourB->isLight());
$this->assertTrue($colourB->isDark());
$this->assertEquals('hsl(132.81deg, 59%, 30%)', (string)$colourB);
}
public function testMisuzu(): void {
$msz1 = Colour::fromMisuzu(0x40AABBCC);
$this->assertInstanceOf(ColourNull::class, $msz1);
$this->assertTrue($msz1->shouldInherit());
$this->assertEquals('inherit', $msz1);
$msz2 = Colour::fromMisuzu(0xAABBCC);
$this->assertEquals(0xAA, $msz2->getRed());
$this->assertEquals(0xBB, $msz2->getGreen());
$this->assertEquals(0xCC, $msz2->getBlue());
$this->assertEquals(1.0, $msz2->getAlpha());
$this->assertInstanceOf(ColourRGB::class, $msz2);
$this->assertFalse($msz2->shouldInherit());
$this->assertFalse($msz2->isLight());
$this->assertTrue($msz2->isDark());
$this->assertEquals('#aabbcc', $msz2);
$mod = Colour::fromMisuzu(1693465);
$this->assertEquals(0x19, $mod->getRed());
$this->assertEquals(0xD7, $mod->getGreen());
$this->assertEquals(0x19, $mod->getBlue());
$this->assertEquals(1.0, $mod->getAlpha());
$this->assertInstanceOf(ColourRGB::class, $mod);
$this->assertFalse($mod->shouldInherit());
$this->assertFalse($mod->isLight());
$this->assertTrue($mod->isDark());
$this->assertEquals('#19d719', $mod);
$adm = Colour::fromMisuzu(16711680);
$this->assertEquals(0xFF, $adm->getRed());
$this->assertEquals(0, $adm->getGreen());
$this->assertEquals(0, $adm->getBlue());
$this->assertEquals(1.0, $adm->getAlpha());
$this->assertInstanceOf(ColourRGB::class, $adm);
$this->assertFalse($adm->shouldInherit());
$this->assertFalse($adm->isLight());
$this->assertTrue($adm->isDark());
$this->assertEquals('#ff0000', $adm);
$dev = Colour::fromMisuzu(7558084);
$this->assertEquals(0x73, $dev->getRed());
$this->assertEquals(0x53, $dev->getGreen());
$this->assertEquals(0xC4, $dev->getBlue());
$this->assertEquals(1.0, $dev->getAlpha());
$this->assertInstanceOf(ColourRGB::class, $dev);
$this->assertFalse($dev->shouldInherit());
$this->assertFalse($dev->isLight());
$this->assertTrue($dev->isDark());
$this->assertEquals('#7353c4', $dev);
}
public function testParse(): void {
$colour = Colour::parse(null);
$this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour);
$colour = Colour::parse(' ');
$this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour);
$colour = Colour::parse('inherit');
$this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour);
$colour = Colour::parse('xthiswillalsoneverexist');
$this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour);
$colour = Colour::parse('TRANSPARENT');
$this->assertInstanceOf(ColourNamed::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertTrue($colour->isTransparent());
$this->assertEquals('transparent', $colour->getName());
$this->assertEquals('transparent', $colour);
$this->assertEquals(0, $colour->getAlpha());
$colour = Colour::parse(' MediumOrchid ');
$this->assertInstanceOf(ColourNamed::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isTransparent());
$this->assertEquals('mediumorchid', $colour->getName());
$this->assertEquals('mediumorchid', $colour);
$this->assertEquals(1, $colour->getAlpha());
$this->assertEquals(0xBA, $colour->getRed());
$this->assertEquals(0x55, $colour->getGreen());
$this->assertEquals(0xD3, $colour->getBlue());
$colour = Colour::parse('#123');
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('#112233', $colour);
$this->assertEquals(1, $colour->getAlpha());
$this->assertEquals(0x11, $colour->getRed());
$this->assertEquals(0x22, $colour->getGreen());
$this->assertEquals(0x33, $colour->getBlue());
$colour = Colour::parse('#3456');
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('rgba(51, 68, 85, 0.4)', $colour);
$this->assertEquals(0.4, $colour->getAlpha());
$this->assertEquals(0x33, $colour->getRed());
$this->assertEquals(0x44, $colour->getGreen());
$this->assertEquals(0x55, $colour->getBlue());
$colour = Colour::parse('#9475b2');
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('#9475b2', $colour);
$this->assertEquals(1, $colour->getAlpha());
$this->assertEquals(0x94, $colour->getRed());
$this->assertEquals(0x75, $colour->getGreen());
$this->assertEquals(0xB2, $colour->getBlue());
$colour = Colour::parse('#8559A5b3');
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('rgba(133, 89, 165, 0.702)', $colour);
$this->assertEquals(0.702, round($colour->getAlpha(), 3));
$this->assertEquals(0x85, $colour->getRed());
$this->assertEquals(0x59, $colour->getGreen());
$this->assertEquals(0xA5, $colour->getBlue());
$colour = Colour::parse('rgb( 17 , 34 , 51)');
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('#112233', $colour);
$this->assertEquals(1, $colour->getAlpha());
$this->assertEquals(0x11, $colour->getRed());
$this->assertEquals(0x22, $colour->getGreen());
$this->assertEquals(0x33, $colour->getBlue());
$colour = Colour::parse('rgba(51 , 68, 85 ,0.4 )');
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('rgba(51, 68, 85, 0.4)', $colour);
$this->assertEquals(0.4, $colour->getAlpha());
$this->assertEquals(0x33, $colour->getRed());
$this->assertEquals(0x44, $colour->getGreen());
$this->assertEquals(0x55, $colour->getBlue());
$colour = Colour::parse('rgba( 148,117,178 )');
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('#9475b2', $colour);
$this->assertEquals(1, $colour->getAlpha());
$this->assertEquals(0x94, $colour->getRed());
$this->assertEquals(0x75, $colour->getGreen());
$this->assertEquals(0xB2, $colour->getBlue());
$colour = Colour::parse('rgb(133 ,89,165,.700)');
$this->assertInstanceOf(ColourRGB::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('rgba(133, 89, 165, 0.7)', $colour);
$this->assertEquals(0.7, $colour->getAlpha());
$this->assertEquals(0x85, $colour->getRed());
$this->assertEquals(0x59, $colour->getGreen());
$this->assertEquals(0xA5, $colour->getBlue());
// these results aren't really correct
// the difference in results is probably negligible for now
// but should definitely be reviewed in the future
$colour = Colour::parse('hsla( 210 , 50 , 13 )');
$this->assertInstanceOf(ColourHSL::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('hsl(210deg, 50%, 13%)', $colour);
$this->assertEquals(1, $colour->getAlpha());
$this->assertEquals(210, $colour->getHue());
$this->assertEquals(.5, $colour->getSaturation());
$this->assertEquals(.13, $colour->getLightness());
$this->assertEquals(17, $colour->getRed());
$this->assertEquals(33, $colour->getGreen());
$this->assertEquals(50, $colour->getBlue());
$colour = Colour::parse('hsl(210deg ,25%, 27% ,.6)');
$this->assertInstanceOf(ColourHSL::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('hsla(210deg, 25%, 27%, 0.6)', $colour);
$this->assertEquals(.6, $colour->getAlpha());
$this->assertEquals(210, $colour->getHue());
$this->assertEquals(.25, $colour->getSaturation());
$this->assertEquals(.27, $colour->getLightness());
$this->assertEquals(52, $colour->getRed());
$this->assertEquals(69, $colour->getGreen());
$this->assertEquals(86, $colour->getBlue());
$colour = Colour::parse('hsl(300grad,28%,58)');
$this->assertInstanceOf(ColourHSL::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('hsl(270deg, 28%, 58%)', $colour);
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(270, $colour->getHue());
$this->assertEquals(.28, $colour->getSaturation());
$this->assertEquals(.58, $colour->getLightness());
$this->assertEquals(148, $colour->getRed());
$this->assertEquals(118, $colour->getGreen());
$this->assertEquals(178, $colour->getBlue());
$colour = Colour::parse('hsl( 4.799rad ,30 , 50%)');
$this->assertInstanceOf(ColourHSL::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('hsl(275deg, 30%, 50%)', $colour);
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(275, $colour->getHue());
$this->assertEquals(.3, $colour->getSaturation());
$this->assertEquals(.5, $colour->getLightness());
$this->assertEquals(134, $colour->getRed());
$this->assertEquals(89, $colour->getGreen());
$this->assertEquals(166, $colour->getBlue());
$colour = Colour::parse('hsl( .775turn , 13,60, .54)');
$this->assertInstanceOf(ColourHSL::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertEquals('hsla(279deg, 13%, 60%, 0.54)', $colour);
$this->assertEquals(0.54, $colour->getAlpha());
$this->assertEquals(279, $colour->getHue());
$this->assertEquals(.13, $colour->getSaturation());
$this->assertEquals(.6, $colour->getLightness());
$this->assertEquals(157, $colour->getRed());
$this->assertEquals(140, $colour->getGreen());
$this->assertEquals(166, $colour->getBlue());
}
}