123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- namespace fphammerle\helpers\tests\colors;
- use \fphammerle\helpers\colors\RGB;
- class RGBTest extends \PHPUnit_Framework_TestCase
- {
- const comparison_precision = 0.00001;
- public function testConstruct0()
- {
- $c = new RGB();
- $this->assertEquals(0.0, $c->red, '', self::comparison_precision);
- $this->assertEquals(0.0, $c->green, '', self::comparison_precision);
- $this->assertEquals(0.0, $c->blue, '', self::comparison_precision);
- }
- public function testConstruct1()
- {
- $c = new RGB(1.0);
- $this->assertEquals(1.0, $c->red, '', self::comparison_precision);
- $this->assertEquals(0.0, $c->green, '', self::comparison_precision);
- $this->assertEquals(0.0, $c->blue, '', self::comparison_precision);
- }
- public function testConstruct2()
- {
- $c = new RGB(1.0, 0.5);
- $this->assertEquals(1.0, $c->red, '', self::comparison_precision);
- $this->assertEquals(0.5, $c->green, '', self::comparison_precision);
- $this->assertEquals(0.0, $c->blue, '', self::comparison_precision);
- }
- public function testConstruct3()
- {
- $c = new RGB(0.3, 0.2, 1);
- $this->assertEquals(0.3, $c->red, '', self::comparison_precision);
- $this->assertEquals(0.2, $c->green, '', self::comparison_precision);
- $this->assertEquals(1.0, $c->blue, '', self::comparison_precision);
- }
- public function equalsProvider()
- {
- return [
- [new RGB(0.0, 0.0, 0.0 ), new RGB(0.0, 0.0, 0.0 )],
- [new RGB(0.0, 0.0, 0.0 ), new RGB(0, 0, 0 )],
- [new RGB(0.0, 0.0, 0.0 ), new RGB(pow(10, -10), pow(10, -10), pow(10, -10) )],
- [new RGB(1.0, 1.0, 1.0 ), new RGB(1.0, 1.0, 1.0 )],
- [new RGB(1.0, 1.0, 1.0 ), new RGB(1, 1, 1 )],
- [new RGB(1.0, 1.0, 1.0 ), new RGB(1 - pow(10, -10), 1 - pow(10, -10), 1 - pow(10, -10))],
- [new RGB(0.1, 0.2, 0.3 ), new RGB(0.1, 0.2, 0.3 )],
- [new RGB(1/7, 1/9, 1/321), new RGB(1/14*2, 1/36*4, 1/321 )],
- ];
- }
- /**
- * @dataProvider equalsProvider
- */
- public function testEquals($a, $b)
- {
- $this->assertTrue($a->equals($b));
- $this->assertTrue($b->equals($a));
- }
- public function unequalsProvider()
- {
- return [
- [new RGB(0.0, 0.0, 0.0), new RGB(0.0, 0.0, pow(10, -4) )],
- [new RGB(0.0, 0.0, 0.0), new RGB(0.0, pow(10, -4), 0.0 )],
- [new RGB(0.0, 0.0, 0.0), new RGB(0.0, pow(10, -4), pow(10, -4) )],
- [new RGB(0.0, 0.0, 0.0), new RGB(pow(10, -4), 0.0, 0.0 )],
- [new RGB(0.0, 0.0, 0.0), new RGB(pow(10, -4), pow(10, -4), 0.0 )],
- [new RGB(0.0, 0.0, 0.0), new RGB(pow(10, -4), pow(10, -4), pow(10, -4) )],
- [new RGB(0.1, 0.2, 0.3), new RGB(0.1, 0.2, 0.4 )],
- [new RGB(0.1, 0.2, 0.3), new RGB(0.1, 0.4, 0.3 )],
- [new RGB(0.1, 0.2, 0.3), new RGB(0.4, 0.2, 0.3 )],
- [new RGB(1.0, 1.0, 1.0), new RGB(1 - pow(10, -4), 1 - pow(10, -4), 1 - pow(10, -4))],
- [new RGB(1.0, 1.0, 1.0), new RGB(1 - pow(10, -4), 1 - pow(10, -4), 1.0 )],
- [new RGB(1.0, 1.0, 1.0), new RGB(1 - pow(10, -4), 1.0, 1.0 )],
- [new RGB(1.0, 1.0, 1.0), new RGB(1.0, 1 - pow(10, -4), 1 - pow(10, -4))],
- [new RGB(1.0, 1.0, 1.0), new RGB(1.0, 1 - pow(10, -4), 1.0 )],
- [new RGB(1.0, 1.0, 1.0), new RGB(1.0, 1.0, 1 - pow(10, -4))],
- ];
- }
- /**
- * @dataProvider unequalsProvider
- */
- public function testUnequals($a, $b)
- {
- $this->assertFalse($a->equals($b));
- $this->assertFalse($b->equals($a));
- }
- public function getRedProvider()
- {
- return [
- [new RGB(0, 0.1, 0.2), 0.0],
- [new RGB(0, 0.2, 0.3), 0.0],
- [new RGB(0.1, 0.2, 0.3), 0.1],
- [new RGB(1/7, 0.2, 0.3), 1/7],
- ];
- }
- /**
- * @dataProvider getRedProvider
- */
- public function testGetRed($c, $h)
- {
- $this->assertEquals($h, $c->red, '', self::comparison_precision);
- $this->assertEquals($h, $c->getRed(), '', self::comparison_precision);
- }
- public function setRedProvider()
- {
- return [
- [0, 0.0],
- [0, 0.0],
- [0.1, 0.1],
- [1/7, 1/7],
- ];
- }
- /**
- * @dataProvider setRedProvider
- */
- public function testSetRed($s, $g)
- {
- $c = new RGB(0, 0, 0);
- $c->setRed($s);
- $this->assertEquals($g, $c->red, '', self::comparison_precision);
- $c = new RGB(0, 0, 0);
- $c->red = $s;
- $this->assertEquals($g, $c->red, '', self::comparison_precision);
- }
- public function getGreenProvider()
- {
- return [
- [new RGB(0.1, 0, 0.2), 0.0],
- [new RGB(0.2, 0, 0.3), 0.0],
- [new RGB(0.2, 0.1, 0.3), 0.1],
- [new RGB(0.2, 1/7, 0.3), 1/7],
- ];
- }
- /**
- * @dataProvider getGreenProvider
- */
- public function testGetGreen($c, $h)
- {
- $this->assertEquals($h, $c->green, '', self::comparison_precision);
- $this->assertEquals($h, $c->getGreen(), '', self::comparison_precision);
- }
- public function setGreenProvider()
- {
- return [
- [0, 0.0],
- [0, 0.0],
- [0.1, 0.1],
- [1/7, 1/7],
- ];
- }
- /**
- * @dataProvider setGreenProvider
- */
- public function testSetGreen($s, $g)
- {
- $c = new RGB(0, 0, 0);
- $c->setGreen($s);
- $this->assertEquals($g, $c->green, '', self::comparison_precision);
- $c = new RGB(0, 0, 0);
- $c->green = $s;
- $this->assertEquals($g, $c->green, '', self::comparison_precision);
- }
- public function getBlueProvider()
- {
- return [
- [new RGB(0.1, 0.2, 0 ), 0.0],
- [new RGB(0.2, 0.3, 0 ), 0.0],
- [new RGB(0.2, 0.3, 0.1), 0.1],
- [new RGB(0.2, 0.3, 1/7), 1/7],
- ];
- }
- /**
- * @dataProvider getBlueProvider
- */
- public function testGetBlue($c, $h)
- {
- $this->assertEquals($h, $c->blue, '', self::comparison_precision);
- $this->assertEquals($h, $c->getBlue(), '', self::comparison_precision);
- }
- public function setBlueProvider()
- {
- return [
- [0, 0.0],
- [0, 0.0],
- [0.1, 0.1],
- [1/7, 1/7],
- ];
- }
- /**
- * @dataProvider setBlueProvider
- */
- public function testSetBlue($s, $g)
- {
- $c = new RGB(0, 0, 0);
- $c->setBlue($s);
- $this->assertEquals($g, $c->blue, '', self::comparison_precision);
- $c = new RGB(0, 0, 0);
- $c->blue = $s;
- $this->assertEquals($g, $c->blue, '', self::comparison_precision);
- }
- public function getTupleProvider()
- {
- return [
- [new RGB(0.0, 0.0, 0.0), [0.0, 0.0, 0.0]],
- [new RGB(0.2, 0.3, 0.4), [0.2, 0.3, 0.4]],
- [new RGB(0.8, 0.9, 1.0), [0.8, 0.9, 1.0]],
- [new RGB(1/7, 1/9, 1/3), [1/7, 1/9, 1/3]],
- ];
- }
- /**
- * @dataProvider getTupleProvider
- */
- public function testGetTuple($c, $h)
- {
- $this->assertEquals($h, $c->tuple, '', self::comparison_precision);
- $this->assertEquals($h, $c->getTuple(), '', self::comparison_precision);
- }
- public function getDigitalTupleProvider()
- {
- return [
- [new RGB(0.0, 0.0, 0.0), 1, [0, 0, 0]],
- [new RGB(0.0, 0.0, 0.0), 2, [0, 0, 0]],
- [new RGB(0.0, 0.0, 0.0), 3, [0, 0, 0]],
- [new RGB(0.2, 0.3, 0.4), 1, [0, 0, 0]],
- [new RGB(0.2, 0.3, 0.4), 2, [1, 1, 1]],
- [new RGB(0.2, 0.3, 0.4), 3, [1, 2, 3]],
- [new RGB(0.8, 0.9, 1.0), 1, [1, 1, 1]],
- [new RGB(0.8, 0.9, 1.0), 2, [2, 3, 3]],
- [new RGB(0.8, 0.9, 1.0), 3, [6, 6, 7]],
- [new RGB(1/7, 1/9, 1/3), 1, [0, 0, 0]],
- [new RGB(1/7, 1/9, 1/3), 2, [0, 0, 1]],
- [new RGB(1/7, 1/9, 1/3), 3, [1, 1, 2]],
- [new RGB(1/8, 1/4, 1/2), 3, [1, 2, 4]],
- ];
- }
- /**
- * @dataProvider getDigitalTupleProvider
- */
- public function testGetDigitalTuple($c, $bits, $tuple)
- {
- $this->assertSame($tuple, $c->getDigitalTuple($bits));
- }
- public function getDigitalHexTupleProvider()
- {
- return [
- [new RGB(0.0, 0.0, 0.0), 1, ['0', '0', '0']],
- [new RGB(0.0, 0.0, 0.0), 2, ['0', '0', '0']],
- [new RGB(0.0, 0.0, 0.0), 3, ['0', '0', '0']],
- [new RGB(0.2, 0.3, 0.4), 1, ['0', '0', '0']],
- [new RGB(0.2, 0.3, 0.4), 2, ['1', '1', '1']],
- [new RGB(0.2, 0.3, 0.4), 3, ['1', '2', '3']],
- [new RGB(0.8, 0.9, 1.0), 1, ['1', '1', '1']],
- [new RGB(0.8, 0.9, 1.0), 2, ['2', '3', '3']],
- [new RGB(0.8, 0.9, 1.0), 4, ['c', 'e', 'f']],
- [new RGB(1/7, 1/9, 1/3), 1, ['0', '0', '0']],
- [new RGB(1/7, 1/9, 1/3), 5, ['4', '3', 'a']],
- [new RGB(1/7, 1/9, 1/3), 5, ['4', '3', 'a']],
- [new RGB(1/8, 1/4, 1/2), 6, ['8', '10', '20']],
- [new RGB(1/4, 1/2, 1/1), 8, ['40', '80', 'ff']],
- ];
- }
- /**
- * @dataProvider getDigitalHexTupleProvider
- */
- public function testGetDigitalHexTuple($c, $bits, $tuple)
- {
- $this->assertSame($tuple, $c->getDigitalHexTuple($bits));
- }
- public function getHexTripletProvider()
- {
- return [
- [new RGB(1/4, 1/2, 1/1), '4080ff'],
- [new RGB(0.3, 0.6, 0.9), '4d99e6'],
- [new RGB(1.0, 1/3, 0.0), 'ff5500'],
- [new RGB(1/7, 1/8, 1/9), '24201c'],
- [new RGB(1/16, 1/32, 1/64), '100804'],
- [new RGB(1/32, 1/64, 1/96), '080403'],
- ];
- }
- /**
- * @dataProvider getHexTripletProvider
- */
- public function testGetHexTriplet($c, $triplet)
- {
- $this->assertSame($triplet, $c->hexTriplet);
- }
- }
|