index/tests/ColourTest.php

662 lines
32 KiB
PHP
Raw Normal View History

2023-01-02 22:22:37 +00:00
<?php
// ColourTest.php
// Created: 2023-01-02
// Updated: 2024-10-19
2023-01-02 22:22:37 +00:00
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
2024-10-02 01:35:05 +00:00
use Index\Colour\{Colour,ColourHsl,ColourNamed,ColourNull,ColourRgb};
2023-01-02 22:22:37 +00:00
#[CoversClass(Colour::class)]
2024-10-02 01:35:05 +00:00
#[CoversClass(ColourHsl::class)]
#[CoversClass(ColourNamed::class)]
#[CoversClass(ColourNull::class)]
2024-10-02 01:35:05 +00:00
#[CoversClass(ColourRgb::class)]
2023-01-02 22:22:37 +00:00
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;
2024-10-02 01:35:05 +00:00
$colour = new ColourRgb($num, $num, $num, 1);
2023-01-02 22:22:37 +00:00
$this->assertTrue($colour->isDark());
$this->assertFalse($colour->isLight());
}
for($i = 0xB; $i < 0x10; ++$i) {
$num = ($i << 4) | $i;
2024-10-02 01:35:05 +00:00
$colour = new ColourRgb($num, $num, $num, 1);
2023-01-02 22:22:37 +00:00
$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 {
2024-10-02 01:35:05 +00:00
$colour = new ColourRgb(31, 120, 50, 1.0);
2023-01-02 22:22:37 +00:00
$this->assertEquals(31, $colour->getRed());
$this->assertEquals(120, $colour->getGreen());
$this->assertEquals(50, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0x1F7832, Colour::toRawRgb($colour));
$this->assertEquals(0xFF1F7832, Colour::toRawArgb($colour));
$this->assertEquals(0x1F7832FF, Colour::toRawRgba($colour));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('#1f7832', $colour);
2024-10-02 01:35:05 +00:00
$colour = ColourRgb::fromRawArgb(0xCCFF7A7F);
2023-01-02 22:22:37 +00:00
$this->assertEquals(255, $colour->getRed());
$this->assertEquals(122, $colour->getGreen());
$this->assertEquals(127, $colour->getBlue());
$this->assertEquals(.8, $colour->getAlpha());
$this->assertEquals(0xFF7A7F, Colour::toRawRgb($colour));
$this->assertEquals(0xCCFF7A7F, Colour::toRawArgb($colour));
$this->assertEquals(0xFF7A7FCC, Colour::toRawRgba($colour));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('rgba(255, 122, 127, 0.8)', $colour);
2024-10-02 01:35:05 +00:00
$colour = ColourRgb::fromRawRgb(0x4D3380);
2023-01-02 22:22:37 +00:00
$this->assertEquals(77, $colour->getRed());
$this->assertEquals(51, $colour->getGreen());
$this->assertEquals(128, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0x4D3380, Colour::toRawRgb($colour));
$this->assertEquals(0xFF4D3380, Colour::toRawArgb($colour));
$this->assertEquals(0x4D3380FF, Colour::toRawRgba($colour));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('#4d3380', $colour);
2024-10-02 01:35:05 +00:00
$colour = ColourRgb::fromRawRgba(0xFF7A7F33);
2023-01-02 22:22:37 +00:00
$this->assertEquals(255, $colour->getRed());
$this->assertEquals(122, $colour->getGreen());
$this->assertEquals(127, $colour->getBlue());
$this->assertEquals(.2, $colour->getAlpha());
$this->assertEquals(0xFF7A7F, Colour::toRawRgb($colour));
$this->assertEquals(0x33FF7A7F, Colour::toRawArgb($colour));
$this->assertEquals(0xFF7A7F33, Colour::toRawRgba($colour));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$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 {
2024-10-02 01:35:05 +00:00
$colourA = new ColourRgb(31, 120, 50, 1.0);
$colourB = ColourRgb::convert($colourA);
2023-01-02 22:22:37 +00:00
$this->assertSame($colourA, $colourB); // converting to the same type should passthru
$colourA = Colour::none();
2024-10-02 01:35:05 +00:00
$colourB = ColourRgb::convert($colourA); // converting none to RGB makes very little sense, but you can do it if you want...
2023-01-02 22:22:37 +00:00
$this->assertEquals(0, $colourB->getRed());
$this->assertEquals(0, $colourB->getGreen());
$this->assertEquals(0, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha());
$this->assertEquals(0, Colour::toRawRgb($colourB));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colourB));
$this->assertEquals(0xFF, Colour::toRawRgba($colourB));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colourB);
2023-01-02 22:22:37 +00:00
$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');
2024-10-02 01:35:05 +00:00
$colourB = ColourRgb::convert($colourA);
2023-01-02 22:22:37 +00:00
$this->assertEquals(0x46, $colourB->getRed());
$this->assertEquals(0x82, $colourB->getGreen());
$this->assertEquals(0xB4, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha());
$this->assertEquals(0x4682B4, Colour::toRawRgb($colourB));
$this->assertEquals(0xFF4682B4, Colour::toRawArgb($colourB));
$this->assertEquals(0x4682B4FF, Colour::toRawRgba($colourB));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colourB);
2023-01-02 22:22:37 +00:00
$this->assertFalse($colourB->shouldInherit());
$this->assertFalse($colourB->isLight());
$this->assertTrue($colourB->isDark());
$this->assertEquals('#4682b4', $colourB);
2024-10-02 01:35:05 +00:00
$colourA = new ColourHsl(50, .8, .4, 1.0);
$colourB = ColourRgb::convert($colourA);
2023-01-02 22:22:37 +00:00
$this->assertEquals(0xB8, $colourB->getRed());
$this->assertEquals(0x9C, $colourB->getGreen());
$this->assertEquals(0x14, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha());
$this->assertEquals(0xB89C14, Colour::toRawRgb($colourB));
$this->assertEquals(0xFFB89C14, Colour::toRawArgb($colourB));
$this->assertEquals(0xB89C14FF, Colour::toRawRgba($colourB));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colourB);
2023-01-02 22:22:37 +00:00
$this->assertFalse($colourB->shouldInherit());
$this->assertFalse($colourB->isLight());
$this->assertTrue($colourB->isDark());
$this->assertEquals('#b89c14', $colourB);
}
public function testHSL(): void {
2024-10-02 01:35:05 +00:00
$colour = new ColourHsl(50, .8, .4, 1.0);
2023-01-02 22:22:37 +00:00
$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->assertEquals(0xB89C14, Colour::toRawRgb($colour));
$this->assertEquals(0xFFB89C14, Colour::toRawArgb($colour));
$this->assertEquals(0xB89C14FF, Colour::toRawRgba($colour));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colour);
2023-01-02 22:22:37 +00:00
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('hsl(50deg, 80%, 40%)', $colour);
2024-10-02 01:35:05 +00:00
$colour = new ColourHsl(150, .3, .6, 1.0);
2023-01-02 22:22:37 +00:00
$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->assertEquals(0x7AB899, Colour::toRawRgb($colour));
$this->assertEquals(0xFF7AB899, Colour::toRawArgb($colour));
$this->assertEquals(0x7AB899FF, Colour::toRawRgba($colour));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colour);
2023-01-02 22:22:37 +00:00
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('hsl(150deg, 30%, 60%)', $colour);
2024-10-02 01:35:05 +00:00
$colour = new ColourHsl(108, .6, .45, .7);
2023-01-02 22:22:37 +00:00
$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(0x49B82E, Colour::toRawRgb($colour));
$this->assertEquals(0xB349B82E, Colour::toRawArgb($colour));
$this->assertEquals(0x49B82EB3, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$this->assertEquals(.7, $colour->getAlpha());
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colour);
2023-01-02 22:22:37 +00:00
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('hsla(108deg, 60%, 45%, 0.7)', $colour);
2024-10-02 01:35:05 +00:00
$colour = new ColourHsl(0, .8, .5, .25);
2023-01-02 22:22:37 +00:00
$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->assertEquals(0xE61A1A, Colour::toRawRgb($colour));
$this->assertEquals(0x40E61A1A, Colour::toRawArgb($colour));
$this->assertEquals(0xE61A1A40, Colour::toRawRgba($colour));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colour);
2023-01-02 22:22:37 +00:00
$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 {
2024-10-02 01:35:05 +00:00
$colourA = new ColourHsl(50, .8, .4, 1.0);
$colourB = ColourHsl::convert($colourA);
2023-01-02 22:22:37 +00:00
$this->assertSame($colourA, $colourB); // converting to the same type should passthru
$colourA = Colour::none();
2024-10-02 01:35:05 +00:00
$colourB = ColourHsl::convert($colourA); // converting none to HSL makes very little sense, but you can do it if you want...
2023-01-02 22:22:37 +00:00
$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->assertEquals(0, Colour::toRawRgb($colourB));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colourB));
$this->assertEquals(0xFF, Colour::toRawRgba($colourB));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colourB);
2023-01-02 22:22:37 +00:00
$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');
2024-10-02 01:35:05 +00:00
$colourB = ColourHsl::convert($colourA);
2023-01-02 22:22:37 +00:00
$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->assertEquals(0x9ACD32, Colour::toRawRgb($colourB));
$this->assertEquals(0xFF9ACD32, Colour::toRawArgb($colourB));
$this->assertEquals(0x9ACD32FF, Colour::toRawRgba($colourB));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colourB);
2023-01-02 22:22:37 +00:00
$this->assertFalse($colourB->shouldInherit());
$this->assertFalse($colourB->isLight());
$this->assertTrue($colourB->isDark());
$this->assertEquals('hsl(79.74deg, 61%, 50%)', $colourB);
2024-10-02 01:35:05 +00:00
$colourA = new ColourRgb(31, 120, 50, 1.0);
$colourB = ColourHsl::convert($colourA);
2023-01-02 22:22:37 +00:00
$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->assertEquals(0x1F7832, Colour::toRawRgb($colourB));
$this->assertEquals(0xFF1F7832, Colour::toRawArgb($colourB));
$this->assertEquals(0x1F7832FF, Colour::toRawRgba($colourB));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colourB);
2023-01-02 22:22:37 +00:00
$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 {
$colour = Colour::fromMisuzu(0x40AABBCC);
$this->assertInstanceOf(ColourNull::class, $colour);
$this->assertTrue($colour->shouldInherit());
$this->assertEquals('inherit', $colour);
$this->assertEquals(0x40000000, Colour::toMisuzu($colour));
$this->assertEquals(0, Colour::toRawRgb($colour));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colour));
$this->assertEquals(0xFF, Colour::toRawRgba($colour));
$colour = Colour::fromMisuzu(0xAABBCC);
$this->assertEquals(0xAA, $colour->getRed());
$this->assertEquals(0xBB, $colour->getGreen());
$this->assertEquals(0xCC, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0xAABBCC, Colour::toRawRgb($colour));
$this->assertEquals(0xFFAABBCC, Colour::toRawArgb($colour));
$this->assertEquals(0xAABBCCFF, Colour::toRawRgba($colour));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('#aabbcc', $colour);
$this->assertEquals(0xAABBCC, Colour::toMisuzu($colour));
$colour = Colour::fromMisuzu(1693465);
$this->assertEquals(0x19, $colour->getRed());
$this->assertEquals(0xD7, $colour->getGreen());
$this->assertEquals(0x19, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0x19D719, Colour::toRawRgb($colour));
$this->assertEquals(0xFF19D719, Colour::toRawArgb($colour));
$this->assertEquals(0x19D719FF, Colour::toRawRgba($colour));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('#19d719', $colour);
$this->assertEquals(1693465, Colour::toMisuzu($colour));
$colour = Colour::fromMisuzu(16711680);
$this->assertEquals(0xFF, $colour->getRed());
$this->assertEquals(0, $colour->getGreen());
$this->assertEquals(0, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0xFF0000, Colour::toRawRgb($colour));
$this->assertEquals(0xFFFF0000, Colour::toRawArgb($colour));
$this->assertEquals(0xFF0000FF, Colour::toRawRgba($colour));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('#ff0000', $colour);
$this->assertEquals(16711680, Colour::toMisuzu($colour));
$colour = Colour::fromMisuzu(7558084);
$this->assertEquals(0x73, $colour->getRed());
$this->assertEquals(0x53, $colour->getGreen());
$this->assertEquals(0xC4, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0x7353C4, Colour::toRawRgb($colour));
$this->assertEquals(0xFF7353C4, Colour::toRawArgb($colour));
$this->assertEquals(0x7353C4FF, Colour::toRawRgba($colour));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight());
$this->assertTrue($colour->isDark());
$this->assertEquals('#7353c4', $colour);
$this->assertEquals(7558084, Colour::toMisuzu($colour));
2023-01-02 22:22:37 +00:00
}
public function testParse(): void {
$colour = Colour::parse(null);
$this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour);
$this->assertEquals(0, Colour::toRawRgb($colour));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colour));
$this->assertEquals(0xFF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse(' ');
$this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour);
$this->assertEquals(0, Colour::toRawRgb($colour));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colour));
$this->assertEquals(0xFF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('inherit');
$this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour);
$this->assertEquals(0, Colour::toRawRgb($colour));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colour));
$this->assertEquals(0xFF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('xthiswillalsoneverexist');
$this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour);
$this->assertEquals(0, Colour::toRawRgb($colour));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colour));
$this->assertEquals(0xFF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0, Colour::toRawRgb($colour));
$this->assertEquals(0, Colour::toRawArgb($colour));
$this->assertEquals(0, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0xBA55D3, Colour::toRawRgb($colour));
$this->assertEquals(0xFFBA55D3, Colour::toRawArgb($colour));
$this->assertEquals(0xBA55D3FF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('#123');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x112233, Colour::toRawRgb($colour));
$this->assertEquals(0xFF112233, Colour::toRawArgb($colour));
$this->assertEquals(0x112233FF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('#3456');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x334455, Colour::toRawRgb($colour));
$this->assertEquals(0x66334455, Colour::toRawArgb($colour));
$this->assertEquals(0x33445566, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('#9475b2');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x9475B2, Colour::toRawRgb($colour));
$this->assertEquals(0xFF9475B2, Colour::toRawArgb($colour));
$this->assertEquals(0x9475B2FF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('#8559A5b3');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x8559A5, Colour::toRawRgb($colour));
$this->assertEquals(0xB38559A5, Colour::toRawArgb($colour));
$this->assertEquals(0x8559A5B3, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('rgb( 17 , 34 , 51)');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x112233, Colour::toRawRgb($colour));
$this->assertEquals(0xFF112233, Colour::toRawArgb($colour));
$this->assertEquals(0x112233FF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('rgba(51 , 68, 85 ,0.4 )');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x334455, Colour::toRawRgb($colour));
$this->assertEquals(0x66334455, Colour::toRawArgb($colour));
$this->assertEquals(0x33445566, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('rgba( 148,117,178 )');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x9475B2, Colour::toRawRgb($colour));
$this->assertEquals(0xFF9475B2, Colour::toRawArgb($colour));
$this->assertEquals(0x9475B2FF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('rgb(133 ,89,165,.700)');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourRgb::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x8559A5, Colour::toRawRgb($colour));
$this->assertEquals(0xB38559A5, Colour::toRawArgb($colour));
$this->assertEquals(0x8559A5B3, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
// 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 )');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x112132, Colour::toRawRgb($colour));
$this->assertEquals(0xFF112132, Colour::toRawArgb($colour));
$this->assertEquals(0x112132FF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('hsl(210deg ,25%, 27% ,.6)');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x344556, Colour::toRawRgb($colour));
$this->assertEquals(0x99344556, Colour::toRawArgb($colour));
$this->assertEquals(0x34455699, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('hsl(300grad,28%,58)');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x9476B2, Colour::toRawRgb($colour));
$this->assertEquals(0xFF9476B2, Colour::toRawArgb($colour));
$this->assertEquals(0x9476B2FF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('hsl( 4.799rad ,30 , 50%)');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x8659A6, Colour::toRawRgb($colour));
$this->assertEquals(0xFF8659A6, Colour::toRawArgb($colour));
$this->assertEquals(0x8659A6FF, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
$colour = Colour::parse('hsl( .775turn , 13,60, .54)');
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(ColourHsl::class, $colour);
2023-01-02 22:22:37 +00:00
$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());
$this->assertEquals(0x9D8CA6, Colour::toRawRgb($colour));
$this->assertEquals(0x8A9D8CA6, Colour::toRawArgb($colour));
$this->assertEquals(0x9D8CA68A, Colour::toRawRgba($colour));
2023-01-02 22:22:37 +00:00
}
public function testMix(): void {
// none should always return none if involved...
$mixed = Colour::mix(Colour::none(), Colour::parse('lemonchiffon'), 0.243);
$this->assertTrue($mixed->shouldInherit());
$mixed = Colour::mix(Colour::parse('powderblue'), Colour::none(), 0.76);
$this->assertTrue($mixed->shouldInherit());
// ...unless absolute 0 or 1
$mixed = Colour::mix(Colour::none(), Colour::parse('lemonchiffon'), 1.0);
$this->assertFalse($mixed->shouldInherit());
$mixed = Colour::mix(Colour::parse('powderblue'), Colour::none(), 0.0);
$this->assertFalse($mixed->shouldInherit());
// probably a pretty naive test but its better than nothing
2024-10-02 01:35:05 +00:00
$mixed = Colour::mix(new ColourRgb(0, 100, 50), new ColourRgb(100, 0, 50), 0.5);
$this->assertEquals(50, $mixed->getRed());
$this->assertEquals(50, $mixed->getGreen());
$this->assertEquals(50, $mixed->getBlue());
// absolutes should return one of the args
$colour1 = Colour::parse('royalblue');
$colour2 = Colour::parse('tomato');
$this->assertSame($colour1, Colour::mix($colour1, $colour2, 0.0));
$this->assertSame($colour2, Colour::mix($colour1, $colour2, 1.0));
$mixed = Colour::mix($colour1, $colour2, 0.5);
$this->assertEquals(160, $mixed->getRed());
$this->assertEquals(102, $mixed->getGreen());
$this->assertEquals(148, $mixed->getBlue());
$mixed = Colour::mix($colour1, $colour2, 0.2);
$this->assertEquals(103, $mixed->getRed());
$this->assertEquals(104, $mixed->getGreen());
$this->assertEquals(194, $mixed->getBlue());
$mixed = Colour::mix($colour1, $colour2, 0.8);
$this->assertEquals(217, $mixed->getRed());
$this->assertEquals(100, $mixed->getGreen());
$this->assertEquals(102, $mixed->getBlue());
}
2023-01-02 22:22:37 +00:00
}