Skip to content

Commit

Permalink
Update v1.1.20240224
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilkware committed Feb 24, 2024
1 parent 1245c2e commit 1ff205a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
34 changes: 17 additions & 17 deletions Color Loop/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ public function Create()
// Status variable (active)
$exists = @$this->GetIDForIdent('active');
$this->RegisterVariableBoolean('active', $this->Translate('Active'), '~Switch', 0);
if($exists === false) {
if ($exists === false) {
$this->SetValueBoolean('active', true);
}
$this->EnableAction('active');
// Status variable (increment)
$exists = @$this->GetIDForIdent('increment');
$this->RegisterVariableInteger('increment', $this->Translate('Increment'), 'WWXCL.Increment', 1);
if($exists === false) {
if ($exists === false) {
$this->SetValueInteger('increment', 5);
}
$this->EnableAction('increment');
// Status variable (transition)
$exists = @$this->GetIDForIdent('transition');
$vid = $this->RegisterVariableInteger('transition', $this->Translate('Transition'), 'WWXCL.Transition', 2);
if($exists === false) {
if ($exists === false) {
$this->SetValueInteger('transition', 5);
}
$this->EnableAction('transition');
Expand Down Expand Up @@ -119,11 +119,11 @@ public function ApplyChanges()
foreach ($list as &$line) {
if (IPS_VariableExists($line['Variable'])) {
$this->RegisterReference($line['Variable']);
if($this->GetVariableProfile($line['Variable']) != '~HexColor') {
if ($this->GetVariableProfile($line['Variable']) != '~HexColor') {
$this->SendDebug(__FUNCTION__, $line['Variable'] . 'has wrong Profile!');
$profile = false;
}
if(empty($line['Name'])) {
if (empty($line['Name'])) {
$this->SendDebug(__FUNCTION__, $line['Variable'] . 'has no Name!');
$name = false;
}
Expand All @@ -136,12 +136,12 @@ public function ApplyChanges()
}
}
// No lights
if($count == 0) {
if ($count == 0) {
$this->SetStatus(202);
return;
}
// Wrong Profile
if($profile != true) {
if ($profile != true) {
$this->SetStatus(203);
return;
}
Expand Down Expand Up @@ -222,7 +222,7 @@ public function RequestAction($ident, $value)
default:
// should only be 'color_xxxxx'
$this->SetValueInteger($ident, $value);
break;
break;
}
return true;
}
Expand All @@ -235,11 +235,11 @@ public function RequestAction($ident, $value)
private function Active($value)
{
$this->SendDebug(__FUNCTION__, $value);
if($value) { // Acvitvate
if ($value) { // Acvitvate
$vid = $this->ReadPropertyInteger('StateVariable');
if (IPS_VariableExists($vid)) {
$value = GetValue($vid); // no modul getvalue!!!
if($value) {
if ($value) {
$this->SendDebug(__FUNCTION__, 'Activate now!');
$this->Switch(true);
} else {
Expand All @@ -262,14 +262,14 @@ private function Switch($value)
$this->SendDebug(__FUNCTION__, ($value ? 'true' : 'false'));
$cact = $this->ReadPropertyBoolean('CheckActive');
$ccon = $this->ReadPropertyBoolean('CheckContinue');
if($value) { // ON
if ($value) { // ON
$ison = $this->GetValue('active');
if($cact && !$ison) {
if ($cact && !$ison) {
$ison = true;
$this->SetValueBoolean('active', $ison);
}
// only if color loop is active switched!
if($ison) {
if ($ison) {
$tran = $this->GetValue('transition');
$this->SendDebug(__FUNCTION__, 'Trans: ' . $tran);
$ccol = $this->ReadPropertyBoolean('CheckColor');
Expand All @@ -285,14 +285,14 @@ private function Switch($value)
$color = $this->GetValue($ident);
}
// color is transparent then from light
if($color == -1) {
if ($color == -1) {
$color = GetValue($varid);
}
$data[] = [$varid, $color];
}
$this->SendDebug(__FUNCTION__, 'Data: ' . print_r($data, true), 0);
$buffer = $this->GetBuffer('loop_data');
if(!$ccon || empty($buffer)) {
if (!$ccon || empty($buffer)) {
$this->SetBuffer('loop_data', serialize($data));
}
// Start Timer
Expand All @@ -301,7 +301,7 @@ private function Switch($value)
} else { // OFF
$this->SetTimerInterval('ColorLoopTrigger', 0);
// continue with the last colors?
if(!$ccon) {
if (!$ccon) {
$this->SetBuffer('loop_data', '');
}
}
Expand All @@ -318,7 +318,7 @@ private function Cycle()
$step = $this->GetValue('increment');
$this->SendDebug(__FUNCTION__, 'Step size: ' . $step);
$next = [];
foreach($last as $index => $data) {
foreach ($last as $index => $data) {
$this->SendDebug(__FUNCTION__, '(1): ' . $data[1]);
$rgb = $this->int2rgb($data[1]);
$this->SendDebug(__FUNCTION__, '(2): ' . $rgb[0] . ', ' . $rgb[1] . ', ' . $rgb[2]);
Expand Down
30 changes: 21 additions & 9 deletions libs/ColorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private function str2rgb(string $str): array
{
$str = preg_replace('~[^0-9a-f]~', '', $str);
$rgb = str_split($str, 2);
for($i = 0; $i < 3; $i++) {
for ($i = 0; $i < 3; $i++) {
$rgb[$i] = intval($rgb[$i], 16);
}
return $rgb;
Expand Down Expand Up @@ -90,13 +90,13 @@ private function rgb2hsl(int $r, int $g, int $b): array
case $r:
$h = 60 * fmod((($g - $b) / $d), 6);
if ($b > $g) $h += 360;
break;
break;
case $g:
$h = 60 * (($b - $r) / $d + 2);
break;
case $b:
$h = 60 * (($r - $g) / $d + 4);
break;
break;
}
}
return [intval(round($h, 0)), intval(round($s * 100, 0)), intval(round($l * 100, 0))];
Expand All @@ -117,17 +117,29 @@ private function hsl2rgb(int $h, int $s, int $l): array
$x = $c * (1 - abs(fmod(($h / 60), 2) - 1));
$m = ($l / 100) - ($c / 2);
if ($h < 60) {
$r = $c; $g = $x; $b = 0;
$r = $c;
$g = $x;
$b = 0;
} elseif ($h < 120) {
$r = $x; $g = $c; $b = 0;
$r = $x;
$g = $c;
$b = 0;
} elseif ($h < 180) {
$r = 0; $g = $c; $b = $x;
$r = 0;
$g = $c;
$b = $x;
} elseif ($h < 240) {
$r = 0; $g = $x; $b = $c;
$r = 0;
$g = $x;
$b = $c;
} elseif ($h < 300) {
$r = $x; $g = 0; $b = $c;
$r = $x;
$g = 0;
$b = $c;
} else {
$r = $c; $g = 0; $b = $x;
$r = $c;
$g = 0;
$b = $x;
}
return [floor(($r + $m) * 255), floor(($g + $m) * 255), floor(($b + $m) * 255)];
}
Expand Down

0 comments on commit 1ff205a

Please sign in to comment.