Added non-Misuzu specific raw RGB conversion methods.

This commit is contained in:
flash 2024-10-19 16:04:17 +00:00
parent f137dfaab5
commit 17cdb4d1c2
3 changed files with 149 additions and 3 deletions

View file

@ -1 +1 @@
0.2410.191515 0.2410.191603

View file

@ -1,7 +1,7 @@
<?php <?php
// Colour.php // Colour.php
// Created: 2023-01-02 // Created: 2023-01-02
// Updated: 2024-10-02 // Updated: 2024-10-19
namespace Index\Colour; namespace Index\Colour;
@ -162,6 +162,38 @@ abstract class Colour implements Stringable {
return ($colour->getRed() << 16) | ($colour->getGreen() << 8) | $colour->getBlue(); return ($colour->getRed() << 16) | ($colour->getGreen() << 8) | $colour->getBlue();
} }
/**
* Packs a colour into an RGB integer.
*
* @param Colour $colour Colour to pack.
* @return int Packed RGB colour.
*/
public static function toRawRgb(Colour $colour): int {
return ($colour->getRed() << 16) | ($colour->getGreen() << 8) | $colour->getBlue();
}
/**
* Packs a colour into an ARGB integer.
*
* @param Colour $colour Colour to pack.
* @return int Packed ARGB colour.
*/
public static function toRawArgb(Colour $colour): int {
$alpha = max(0, min(255, round($colour->getAlpha() * 255)));
return ($alpha << 24) | ($colour->getRed() << 16) | ($colour->getGreen() << 8) | $colour->getBlue();
}
/**
* Packs a colour into an RGBA integer.
*
* @param Colour $colour Colour to pack.
* @return int Packed RGBA colour.
*/
public static function toRawRgba(Colour $colour): int {
$alpha = max(0, min(255, round($colour->getAlpha() * 255)));
return ($colour->getRed() << 24) | ($colour->getGreen() << 16) | ($colour->getBlue() << 8) | $alpha;
}
/** /**
* Attempts to parse a CSS format colour. * Attempts to parse a CSS format colour.
* *

View file

@ -1,7 +1,7 @@
<?php <?php
// ColourTest.php // ColourTest.php
// Created: 2023-01-02 // Created: 2023-01-02
// Updated: 2024-10-02 // Updated: 2024-10-19
declare(strict_types=1); declare(strict_types=1);
@ -75,6 +75,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(120, $colour->getGreen()); $this->assertEquals(120, $colour->getGreen());
$this->assertEquals(50, $colour->getBlue()); $this->assertEquals(50, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha()); $this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0x1F7832, Colour::toRawRgb($colour));
$this->assertEquals(0xFF1F7832, Colour::toRawArgb($colour));
$this->assertEquals(0x1F7832FF, Colour::toRawRgba($colour));
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight()); $this->assertFalse($colour->isLight());
@ -86,6 +89,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(122, $colour->getGreen()); $this->assertEquals(122, $colour->getGreen());
$this->assertEquals(127, $colour->getBlue()); $this->assertEquals(127, $colour->getBlue());
$this->assertEquals(.8, $colour->getAlpha()); $this->assertEquals(.8, $colour->getAlpha());
$this->assertEquals(0xFF7A7F, Colour::toRawRgb($colour));
$this->assertEquals(0xCCFF7A7F, Colour::toRawArgb($colour));
$this->assertEquals(0xFF7A7FCC, Colour::toRawRgba($colour));
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight()); $this->assertFalse($colour->isLight());
@ -97,6 +103,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(51, $colour->getGreen()); $this->assertEquals(51, $colour->getGreen());
$this->assertEquals(128, $colour->getBlue()); $this->assertEquals(128, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha()); $this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0x4D3380, Colour::toRawRgb($colour));
$this->assertEquals(0xFF4D3380, Colour::toRawArgb($colour));
$this->assertEquals(0x4D3380FF, Colour::toRawRgba($colour));
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight()); $this->assertFalse($colour->isLight());
@ -108,6 +117,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(122, $colour->getGreen()); $this->assertEquals(122, $colour->getGreen());
$this->assertEquals(127, $colour->getBlue()); $this->assertEquals(127, $colour->getBlue());
$this->assertEquals(.2, $colour->getAlpha()); $this->assertEquals(.2, $colour->getAlpha());
$this->assertEquals(0xFF7A7F, Colour::toRawRgb($colour));
$this->assertEquals(0x33FF7A7F, Colour::toRawArgb($colour));
$this->assertEquals(0xFF7A7F33, Colour::toRawRgba($colour));
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight()); $this->assertFalse($colour->isLight());
@ -126,6 +138,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0, $colourB->getGreen()); $this->assertEquals(0, $colourB->getGreen());
$this->assertEquals(0, $colourB->getBlue()); $this->assertEquals(0, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha()); $this->assertEquals(1.0, $colourB->getAlpha());
$this->assertEquals(0, Colour::toRawRgb($colourB));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colourB));
$this->assertEquals(0xFF, Colour::toRawRgba($colourB));
$this->assertInstanceOf(ColourRgb::class, $colourB); $this->assertInstanceOf(ColourRgb::class, $colourB);
$this->assertFalse($colourB->shouldInherit()); // it does lose the inherit flag, which is intentional $this->assertFalse($colourB->shouldInherit()); // it does lose the inherit flag, which is intentional
$this->assertFalse($colourB->isLight()); $this->assertFalse($colourB->isLight());
@ -138,6 +153,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x82, $colourB->getGreen()); $this->assertEquals(0x82, $colourB->getGreen());
$this->assertEquals(0xB4, $colourB->getBlue()); $this->assertEquals(0xB4, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha()); $this->assertEquals(1.0, $colourB->getAlpha());
$this->assertEquals(0x4682B4, Colour::toRawRgb($colourB));
$this->assertEquals(0xFF4682B4, Colour::toRawArgb($colourB));
$this->assertEquals(0x4682B4FF, Colour::toRawRgba($colourB));
$this->assertInstanceOf(ColourRgb::class, $colourB); $this->assertInstanceOf(ColourRgb::class, $colourB);
$this->assertFalse($colourB->shouldInherit()); $this->assertFalse($colourB->shouldInherit());
$this->assertFalse($colourB->isLight()); $this->assertFalse($colourB->isLight());
@ -150,6 +168,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x9C, $colourB->getGreen()); $this->assertEquals(0x9C, $colourB->getGreen());
$this->assertEquals(0x14, $colourB->getBlue()); $this->assertEquals(0x14, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha()); $this->assertEquals(1.0, $colourB->getAlpha());
$this->assertEquals(0xB89C14, Colour::toRawRgb($colourB));
$this->assertEquals(0xFFB89C14, Colour::toRawArgb($colourB));
$this->assertEquals(0xB89C14FF, Colour::toRawRgba($colourB));
$this->assertInstanceOf(ColourRgb::class, $colourB); $this->assertInstanceOf(ColourRgb::class, $colourB);
$this->assertFalse($colourB->shouldInherit()); $this->assertFalse($colourB->shouldInherit());
$this->assertFalse($colourB->isLight()); $this->assertFalse($colourB->isLight());
@ -166,6 +187,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x9C, $colour->getGreen()); $this->assertEquals(0x9C, $colour->getGreen());
$this->assertEquals(0x14, $colour->getBlue()); $this->assertEquals(0x14, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha()); $this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0xB89C14, Colour::toRawRgb($colour));
$this->assertEquals(0xFFB89C14, Colour::toRawArgb($colour));
$this->assertEquals(0xB89C14FF, Colour::toRawRgba($colour));
$this->assertInstanceOf(ColourHsl::class, $colour); $this->assertInstanceOf(ColourHsl::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight()); $this->assertFalse($colour->isLight());
@ -180,6 +204,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0xB8, $colour->getGreen()); $this->assertEquals(0xB8, $colour->getGreen());
$this->assertEquals(0x99, $colour->getBlue()); $this->assertEquals(0x99, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha()); $this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0x7AB899, Colour::toRawRgb($colour));
$this->assertEquals(0xFF7AB899, Colour::toRawArgb($colour));
$this->assertEquals(0x7AB899FF, Colour::toRawRgba($colour));
$this->assertInstanceOf(ColourHsl::class, $colour); $this->assertInstanceOf(ColourHsl::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight()); $this->assertFalse($colour->isLight());
@ -193,6 +220,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x49, $colour->getRed()); $this->assertEquals(0x49, $colour->getRed());
$this->assertEquals(0xB8, $colour->getGreen()); $this->assertEquals(0xB8, $colour->getGreen());
$this->assertEquals(0x2E, $colour->getBlue()); $this->assertEquals(0x2E, $colour->getBlue());
$this->assertEquals(0x49B82E, Colour::toRawRgb($colour));
$this->assertEquals(0xB349B82E, Colour::toRawArgb($colour));
$this->assertEquals(0x49B82EB3, Colour::toRawRgba($colour));
$this->assertEquals(.7, $colour->getAlpha()); $this->assertEquals(.7, $colour->getAlpha());
$this->assertInstanceOf(ColourHsl::class, $colour); $this->assertInstanceOf(ColourHsl::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
@ -208,6 +238,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x1A, $colour->getGreen()); $this->assertEquals(0x1A, $colour->getGreen());
$this->assertEquals(0x1A, $colour->getBlue()); $this->assertEquals(0x1A, $colour->getBlue());
$this->assertEquals(.25, $colour->getAlpha()); $this->assertEquals(.25, $colour->getAlpha());
$this->assertEquals(0xE61A1A, Colour::toRawRgb($colour));
$this->assertEquals(0x40E61A1A, Colour::toRawArgb($colour));
$this->assertEquals(0xE61A1A40, Colour::toRawRgba($colour));
$this->assertInstanceOf(ColourHsl::class, $colour); $this->assertInstanceOf(ColourHsl::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight()); $this->assertFalse($colour->isLight());
@ -229,6 +262,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0, $colourB->getGreen()); $this->assertEquals(0, $colourB->getGreen());
$this->assertEquals(0, $colourB->getBlue()); $this->assertEquals(0, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha()); $this->assertEquals(1.0, $colourB->getAlpha());
$this->assertEquals(0, Colour::toRawRgb($colourB));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colourB));
$this->assertEquals(0xFF, Colour::toRawRgba($colourB));
$this->assertInstanceOf(ColourHsl::class, $colourB); $this->assertInstanceOf(ColourHsl::class, $colourB);
$this->assertFalse($colourB->shouldInherit()); // it does lose the inherit flag, which is intentional $this->assertFalse($colourB->shouldInherit()); // it does lose the inherit flag, which is intentional
$this->assertFalse($colourB->isLight()); $this->assertFalse($colourB->isLight());
@ -244,6 +280,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0xCD, $colourB->getGreen()); $this->assertEquals(0xCD, $colourB->getGreen());
$this->assertEquals(0x32, $colourB->getBlue()); $this->assertEquals(0x32, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha()); $this->assertEquals(1.0, $colourB->getAlpha());
$this->assertEquals(0x9ACD32, Colour::toRawRgb($colourB));
$this->assertEquals(0xFF9ACD32, Colour::toRawArgb($colourB));
$this->assertEquals(0x9ACD32FF, Colour::toRawRgba($colourB));
$this->assertInstanceOf(ColourHsl::class, $colourB); $this->assertInstanceOf(ColourHsl::class, $colourB);
$this->assertFalse($colourB->shouldInherit()); $this->assertFalse($colourB->shouldInherit());
$this->assertFalse($colourB->isLight()); $this->assertFalse($colourB->isLight());
@ -259,6 +298,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x78, $colourB->getGreen()); $this->assertEquals(0x78, $colourB->getGreen());
$this->assertEquals(0x32, $colourB->getBlue()); $this->assertEquals(0x32, $colourB->getBlue());
$this->assertEquals(1.0, $colourB->getAlpha()); $this->assertEquals(1.0, $colourB->getAlpha());
$this->assertEquals(0x1F7832, Colour::toRawRgb($colourB));
$this->assertEquals(0xFF1F7832, Colour::toRawArgb($colourB));
$this->assertEquals(0x1F7832FF, Colour::toRawRgba($colourB));
$this->assertInstanceOf(ColourHsl::class, $colourB); $this->assertInstanceOf(ColourHsl::class, $colourB);
$this->assertFalse($colourB->shouldInherit()); $this->assertFalse($colourB->shouldInherit());
$this->assertFalse($colourB->isLight()); $this->assertFalse($colourB->isLight());
@ -272,12 +314,18 @@ final class ColourTest extends TestCase {
$this->assertTrue($colour->shouldInherit()); $this->assertTrue($colour->shouldInherit());
$this->assertEquals('inherit', $colour); $this->assertEquals('inherit', $colour);
$this->assertEquals(0x40000000, Colour::toMisuzu($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); $colour = Colour::fromMisuzu(0xAABBCC);
$this->assertEquals(0xAA, $colour->getRed()); $this->assertEquals(0xAA, $colour->getRed());
$this->assertEquals(0xBB, $colour->getGreen()); $this->assertEquals(0xBB, $colour->getGreen());
$this->assertEquals(0xCC, $colour->getBlue()); $this->assertEquals(0xCC, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha()); $this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0xAABBCC, Colour::toRawRgb($colour));
$this->assertEquals(0xFFAABBCC, Colour::toRawArgb($colour));
$this->assertEquals(0xAABBCCFF, Colour::toRawRgba($colour));
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight()); $this->assertFalse($colour->isLight());
@ -290,6 +338,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0xD7, $colour->getGreen()); $this->assertEquals(0xD7, $colour->getGreen());
$this->assertEquals(0x19, $colour->getBlue()); $this->assertEquals(0x19, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha()); $this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0x19D719, Colour::toRawRgb($colour));
$this->assertEquals(0xFF19D719, Colour::toRawArgb($colour));
$this->assertEquals(0x19D719FF, Colour::toRawRgba($colour));
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight()); $this->assertFalse($colour->isLight());
@ -302,6 +353,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0, $colour->getGreen()); $this->assertEquals(0, $colour->getGreen());
$this->assertEquals(0, $colour->getBlue()); $this->assertEquals(0, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha()); $this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0xFF0000, Colour::toRawRgb($colour));
$this->assertEquals(0xFFFF0000, Colour::toRawArgb($colour));
$this->assertEquals(0xFF0000FF, Colour::toRawRgba($colour));
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight()); $this->assertFalse($colour->isLight());
@ -314,6 +368,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x53, $colour->getGreen()); $this->assertEquals(0x53, $colour->getGreen());
$this->assertEquals(0xC4, $colour->getBlue()); $this->assertEquals(0xC4, $colour->getBlue());
$this->assertEquals(1.0, $colour->getAlpha()); $this->assertEquals(1.0, $colour->getAlpha());
$this->assertEquals(0x7353C4, Colour::toRawRgb($colour));
$this->assertEquals(0xFF7353C4, Colour::toRawArgb($colour));
$this->assertEquals(0x7353C4FF, Colour::toRawRgba($colour));
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
$this->assertFalse($colour->shouldInherit()); $this->assertFalse($colour->shouldInherit());
$this->assertFalse($colour->isLight()); $this->assertFalse($colour->isLight());
@ -327,21 +384,33 @@ final class ColourTest extends TestCase {
$this->assertTrue($colour->shouldInherit()); $this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour); $this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour); $this->assertEquals('inherit', $colour);
$this->assertEquals(0, Colour::toRawRgb($colour));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colour));
$this->assertEquals(0xFF, Colour::toRawRgba($colour));
$colour = Colour::parse(' '); $colour = Colour::parse(' ');
$this->assertTrue($colour->shouldInherit()); $this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour); $this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour); $this->assertEquals('inherit', $colour);
$this->assertEquals(0, Colour::toRawRgb($colour));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colour));
$this->assertEquals(0xFF, Colour::toRawRgba($colour));
$colour = Colour::parse('inherit'); $colour = Colour::parse('inherit');
$this->assertTrue($colour->shouldInherit()); $this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour); $this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour); $this->assertEquals('inherit', $colour);
$this->assertEquals(0, Colour::toRawRgb($colour));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colour));
$this->assertEquals(0xFF, Colour::toRawRgba($colour));
$colour = Colour::parse('xthiswillalsoneverexist'); $colour = Colour::parse('xthiswillalsoneverexist');
$this->assertTrue($colour->shouldInherit()); $this->assertTrue($colour->shouldInherit());
$this->assertSame(Colour::none(), $colour); $this->assertSame(Colour::none(), $colour);
$this->assertEquals('inherit', $colour); $this->assertEquals('inherit', $colour);
$this->assertEquals(0, Colour::toRawRgb($colour));
$this->assertEquals(0xFF000000, Colour::toRawArgb($colour));
$this->assertEquals(0xFF, Colour::toRawRgba($colour));
$colour = Colour::parse('TRANSPARENT'); $colour = Colour::parse('TRANSPARENT');
@ -351,6 +420,9 @@ final class ColourTest extends TestCase {
$this->assertEquals('transparent', $colour->getName()); $this->assertEquals('transparent', $colour->getName());
$this->assertEquals('transparent', $colour); $this->assertEquals('transparent', $colour);
$this->assertEquals(0, $colour->getAlpha()); $this->assertEquals(0, $colour->getAlpha());
$this->assertEquals(0, Colour::toRawRgb($colour));
$this->assertEquals(0, Colour::toRawArgb($colour));
$this->assertEquals(0, Colour::toRawRgba($colour));
$colour = Colour::parse(' MediumOrchid '); $colour = Colour::parse(' MediumOrchid ');
$this->assertInstanceOf(ColourNamed::class, $colour); $this->assertInstanceOf(ColourNamed::class, $colour);
@ -362,6 +434,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0xBA, $colour->getRed()); $this->assertEquals(0xBA, $colour->getRed());
$this->assertEquals(0x55, $colour->getGreen()); $this->assertEquals(0x55, $colour->getGreen());
$this->assertEquals(0xD3, $colour->getBlue()); $this->assertEquals(0xD3, $colour->getBlue());
$this->assertEquals(0xBA55D3, Colour::toRawRgb($colour));
$this->assertEquals(0xFFBA55D3, Colour::toRawArgb($colour));
$this->assertEquals(0xBA55D3FF, Colour::toRawRgba($colour));
$colour = Colour::parse('#123'); $colour = Colour::parse('#123');
@ -372,6 +447,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x11, $colour->getRed()); $this->assertEquals(0x11, $colour->getRed());
$this->assertEquals(0x22, $colour->getGreen()); $this->assertEquals(0x22, $colour->getGreen());
$this->assertEquals(0x33, $colour->getBlue()); $this->assertEquals(0x33, $colour->getBlue());
$this->assertEquals(0x112233, Colour::toRawRgb($colour));
$this->assertEquals(0xFF112233, Colour::toRawArgb($colour));
$this->assertEquals(0x112233FF, Colour::toRawRgba($colour));
$colour = Colour::parse('#3456'); $colour = Colour::parse('#3456');
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
@ -381,6 +459,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x33, $colour->getRed()); $this->assertEquals(0x33, $colour->getRed());
$this->assertEquals(0x44, $colour->getGreen()); $this->assertEquals(0x44, $colour->getGreen());
$this->assertEquals(0x55, $colour->getBlue()); $this->assertEquals(0x55, $colour->getBlue());
$this->assertEquals(0x334455, Colour::toRawRgb($colour));
$this->assertEquals(0x66334455, Colour::toRawArgb($colour));
$this->assertEquals(0x33445566, Colour::toRawRgba($colour));
$colour = Colour::parse('#9475b2'); $colour = Colour::parse('#9475b2');
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
@ -390,6 +471,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x94, $colour->getRed()); $this->assertEquals(0x94, $colour->getRed());
$this->assertEquals(0x75, $colour->getGreen()); $this->assertEquals(0x75, $colour->getGreen());
$this->assertEquals(0xB2, $colour->getBlue()); $this->assertEquals(0xB2, $colour->getBlue());
$this->assertEquals(0x9475B2, Colour::toRawRgb($colour));
$this->assertEquals(0xFF9475B2, Colour::toRawArgb($colour));
$this->assertEquals(0x9475B2FF, Colour::toRawRgba($colour));
$colour = Colour::parse('#8559A5b3'); $colour = Colour::parse('#8559A5b3');
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
@ -399,6 +483,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x85, $colour->getRed()); $this->assertEquals(0x85, $colour->getRed());
$this->assertEquals(0x59, $colour->getGreen()); $this->assertEquals(0x59, $colour->getGreen());
$this->assertEquals(0xA5, $colour->getBlue()); $this->assertEquals(0xA5, $colour->getBlue());
$this->assertEquals(0x8559A5, Colour::toRawRgb($colour));
$this->assertEquals(0xB38559A5, Colour::toRawArgb($colour));
$this->assertEquals(0x8559A5B3, Colour::toRawRgba($colour));
$colour = Colour::parse('rgb( 17 , 34 , 51)'); $colour = Colour::parse('rgb( 17 , 34 , 51)');
@ -409,6 +496,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x11, $colour->getRed()); $this->assertEquals(0x11, $colour->getRed());
$this->assertEquals(0x22, $colour->getGreen()); $this->assertEquals(0x22, $colour->getGreen());
$this->assertEquals(0x33, $colour->getBlue()); $this->assertEquals(0x33, $colour->getBlue());
$this->assertEquals(0x112233, Colour::toRawRgb($colour));
$this->assertEquals(0xFF112233, Colour::toRawArgb($colour));
$this->assertEquals(0x112233FF, Colour::toRawRgba($colour));
$colour = Colour::parse('rgba(51 , 68, 85 ,0.4 )'); $colour = Colour::parse('rgba(51 , 68, 85 ,0.4 )');
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
@ -418,6 +508,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x33, $colour->getRed()); $this->assertEquals(0x33, $colour->getRed());
$this->assertEquals(0x44, $colour->getGreen()); $this->assertEquals(0x44, $colour->getGreen());
$this->assertEquals(0x55, $colour->getBlue()); $this->assertEquals(0x55, $colour->getBlue());
$this->assertEquals(0x334455, Colour::toRawRgb($colour));
$this->assertEquals(0x66334455, Colour::toRawArgb($colour));
$this->assertEquals(0x33445566, Colour::toRawRgba($colour));
$colour = Colour::parse('rgba( 148,117,178 )'); $colour = Colour::parse('rgba( 148,117,178 )');
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
@ -427,6 +520,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x94, $colour->getRed()); $this->assertEquals(0x94, $colour->getRed());
$this->assertEquals(0x75, $colour->getGreen()); $this->assertEquals(0x75, $colour->getGreen());
$this->assertEquals(0xB2, $colour->getBlue()); $this->assertEquals(0xB2, $colour->getBlue());
$this->assertEquals(0x9475B2, Colour::toRawRgb($colour));
$this->assertEquals(0xFF9475B2, Colour::toRawArgb($colour));
$this->assertEquals(0x9475B2FF, Colour::toRawRgba($colour));
$colour = Colour::parse('rgb(133 ,89,165,.700)'); $colour = Colour::parse('rgb(133 ,89,165,.700)');
$this->assertInstanceOf(ColourRgb::class, $colour); $this->assertInstanceOf(ColourRgb::class, $colour);
@ -436,6 +532,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(0x85, $colour->getRed()); $this->assertEquals(0x85, $colour->getRed());
$this->assertEquals(0x59, $colour->getGreen()); $this->assertEquals(0x59, $colour->getGreen());
$this->assertEquals(0xA5, $colour->getBlue()); $this->assertEquals(0xA5, $colour->getBlue());
$this->assertEquals(0x8559A5, Colour::toRawRgb($colour));
$this->assertEquals(0xB38559A5, Colour::toRawArgb($colour));
$this->assertEquals(0x8559A5B3, Colour::toRawRgba($colour));
// these results aren't really correct // these results aren't really correct
@ -453,6 +552,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(17, $colour->getRed()); $this->assertEquals(17, $colour->getRed());
$this->assertEquals(33, $colour->getGreen()); $this->assertEquals(33, $colour->getGreen());
$this->assertEquals(50, $colour->getBlue()); $this->assertEquals(50, $colour->getBlue());
$this->assertEquals(0x112132, Colour::toRawRgb($colour));
$this->assertEquals(0xFF112132, Colour::toRawArgb($colour));
$this->assertEquals(0x112132FF, Colour::toRawRgba($colour));
$colour = Colour::parse('hsl(210deg ,25%, 27% ,.6)'); $colour = Colour::parse('hsl(210deg ,25%, 27% ,.6)');
$this->assertInstanceOf(ColourHsl::class, $colour); $this->assertInstanceOf(ColourHsl::class, $colour);
@ -465,6 +567,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(52, $colour->getRed()); $this->assertEquals(52, $colour->getRed());
$this->assertEquals(69, $colour->getGreen()); $this->assertEquals(69, $colour->getGreen());
$this->assertEquals(86, $colour->getBlue()); $this->assertEquals(86, $colour->getBlue());
$this->assertEquals(0x344556, Colour::toRawRgb($colour));
$this->assertEquals(0x99344556, Colour::toRawArgb($colour));
$this->assertEquals(0x34455699, Colour::toRawRgba($colour));
$colour = Colour::parse('hsl(300grad,28%,58)'); $colour = Colour::parse('hsl(300grad,28%,58)');
$this->assertInstanceOf(ColourHsl::class, $colour); $this->assertInstanceOf(ColourHsl::class, $colour);
@ -477,6 +582,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(148, $colour->getRed()); $this->assertEquals(148, $colour->getRed());
$this->assertEquals(118, $colour->getGreen()); $this->assertEquals(118, $colour->getGreen());
$this->assertEquals(178, $colour->getBlue()); $this->assertEquals(178, $colour->getBlue());
$this->assertEquals(0x9476B2, Colour::toRawRgb($colour));
$this->assertEquals(0xFF9476B2, Colour::toRawArgb($colour));
$this->assertEquals(0x9476B2FF, Colour::toRawRgba($colour));
$colour = Colour::parse('hsl( 4.799rad ,30 , 50%)'); $colour = Colour::parse('hsl( 4.799rad ,30 , 50%)');
$this->assertInstanceOf(ColourHsl::class, $colour); $this->assertInstanceOf(ColourHsl::class, $colour);
@ -489,6 +597,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(134, $colour->getRed()); $this->assertEquals(134, $colour->getRed());
$this->assertEquals(89, $colour->getGreen()); $this->assertEquals(89, $colour->getGreen());
$this->assertEquals(166, $colour->getBlue()); $this->assertEquals(166, $colour->getBlue());
$this->assertEquals(0x8659A6, Colour::toRawRgb($colour));
$this->assertEquals(0xFF8659A6, Colour::toRawArgb($colour));
$this->assertEquals(0x8659A6FF, Colour::toRawRgba($colour));
$colour = Colour::parse('hsl( .775turn , 13,60, .54)'); $colour = Colour::parse('hsl( .775turn , 13,60, .54)');
$this->assertInstanceOf(ColourHsl::class, $colour); $this->assertInstanceOf(ColourHsl::class, $colour);
@ -501,6 +612,9 @@ final class ColourTest extends TestCase {
$this->assertEquals(157, $colour->getRed()); $this->assertEquals(157, $colour->getRed());
$this->assertEquals(140, $colour->getGreen()); $this->assertEquals(140, $colour->getGreen());
$this->assertEquals(166, $colour->getBlue()); $this->assertEquals(166, $colour->getBlue());
$this->assertEquals(0x9D8CA6, Colour::toRawRgb($colour));
$this->assertEquals(0x8A9D8CA6, Colour::toRawArgb($colour));
$this->assertEquals(0x9D8CA68A, Colour::toRawRgba($colour));
} }
public function testMix(): void { public function testMix(): void {