Added function to convert to Misuzu format colours.

This commit is contained in:
flash 2023-01-02 22:37:10 +00:00
parent a24cfb8e6f
commit 28634b5db3
2 changed files with 55 additions and 44 deletions

View file

@ -52,6 +52,12 @@ abstract class Colour implements Stringable {
return ColourRGB::fromRawRGB($raw);
}
public static function toMisuzu(Colour $colour): int {
if($colour->shouldInherit())
return self::MSZ_INHERIT;
return ($colour->getRed() << 16) | ($colour->getGreen() << 8) | $colour->getBlue();
}
public static function parse(?string $value): Colour {
if($value === null)
return self::$none;

View file

@ -272,54 +272,59 @@ final class ColourTest extends TestCase {
}
public function testMisuzu(): void {
$msz1 = Colour::fromMisuzu(0x40AABBCC);
$this->assertInstanceOf(ColourNull::class, $msz1);
$this->assertTrue($msz1->shouldInherit());
$this->assertEquals('inherit', $msz1);
$colour = Colour::fromMisuzu(0x40AABBCC);
$this->assertInstanceOf(ColourNull::class, $colour);
$this->assertTrue($colour->shouldInherit());
$this->assertEquals('inherit', $colour);
$this->assertEquals(0x40000000, Colour::toMisuzu($colour));
$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);
$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->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));
$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);
$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->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));
$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);
$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->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));
$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);
$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->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));
}
public function testParse(): void {