Browse Source

added colors\RGB::getHexTriplet

Fabian Peter Hammerle 7 years ago
parent
commit
6a875a3e47
2 changed files with 31 additions and 0 deletions
  1. 11 0
      colors/RGB.php
  2. 20 0
      tests/colors/RGBTest.php

+ 11 - 0
colors/RGB.php

@@ -85,4 +85,15 @@ class RGB
     {
         return array_map(function($v) { return dechex($v); }, $this->getDigitalTuple($bits));
     }
+
+    /**
+     * @return string
+     */
+    public function getHexTriplet()
+    {
+        return implode('', array_map(
+            function($s) { return str_pad($s, 2, '0', STR_PAD_LEFT); },
+            $this->getDigitalHexTuple(8)
+            ));
+    }
 }

+ 20 - 0
tests/colors/RGBTest.php

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