convertRGBToXY() public static method

Converts RGB values to XY values Based on: http://stackoverflow.com/a/22649803
public static convertRGBToXY ( integer $red, integer $green, integer $blue ) : array
$red integer Red value
$green integer Green value
$blue integer Blue value
return array x, y, bri key/value
Ejemplo n.º 1
0
 /**
  * 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.5004999999999999, $xy['y'], '', 0.0001);
     $this->assertEquals(81, $xy['bri']);
 }
Ejemplo n.º 2
0
Archivo: Group.php Proyecto: sqmk/phue
 /**
  * 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;
 }
Ejemplo n.º 3
0
 /**
  * Test: set XY and brightness via RGB
  *
  * @dataProvider providerRGB
  *
  * @covers \Phue\Command\SetLightState::rgb
  * @covers \Phue\Command\SetLightState::send
  */
 public function testRGBSend($red, $green, $blue)
 {
     // Build command
     $command = new SetLightState($this->mockLight);
     // Set expected payload
     $xy = ColorConversion::convertRGBToXY($red, $green, $blue);
     $this->stubTransportSendRequestWithPayload((object) array('xy' => array($xy['x'], $xy['y']), 'bri' => $xy['bri']));
     // Ensure instance is returned
     $this->assertEquals($command, $command->rgb($red, $green, $blue));
     // Send
     $command->send($this->mockClient);
 }
Ejemplo n.º 4
0
 /**
  * Sets xy and brightness calculated from RGB
  *
  * @param int $red
  *          Red value
  * @param int $green
  *          Green value
  * @param int $blue
  *          Blue value
  *
  * @throws \InvalidArgumentException
  *
  * @return self This object
  */
 public function rgb($red, $green, $blue)
 {
     // Don't continue if rgb values are invalid
     foreach (array($red, $green, $blue) as $value) {
         if (!(self::RGB_MIN <= $value && $value <= self::RGB_MAX)) {
             throw new \InvalidArgumentException("RGB values must be between " . self::RGB_MIN . " and " . self::RGB_MAX);
         }
     }
     $xy = ColorConversion::convertRGBToXY($red, $green, $blue);
     return $this->xy($xy['x'], $xy['y'])->brightness($xy['bri']);
 }