Browse Source

added class colors\RGBA

Fabian Peter Hammerle 7 years ago
parent
commit
19364bc548
4 changed files with 422 additions and 11 deletions
  1. 7 7
      Image.php
  2. 5 4
      colors/RGB.php
  3. 41 0
      colors/RGBA.php
  4. 369 0
      tests/colors/RGBATest.php

+ 7 - 7
Image.php

@@ -12,8 +12,8 @@ class Image
 
     public function __destruct()
     {
-        if($this->resource) {
-            imagedestroy($this->resource);
+        if($this->_resource) {
+            imagedestroy($this->_resource);
         }
     }
 
@@ -26,7 +26,7 @@ class Image
         switch(exif_imagetype($path)) {
             case IMAGETYPE_JPEG:
                 $image = new self;
-                $image->resource = imagecreatefromjpeg($path);
+                $image->_resource = imagecreatefromjpeg($path);
                 return $image;
             default:
                 throw new \InvalidArgumentException("type of '$path' is not supported");
@@ -39,9 +39,9 @@ class Image
      */
     public function rotate($angle)
     {
-        $resource = imagerotate($this->resource, $angle, 0);
-        imagedestroy($this->resource);
-        $this->resource = $resource;
+        $resource = imagerotate($this->_resource, $angle, 0);
+        imagedestroy($this->_resource);
+        $this->_resource = $resource;
     }
 
     /**
@@ -66,6 +66,6 @@ class Image
      */
     public function saveJpeg($path)
     {
-        imagejpeg($this->resource, $path);
+        imagejpeg($this->_resource, $path);
     }
 }

+ 5 - 4
colors/RGB.php

@@ -75,10 +75,11 @@ class RGB
 
     public function getDigitalTuple($bits)
     {
-        $bits = (int)$bits;
         $factor = (2 << ($bits - 1)) - 1;
-        $tuple = $this->tuple;
-        return array_map(function($v) use ($factor) { return (int)round($v * $factor); }, $tuple);
+        return array_map(
+            function($v) use ($factor) { return (int)round($v * $factor); },
+            $this->tuple
+            );
     }
 
     public function getDigitalHexTuple($bits)
@@ -93,7 +94,7 @@ class RGB
     {
         return implode('', array_map(
             function($s) { return str_pad($s, 2, '0', STR_PAD_LEFT); },
-            $this->getDigitalHexTuple(8)
+            array_slice($this->getDigitalHexTuple(8), 0, 3)
             ));
     }
 }

+ 41 - 0
colors/RGBA.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace fphammerle\helpers\colors;
+
+class RGBA extends RGB
+{
+    private $_alpha;
+
+    public function __construct($red = 0, $green = 0, $blue = 0, $alpha = 1)
+    {
+        $this->setRed($red);
+        $this->setGreen($green);
+        $this->setBlue($blue);
+        $this->setAlpha($alpha);
+    }
+
+    public function getAlpha()
+    {
+        return $this->_alpha;
+    }
+
+    public function setAlpha($alpha)
+    {
+        $alpha = (float)$alpha;
+        if($alpha < 0 || $alpha > 1) {
+            throw new \UnexpectedValueException('value must be within [0, 1]');
+        }
+        $this->_alpha = $alpha;
+    }
+
+    public function equals(RGB $other)
+    {
+        return parent::equals($other)
+            && abs($this->alpha - $other->alpha)  < self::comparison_precision;
+    }
+
+    public function getTuple()
+    {
+        return [$this->red, $this->green, $this->blue, $this->alpha];
+    }
+}

+ 369 - 0
tests/colors/RGBATest.php

