Skip to content

Commit

Permalink
Merge pull request #116 from mbarbey/patch-1
Browse files Browse the repository at this point in the history
Add missing setRGB and getRGB functions
  • Loading branch information
sqmk committed Nov 16, 2016
2 parents db67f88 + ef650c1 commit e668f9b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
42 changes: 42 additions & 0 deletions library/Phue/Group.php
Expand Up @@ -12,6 +12,7 @@
use Phue\Command\SetLightState;
use Phue\Command\SetGroupAttributes;
use Phue\Command\SetGroupState;
use Phue\Helper\ColorConversion;

/**
* Group object
Expand Down Expand Up @@ -330,6 +331,47 @@ public function setXY($x, $y)
return $this;
}

/**
* Get calculated RGB
*
* @return array red, green, blue key/value
*/
public function getRGB()
{
$xy = $this->getXY();
$bri = $this->getBrightness();
$rgb = ColorConversion::convertXYToRGB($xy['x'], $xy['y'], $bri);

return $rgb;
}

/**
* Set XY and brightness calculated from RGB
*
* @param int $red Red value
* @param int $green Green value
* @param int $blue Blue value
*
* @return self This object
*/
public function setRGB($red, $green, $blue)
{
$x = new SetGroupState($this);
$y = $x->rgb((int) $red, (int) $green, (int) $blue);
$this->client->sendCommand($y);

// Change internal xy, brightness and colormode state
$xy = ColorConversion::convertRGBToXY($red, $green, $blue);
$this->attributes->action->xy = array(
$xy['x'],
$xy['y']
);
$this->attributes->action->bri = $xy['bri'];
$this->attributes->action->colormode = 'xy';

return $this;
}

/**
* Get Color temperature
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Phue/Test/GroupTest.php
Expand Up @@ -9,6 +9,7 @@
namespace Phue\Test;

use Phue\Client;
use Phue\Helper\ColorConversion;
use Phue\Group;

/**
Expand Down Expand Up @@ -285,6 +286,41 @@ public function testGetSetXY()
), $this->group->getXY());
}

/**
* Test: Get/Set RGB
*
* @covers \Phue\Group::getRGB
* @covers \Phue\Group::setRGB
*/
public function testGetSetRGB()
{
$this->stubMockClientSendSetGroupStateCommand();

// Make sure original rgb is retrievable
$rgb = ColorConversion::convertXYToRGB(
$this->attributes->action->xy[0],
$this->attributes->action->xy[1],
$this->attributes->action->bri
);
$this->assertEquals(
array(
'red' => $rgb['red'],
'green' => $rgb['green'],
'blue' => $rgb['blue']
), $this->group->getRGB());

// Ensure setRGB returns self
$this->assertEquals($this->group, $this->group->setRGB(50, 50, 50));

// Make sure group attributes are updated
$this->assertEquals(
array(
'red' => 50,
'green' => 50,
'blue' => 50
), $this->group->getRGB());
}

/**
* Test: Get/Set Color temp
*
Expand Down

0 comments on commit e668f9b

Please sign in to comment.