index/tests/ColourTest.php

505 lines
23 KiB
PHP

<?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());
}
}