@@ -0,0 +1,369 @@
+<?php
+
+namespace fphammerle\helpers\tests\colors;
+
+use \fphammerle\helpers\colors\RGBA;
+
+class RGBATest extends \PHPUnit_Framework_TestCase
+{
+    const comparison_precision = 0.00001;
+
+    public function testConstruct0()
+    {
+        $c = new RGBA();
+        $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);
+        $this->assertEquals(1.0, $c->alpha, '', self::comparison_precision);
+    }
+
+    public function testConstruct1()
+    {
+        $c = new RGBA(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);
+        $this->assertEquals(1.0, $c->alpha, '', self::comparison_precision);
+    }
+
+    public function testConstruct2()
+    {
+        $c = new RGBA(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);
+        $this->assertEquals(1.0, $c->alpha, '', self::comparison_precision);
+    }
+
+    public function testConstruct3()
+    {
+        $c = new RGBA(0.3, 0.2, 1, 0.6);
+        $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);
+        $this->assertEquals(0.6, $c->alpha, '', self::comparison_precision);
+    }
+
+    public function equalsProvider()
+    {
+        return [
+            [new RGBA(0.0, 0.0, 0.0, 0.0), new RGBA(0.0,              0.0,              0.0,              0.0             )],
+            [new RGBA(0.0, 0.0, 0.0, 0.0), new RGBA(0,                0,                0,                0               )],
+            [new RGBA(0.0, 0.0, 0.0, 0.0), new RGBA(pow(10, -10),     pow(10, -10),     pow(10, -10),     pow(10, -10)    )],
+            [new RGBA(1.0, 1.0, 1.0, 1.0), new RGBA(1.0,              1.0,              1.0,              1.0             )],
+            [new RGBA(1.0, 1.0, 1.0, 1.0), new RGBA(1,                1,                1,                1               )],
+            [new RGBA(1.0, 1.0, 1.0, 1.0), new RGBA(1 - pow(10, -10), 1 - pow(10, -10), 1 - pow(10, -10), 1 - pow(10, -10))],
+            [new RGBA(0.1, 0.2, 0.3, 0.4), new RGBA(0.1,              0.2,              0.3,              0.4             )],
+            [new RGBA(1/7, 1/9, 1/6, 1/3), new RGBA(1/14*2,           1/36*4,           6/36,             9/27            )],
+            ];
+    }
+
+    /**
+     * @dataProvider equalsProvider
+     */
+    public function testEquals($a, $b)
+    {
+        $this->assertTrue($a->equals($b));
+        $this->assertTrue($b->equals($a));
+    }
+
+    public function unequalsProvider()
+    {
+        return [
+            [new RGBA(0.0, 0.0, 0.0), new RGBA(0.0,             0.0,             pow(10, -4)    )],
+            [new RGBA(0.0, 0.0, 0.0), new RGBA(0.0,             pow(10, -4),     0.0            )],
+            [new RGBA(0.0, 0.0, 0.0), new RGBA(0.0,             pow(10, -4),     pow(10, -4)    )],
+            [new RGBA(0.0, 0.0, 0.0), new RGBA(pow(10, -4),     0.0,             0.0            )],
+            [new RGBA(0.0, 0.0, 0.0), new RGBA(pow(10, -4),     pow(10, -4),     0.0            )],
+            [new RGBA(0.0, 0.0, 0.0), new RGBA(pow(10, -4),     pow(10, -4),     pow(10, -4)    )],
+            [new RGBA(0.1, 0.2, 0.3), new RGBA(0.1,             0.2,             0.4            )],
+            [new RGBA(0.1, 0.2, 0.3), new RGBA(0.1,             0.4,             0.3            )],
+            [new RGBA(0.1, 0.2, 0.3), new RGBA(0.4,             0.2,             0.3            )],
+            [new RGBA(1.0, 1.0, 1.0), new RGBA(1 - pow(10, -4), 1 - pow(10, -4), 1 - pow(10, -4))],
+            [new RGBA(1.0, 1.0, 1.0), new RGBA(1 - pow(10, -4), 1 - pow(10, -4), 1.0            )],
+            [new RGBA(1.0, 1.0, 1.0), new RGBA(1 - pow(10, -4), 1.0,             1.0            )],
+            [new RGBA(1.0, 1.0, 1.0), new RGBA(1.0,             1 - pow(10, -4), 1 - pow(10, -4))],
+            [new RGBA(1.0, 1.0, 1.0), new RGBA(1.0,             1 - pow(10, -4), 1.0            )],
+            [new RGBA(1.0, 1.0, 1.0), new RGBA(1.0,             1.0,             1 - pow(10, -4))],
+            [new RGBA(0.0, 0.0, 0.0, 0.0), new RGBA(0.0, 0.0, 0.0, pow(10, -4))],
+            [new RGBA(0.1, 0.0, 0.0, 0.0), new RGBA(0.0, 0.0, 0.0, 0.0        )],
+            [new RGBA(0.0, 0.2, 0.0, 0.0), new RGBA(0.0, 0.0, 0.0, 0.0        )],
+            [new RGBA(0.0, 0.0, 0.3, 0.0), new RGBA(0.0, 0.0, 0.0, 0.0        )],
+            ];
+    }
+
+    /**
+     * @dataProvider unequalsProvider
+     */
+    public function testUnequals($a, $b)
+    {
+        $this->assertFalse($a->equals($b));
+        $this->assertFalse($b->equals($a));
+    }
+
+    public function getRedProvider()
+    {
+        return [
+            [new RGBA(0,   0.1, 0.2, 0.4), 0.0],
+            [new RGBA(0.0, 0.2, 0.3, 0.4), 0.0],
+            [new RGBA(0.1, 0.2, 0.3, 0.4), 0.1],
+            [new RGBA(1/7, 0.2, 0.3, 0.4), 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 RGBA(0, 0, 0, 0);
+        $c->setRed($s);
+        $this->assertEquals($g, $c->red, '', self::comparison_precision);
+
+        $c = new RGBA(0, 0, 0, 0);
+        $c->red = $s;
+        $this->assertEquals($g, $c->red, '', self::comparison_precision);
+    }
+
+    public function getGreenProvider()
+    {
+        return [
+            [new RGBA(0.1, 0,   0.2, 0.4), 0.0],
+            [new RGBA(0.2, 0.0, 0.3, 0.4), 0.0],
+            [new RGBA(0.2, 0.1, 0.3, 0.4), 0.1],
+            [new RGBA(0.2, 1/7, 0.3, 0.4), 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 RGBA(0, 0, 0, 0);
+        $c->setGreen($s);
+        $this->assertEquals($g, $c->green, '', self::comparison_precision);
+
+        $c = new RGBA(0, 0, 0, 0);
+        $c->green = $s;
+        $this->assertEquals($g, $c->green, '', self::comparison_precision);
+    }
+
+    public function getBlueProvider()
+    {
+        return [
+            [new RGBA(0.1, 0.2, 0,   0.4), 0.0],
+            [new RGBA(0.2, 0.3, 0.0, 0.4), 0.0],
+            [new RGBA(0.2, 0.3, 0.1, 0.4), 0.1],
+            [new RGBA(0.2, 0.3, 1/7, 0.4), 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 RGBA(0, 0, 0, 0);
+        $c->setBlue($s);
+        $this->assertEquals($g, $c->blue, '', self::comparison_precision);
+
+        $c = new RGBA(0, 0, 0, 0);
+        $c->blue = $s;
+        $this->assertEquals($g, $c->blue, '', self::comparison_precision);
+    }
+
+    public function getAlphaProvider()
+    {
+        return [
+            [new RGBA(0.1, 0.2, 0.4, 0  ), 0.0],
+            [new RGBA(0.2, 0.3, 0.4, 0.0), 0.0],
+            [new RGBA(0.2, 0.3, 0.4, 0.1), 0.1],
+            [new RGBA(0.2, 0.3, 0.4, 1/7), 1/7],
+            ];
+    }
+
+    /**
+     * @dataProvider getAlphaProvider
+     */
+    public function testGetAlpha($c, $h)
+    {
+        $this->assertEquals($h, $c->alpha,      '', self::comparison_precision);
+        $this->assertEquals($h, $c->getAlpha(), '', self::comparison_precision);
+    }
+
+    public function setAlphaProvider()
+    {
+        return [
+            [0,   0.0],
+            [0,   0.0],
+            [0.1, 0.1],
+            [1/7, 1/7],
+            ];
+    }
+
+    /**
+     * @dataProvider setAlphaProvider
+     */
+    public function testSetAlpha($s, $g)
+    {
+        $c = new RGBA(0, 0, 0, 0);
+        $c->setAlpha($s);
+        $this->assertEquals($g, $c->alpha, '', self::comparison_precision);
+
+        $c = new RGBA(0, 0, 0, 0);
+        $c->alpha = $s;
+        $this->assertEquals($g, $c->alpha, '', self::comparison_precision);
+    }
+
+    public function getTupleProvider()
+    {
+        return [
+            [new RGBA(0.0, 0.0, 0.0, 0.0), [0.0, 0.0, 0.0, 0.0]],
+            [new RGBA(0.2, 0.3, 0.4, 0.5), [0.2, 0.3, 0.4, 0.5]],
+            [new RGBA(0.8, 0.9, 1.0, 0.7), [0.8, 0.9, 1.0, 0.7]],
+            [new RGBA(1/7, 1/9, 1/3, 1/4), [1/7, 1/9, 1/3, 1/4]],
+            ];
+    }
+
+    /**
+     * @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 RGBA(0.0, 0.0, 0.0, 0.0), 1, [0, 0, 0, 0]],
+            [new RGBA(0.0, 0.0, 0.0, 0.0), 2, [0, 0, 0, 0]],
+            [new RGBA(0.0, 0.0, 0.0, 0.0), 3, [0, 0, 0, 0]],
+            [new RGBA(0.2, 0.3, 0.4, 0.5), 1, [0, 0, 0, 1]],
+            [new RGBA(0.2, 0.3, 0.4, 0.5), 2, [1, 1, 1, 2]],
+            [new RGBA(0.2, 0.3, 0.4, 0.5), 3, [1, 2, 3, 4]],
+            [new RGBA(0.8, 0.9, 1.0, 0.7), 1, [1, 1, 1, 1]],
+            [new RGBA(0.8, 0.9, 1.0, 0.7), 2, [2, 3, 3, 2]],
+            [new RGBA(0.8, 0.9, 1.0, 0.7), 3, [6, 6, 7, 5]],
+            [new RGBA(1/7, 1/9, 1/3, 1/8), 1, [0, 0, 0, 0]],
+            [new RGBA(1/7, 1/9, 1/3, 1/8), 2, [0, 0, 1, 0]],
+            [new RGBA(1/7, 1/9, 1/3, 1/8), 3, [1, 1, 2, 1]],
+            [new RGBA(1/8, 1/4, 1/2, 1/4), 3, [1, 2, 4, 2]],
+            ];
+    }
+
+    /**
+     * @dataProvider getDigitalTupleProvider
+     */
+    public function testGetDigitalTuple($c, $bits, $tuple)
+    {
+        $this->assertSame($tuple, $c->getDigitalTuple($bits));
+    }
+
+    public function getDigitalHexTupleProvider()
+    {
+        return [
+            [new RGBA(0.0, 0.0, 0.0, 0.0), 1, ['0',  '0',  '0',  '0' ]],
+            [new RGBA(0.0, 0.0, 0.0, 0.0), 2, ['0',  '0',  '0',  '0' ]],
+            [new RGBA(0.0, 0.0, 0.0, 0.0), 3, ['0',  '0',  '0',  '0' ]],
+            [new RGBA(0.2, 0.3, 0.4, 0.5), 1, ['0',  '0',  '0',  '1' ]],
+            [new RGBA(0.2, 0.3, 0.4, 0.5), 2, ['1',  '1',  '1',  '2' ]],
+            [new RGBA(0.2, 0.3, 0.4, 0.5), 3, ['1',  '2',  '3',  '4' ]],
+            [new RGBA(0.8, 0.9, 1.0, 0.7), 1, ['1',  '1',  '1',  '1' ]],
+            [new RGBA(0.8, 0.9, 1.0, 0.7), 2, ['2',  '3',  '3',  '2' ]],
+            [new RGBA(0.8, 0.9, 1.0, 0.7), 4, ['c',  'e',  'f',  'b' ]],
+            [new RGBA(1/7, 1/9, 1/3, 1/8), 1, ['0',  '0',  '0',  '0' ]],
+            [new RGBA(1/7, 1/9, 1/3, 1/8), 5, ['4',  '3',  'a',  '4' ]],
+            [new RGBA(1/7, 1/9, 1/3, 1/8), 5, ['4',  '3',  'a',  '4' ]],
+            [new RGBA(1/8, 1/4, 1/2, 1/4), 6, ['8',  '10', '20', '10']],
+            [new RGBA(1/4, 1/2, 1/1, 1/8), 8, ['40', '80', 'ff', '20']],
+            ];
+    }
+
+    /**
+     * @dataProvider getDigitalHexTupleProvider
+     */
+    public function testGetDigitalHexTuple($c, $bits, $tuple)
+    {
+        $this->assertSame($tuple, $c->getDigitalHexTuple($bits));
+    }
+
+    public function getHexTripletProvider()
+    {
+        return [
+            [new RGBA(1/4, 1/2, 1/1), '4080ff'],
+            [new RGBA(0.3, 0.6, 0.9), '4d99e6'],
+            [new RGBA(1.0, 1/3, 0.0), 'ff5500'],
+            [new RGBA(1/7, 1/8, 1/9), '24201c'],
+            [new RGBA(1/16, 1/32, 1/64), '100804'],
+            [new RGBA(1/32, 1/64, 1/96), '080403'],
+            ];
+    }
+
+    /**
+     * @dataProvider getHexTripletProvider
+     */
+    public function testGetHexTriplet($c, $triplet)
+    {
+        $this->assertSame($triplet, $c->hexTriplet);
+    }
+}