From 6e4d155536bcec9b9e6a26fb94157cef903a5b70 Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 15 Sep 2023 22:02:23 +0000 Subject: [PATCH] Added colour mixing and various number funcs. --- VERSION | 2 +- src/Colour/Colour.php | 21 ++++++++++++++++++++- src/XNumber.php | 29 ++++++++++++++++++++++++++++ tests/ColourTest.php | 44 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 src/XNumber.php diff --git a/VERSION b/VERSION index bbc05bc..966fa8e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2309.112008 +0.2309.152202 diff --git a/src/Colour/Colour.php b/src/Colour/Colour.php index c839a76..c031a7f 100644 --- a/src/Colour/Colour.php +++ b/src/Colour/Colour.php @@ -1,11 +1,12 @@ shouldInherit() || $colour2->shouldInherit()) + return self::none(); + + return new ColourRGB( + (int)round(XNumber::weighted($colour2->getRed(), $colour1->getRed(), $weight)), + (int)round(XNumber::weighted($colour2->getGreen(), $colour1->getGreen(), $weight)), + (int)round(XNumber::weighted($colour2->getBlue(), $colour1->getBlue(), $weight)), + XNumber::weighted($colour2->getAlpha(), $colour1->getAlpha(), $weight), + ); + } + private const MSZ_INHERIT = 0x40000000; public static function fromMisuzu(int $raw): Colour { diff --git a/src/XNumber.php b/src/XNumber.php new file mode 100644 index 0000000..3ed7605 --- /dev/null +++ b/src/XNumber.php @@ -0,0 +1,29 @@ +assertEquals(140, $colour->getGreen()); $this->assertEquals(166, $colour->getBlue()); } + + 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 + $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()); + } }