Skip to content

Commit

Permalink
GitHub #1 - Coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmk committed May 29, 2016
1 parent fefba26 commit 6b8ebf3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 59 deletions.
9 changes: 4 additions & 5 deletions library/Phue/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
*/
class Client
{

/**
* Host address
*
Expand Down Expand Up @@ -83,7 +82,7 @@ public function getHost()
public function setHost($host)
{
$this->host = (string) $host;

return $this;
}

Expand All @@ -108,7 +107,7 @@ public function getUsername()
public function setUsername($username)
{
$this->username = (string) $username;

return $this;
}

Expand Down Expand Up @@ -213,7 +212,7 @@ public function getTransport()
if ($this->transport === null) {
$this->setTransport(new Http($this));
}

return $this->transport;
}

Expand All @@ -228,7 +227,7 @@ public function getTransport()
public function setTransport(TransportInterface $transport)
{
$this->transport = $transport;

return $this;
}

Expand Down
40 changes: 18 additions & 22 deletions library/Phue/Helper/ColorConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@
*/
namespace Phue\Helper;


/**
* Helper for converting colors to and from rgb
*/
class ColorConversion
{
/**
* Converts RGB values to XY values
* Based on: http://stackoverflow.com/a/22649803
*
* @param int $red
* Red value
* @param int $green
* Green value
* @param int $blue
* Blue value
*
* @param int $red Red value
* @param int $green Green value
* @param int $blue Blue value
*
* @return array x, y, bri key/value
*/
Expand Down Expand Up @@ -53,24 +52,21 @@ 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,
'x' => $x,
'y' => $y,
'bri' => round($xyz['y'] * 255)
);
}

/**
* Converts XY (and brightness) values to RGB
*
* @param float $x
* X value
* @param float $y
* Y value
* @param int $bri
* Brightness value
*
*
* @param float $x X value
* @param float $y Y value
* @param int $bri Brightness value
*
* @return array red, green, blue key/value
*/
public static function convertXYToRGB($x, $y, $bri = 255)
Expand All @@ -85,19 +81,19 @@ public static function convertXYToRGB($x, $y, $bri = 255)
$color['red'] = $xyz['x'] * 1.656492 - $xyz['y'] * 0.354851 - $xyz['z'] * 0.255038;
$color['green'] = -$xyz['x'] * 0.707196 + $xyz['y'] * 1.655397 + $xyz['z'] * 0.036152;
$color['blue'] = $xyz['x'] * 0.051713 - $xyz['y'] * 0.121364 + $xyz['z'] * 1.011530;

foreach ($color as $key => $normalized) {
// Apply reverse gamma correction
if ($normalized <= 0.0031308) {
$color[$key] = 12.92 * $normalized;
} else {
$color[$key] = (1.0 + 0.055) * pow($normalized, 1.0 / 2.4) - 0.055;
}

// Scale back from a maximum of 1 to a maximum of 255
$color[$key] = round($color[$key] * 255);
}

return $color;
}
}
57 changes: 26 additions & 31 deletions library/Phue/Light.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
class Light
{

/**
* Id
*
Expand Down Expand Up @@ -87,9 +86,9 @@ public function getName()
public function setName($name)
{
$this->client->sendCommand(new Command\SetLightName($this, (string) $name));

$this->attributes->name = (string) $name;

return $this;
}

Expand Down Expand Up @@ -166,9 +165,9 @@ public function setOn($flag = true)
$x = new SetLightState($this);
$y = $x->on((bool) $flag);
$this->client->sendCommand($y);

$this->attributes->state->on = (bool) $flag;

return $this;
}

Expand All @@ -195,9 +194,9 @@ public function setAlert($mode = SetLightState::ALERT_LONG_SELECT)
$x = new SetLightState($this);
$y = $x->alert($mode);
$this->client->sendCommand($y);

$this->attributes->state->alert = $mode;

return $this;
}

Expand All @@ -224,9 +223,9 @@ public function setEffect($mode = SetLightState::EFFECT_NONE)
$x = new SetLightState($this);
$y = $x->effect($mode);
$this->client->sendCommand($y);

$this->attributes->state->effect = $mode;

return $this;
}

Expand All @@ -253,9 +252,9 @@ public function setBrightness($level = SetLightState::BRIGHTNESS_MAX)
$x = new SetLightState($this);
$y = $x->brightness((int) $level);
$this->client->sendCommand($y);

$this->attributes->state->bri = (int) $level;

return $this;
}

Expand All @@ -282,11 +281,11 @@ public function setHue($value)
$x = new SetLightState($this);
$y = $x->hue((int) $value);
$this->client->sendCommand($y);

// Change both hue and color mode state
$this->attributes->state->hue = (int) $value;
$this->attributes->state->colormode = 'hs';

return $this;
}

Expand All @@ -313,11 +312,11 @@ public function setSaturation($value)
$x = new SetLightState($this);
$y = $x->saturation((int) $value);
$this->client->sendCommand($y);

// Change both saturation and color mode state
$this->attributes->state->sat = (int) $value;
$this->attributes->state->colormode = 'hs';

return $this;
}

Expand Down Expand Up @@ -349,14 +348,14 @@ public function setXY($x, $y)
$_x = new SetLightState($this);
$_y = $_x->xy((float) $x, (float) $y);
$this->client->sendCommand($_y);

// Change both internal xy and colormode state
$this->attributes->state->xy = array(
$x,
$y
);
$this->attributes->state->colormode = 'xy';

return $this;
}

Expand All @@ -367,7 +366,7 @@ public function setXY($x, $y)
*/
public function getRGB()
{
$xy = $this->getXY();
$xy = $this->getXY();
$bri = $this->getBrightness();
$rgb = ColorConversion::convertXYToRGB($xy['x'], $xy['y'], $bri);

Expand All @@ -376,14 +375,11 @@ public function getRGB()

/**
* Set XY and brightness calculated from RGB
*
* @param int $red
* Red value
* @param int $green
* Green value
* @param int $blue
* Blue value
*
*
* @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)
Expand All @@ -400,7 +396,7 @@ public function setRGB($red, $green, $blue)
);
$this->attributes->state->bri = $xy['bri'];
$this->attributes->state->colormode = 'xy';

return $this;
}

Expand All @@ -417,8 +413,7 @@ public function getColorTemp()
/**
* Set Color temperature
*
* @param int $value
* Color temperature value
* @param int $value Color temperature value
*
* @return self This object
*/
Expand All @@ -427,11 +422,11 @@ public function setColorTemp($value)
$x = new SetLightState($this);
$y = $x->colorTemp((int) $value);
$this->client->sendCommand($y);

// Change both internal color temp and colormode state
$this->attributes->state->ct = (int) $value;
$this->attributes->state->colormode = 'ct';

return $this;
}

Expand Down
1 change: 0 additions & 1 deletion library/Phue/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
class User
{

/**
* Username
*
Expand Down

0 comments on commit 6b8ebf3

Please sign in to comment.