Browse Source

added colors\RGB::getDigitalHexTuple

Fabian Peter Hammerle 7 years ago
parent
commit
7478b07bc9
2 changed files with 33 additions and 0 deletions
  1. 5 0
      colors/RGB.php
  2. 28 0
      tests/colors/RGBTest.php

+ 5 - 0
colors/RGB.php

@@ -80,4 +80,9 @@ class RGB
         $tuple = $this->tuple;
         return array_map(function($v) use ($factor) { return (int)round($v * $factor); }, $tuple);
     }
+
+    public function getDigitalHexTuple($bits)
+    {
+        return array_map(function($v) { return dechex($v); }, $this->getDigitalTuple($bits));
+    }
 }

+ 28 - 0
tests/colors/RGBTest.php

@@ -267,4 +267,32 @@ class RGBTest extends \PHPUnit_Framework_TestCase
     {
         $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));
+    }
 }