Skip to content

Commit

Permalink
Merge pull request #3 from wearebuilders/bugfix/sqmk#130/fix-color-hi…
Browse files Browse the repository at this point in the history
…gher-than-255

sqmk#130 Fix color higher than 255
  • Loading branch information
neoteknic authored May 19, 2018
2 parents aaaf427 + b8b4209 commit b2cc9e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions library/Phue/Helper/ColorConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function convertRGBToXY($red, $green, $blue)
$normalizedToOne['red'] = $red / 255;
$normalizedToOne['green'] = $green / 255;
$normalizedToOne['blue'] = $blue / 255;

// Make colors more vivid
foreach ($normalizedToOne as $key => $normalized) {
if ($normalized > 0.04045) {
Expand All @@ -38,12 +38,12 @@ public static function convertRGBToXY($red, $green, $blue)
$color[$key] = $normalized / 12.92;
}
}

// Convert to XYZ using the Wide RGB D65 formula
$xyz['x'] = $color['red'] * 0.664511 + $color['green'] * 0.154324 + $color['blue'] * 0.162028;
$xyz['y'] = $color['red'] * 0.283881 + $color['green'] * 0.668433 + $color['blue'] * 0.047685;
$xyz['z'] = $color['red'] * 0.000000 + $color['green'] * 0.072310 + $color['blue'] * 0.986039;

// Calculate the x/y values
if (array_sum($xyz) == 0) {
$x = 0;
Expand All @@ -52,7 +52,7 @@ public static function convertRGBToXY($red, $green, $blue)
$x = $xyz['x'] / array_sum($xyz);
$y = $xyz['y'] / array_sum($xyz);
}

return array(
'x' => $x,
'y' => $y,
Expand Down Expand Up @@ -109,4 +109,4 @@ public static function convertXYToRGB($x, $y, $bri = 255)

return $color;
}
}
}
18 changes: 9 additions & 9 deletions tests/Phue/Test/Helper/ColorConversionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,32 @@ class ColorConversionTest extends \PHPUnit_Framework_TestCase
{
/**
* Test: convert RGB to XY and brightness
*
*
* @covers \Phue\Helper\ColorConversion::convertRGBToXY
*/
public function testConvertRGBToXY()
{
// Values from: http://www.developers.meethue.com/documentation/hue-xy-values

// Alice Blue
$xy = ColorConversion::convertRGBToXY(239, 247, 255);
$this->assertEquals(0.3088, $xy['x'], '', 0.0001);
$this->assertEquals(0.3212, $xy['y'], '', 0.0001);
$this->assertEquals(233, $xy['bri']);

// Firebrick
$xy = ColorConversion::convertRGBToXY(178, 33, 33);
$this->assertEquals(0.6622, $xy['x'], '', 0.0001);
$this->assertEquals(0.3024, $xy['y'], '', 0.0001);
$this->assertEquals(35, $xy['bri']);

// Medium Sea Green
$xy = ColorConversion::convertRGBToXY(61, 178, 112);
$this->assertEquals(0.1979, $xy['x'], '', 0.0001);
$this->assertEquals(0.5005, $xy['y'], '', 0.0001);
$this->assertEquals(81, $xy['bri']);
}

/**
* Test: convert XY and brightness to RGB
*
Expand All @@ -50,19 +50,19 @@ public function testConvertRGBToXY()
public function testConvertXYToRGB()
{
// Conversion back from the test above

// Alice Blue
$rgb = ColorConversion::convertXYToRGB(0.3088, 0.3212, 233);
$this->assertEquals($rgb['red'], 239);
$this->assertEquals($rgb['green'], 247);
$this->assertEquals($rgb['blue'], 255);

// Firebrick
$rgb = ColorConversion::convertXYToRGB(0.6622, 0.3024, 35);
$this->assertEquals($rgb['red'], 178);
$this->assertEquals($rgb['green'], 33);
$this->assertEquals($rgb['blue'], 33);

// Medium Sea Green
$rgb = ColorConversion::convertXYToRGB(0.1979, 0.5005, 81);
$this->assertEquals($rgb['red'], 61);
Expand All @@ -76,4 +76,4 @@ public function testConvertXYToRGB()
$this->assertEquals($rgb['green'], 186);
$this->assertEquals($rgb['blue'], 0);
}
}
}

0 comments on commit b2cc9e4

Please sign in to comment.