Skip to content
Permalink
Browse files

Theme color affected by custom upload

  • Loading branch information...
NEMS Linux
NEMS Linux committed Mar 2, 2019
1 parent a764313 commit 2b786f7ee31b8a611b25f3bfaa3d4972774b1808
Showing with 97 additions and 31 deletions.
  1. +59 −1 config/index.php
  2. +25 −27 inc/bgcolor.php
  3. +13 −3 inc/wallpaper.php
@@ -47,6 +47,56 @@
$v++;
}
function hex2rgb($color){
$color = str_replace('#', '', $color);
if (strlen($color) != 6){ return array(0,0,0); }
$rgb = array();
for ($x=0;$x<3;$x++){
$rgb[$x] = hexdec(substr($color,(2*$x),2));
}
return $rgb;
}
function rgb2hsv($rgb){
$r = $rgb[0] / 255;
$g = $rgb[1] / 255;
$b = $rgb[2] / 255;
$v = max($r, $g, $b);
$diff = $v - min($r, $g, $b);
$diffc = function($c) use ($v, $diff) {
return ($v - $c) / 6 / $diff + 1 / 2;
};
if($diff == 0){
$h = $s = 0;
}else{
$s = $diff / $v;
$rr = $diffc($r);
$gg = $diffc($g);
$bb = $diffc($b);
if($r === $v){
$h = $bb - $gg;
}else if($g === $v){
$h = (1 / 3) + $rr - $bb;
}else if($b === $v){
$h = (2 / 3) + $gg - $rr;
}
if($h < 0){
$h += 1;
}else if($h > 1){
$h -= 1;
}
}
$hsv = round($h * 360) . ',' . round($s * 100) . '%,' . round($v * 100) . '%';
return $hsv;
}
// Nagios config
if (ver('nems') < 1.4) {
# LEGACY VERSION
@@ -99,6 +149,10 @@
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
$imguploadresp = "Image succesfully uploaded.";
$bgfileNEW = basename($_FILES['file']['name']);
// create a hex color that is the most prominent color in the photo
$image=imagecreatefromjpeg($uploadfile);
$thumb=imagecreatetruecolor(1,1); imagecopyresampled($thumb,$image,0,0,0,0,1,1,imagesx($image),imagesy($image));
$mainColor='hsv(' . rgb2hsv(hex2rgb(strtoupper(dechex(imagecolorat($thumb,0,0))))) . ')';
} else {
$imguploadresp = "Image uploading failed.";
}
@@ -179,7 +233,11 @@
$nemsconf['backgroundImage'] = $bgfileNEW;
}
$nemsconf['backgroundBlur'] = intval($_POST['backgroundBlur']) ?: 1;
$nemsconf['backgroundColor'] = sanitize($_POST['backgroundColor']) ?: '#0d0b3f';
if (isset($mainColor)) {
$nemsconf['backgroundColor'] = $mainColor;
} else {
$nemsconf['backgroundColor'] = sanitize($_POST['backgroundColor']) ?: '0,100%,100%';
}
$nemsconf['checkin.enabled'] = intval($_POST['checkin_enabled']) ?: 0;
$nemsconf['checkin.email'] = filter_var(trim($_POST['checkin_email']), FILTER_VALIDATE_EMAIL) ?: '';
$nemsconf['checkin.interval'] = intval($_POST['checkin_interval']) ?: 8; // how many 15 minute cycles before notifying. Default 8 (2 hours).
@@ -2,6 +2,30 @@
$defaultbgcolor = '040111';
if (!function_exists('hsv2rgb')) {
function hsv2rgb($hue,$sat,$val) {;
$rgb = array(0,0,0);
//calc rgb for 100% SV, go +1 for BR-range
for($i=0;$i<4;$i++) {
if (abs($hue - $i*120)<120) {
$distance = max(60,abs($hue - $i*120));
$rgb[$i % 3] = 1 - (($distance-60) / 60);
}
}
//desaturate by increasing lower levels
$max = max($rgb);
$factor = 255 * ($val/100);
for($i=0;$i<3;$i++) {
//use distance between 0 and max (1) and multiply with value
$rgb[$i] = round(($rgb[$i] + ($max - $rgb[$i]) * (1 - $sat/100)) * $factor);
}
$rgb['html'] = sprintf('%02X%02X%02X', $rgb[0], $rgb[1], $rgb[2]);
return $rgb;
}
}
$conftmp = file('/usr/local/share/nems/nems.conf');
if (is_array($conftmp) && count($conftmp) > 0) {
foreach ($conftmp as $line) {
@@ -15,7 +39,7 @@
}
// User has defined their own color
if ($background == 7) {
if ($background == 7 || $background == 8) {
$tmp=explode(',',str_replace(array('hsv(',')'),array('',''),$backgroundColor));
$h = trim($tmp[0]);
@@ -50,30 +74,4 @@
}
}
if (!function_exists('hsv2rgb')) {
function hsv2rgb($hue,$sat,$val) {;
$rgb = array(0,0,0);
//calc rgb for 100% SV, go +1 for BR-range
for($i=0;$i<4;$i++) {
if (abs($hue - $i*120)<120) {
$distance = max(60,abs($hue - $i*120));
$rgb[$i % 3] = 1 - (($distance-60) / 60);
}
}
//desaturate by increasing lower levels
$max = max($rgb);
$factor = 255 * ($val/100);
for($i=0;$i<3;$i++) {
//use distance between 0 and max (1) and multiply with value
$rgb[$i] = round(($rgb[$i] + ($max - $rgb[$i]) * (1 - $sat/100)) * $factor);
}
$rgb['html'] = sprintf('%02X%02X%02X', $rgb[0], $rgb[1], $rgb[2]);
return $rgb;
}
}
?>
@@ -43,6 +43,15 @@
switch ($background) {
case 8:
$tmp=explode(',',str_replace(array('hsv(',')'),array('',''),$backgroundColor));
$h = trim($tmp[0]);
$s = trim($tmp[1]);
$v = trim($tmp[2]);
$rgb=hsv2rgb($h,$s,$v);
$vDark = ($v-40);
if ($vDark < 1) $vDark = 1;
$rgbDark=hsv2rgb($h,$s,$vDark);
$bgcolor = $rgb['html'];
$bgimg = '/userfiles/' . $backgroundImage;
$output = "<script>jQuery(document).ready(function() {
$('" . $backgroundElem . "').backstretch([
@@ -61,8 +70,9 @@
if ($vDark < 1) $vDark = 1;
$rgbDark=hsv2rgb($h,$s,$vDark);
$output = "
<style>$backgroundElem { background-image: radial-gradient(" . $rgb['html'] . "," . $rgbDark['html'] . "); }</style>
<style>$backgroundElem { background-image: radial-gradient(#" . $rgb['html'] . ",#" . $rgbDark['html'] . "); }</style>
";
$bgcolor = $rgb['html'];
break;
case 9:
@@ -90,12 +100,12 @@
}
list($rgb[0], $rgb[1], $rgb[2]) = sscanf($bgcolor, "%02x%02x%02x");
$rgb['html'] = sprintf('#%02X%02X%02X', $rgb[0], $rgb[1], $rgb[2]);
$rgb['html'] = sprintf('%02X%02X%02X', $rgb[0], $rgb[1], $rgb[2]);
$vDark = ($v-40);
if ($vDark < 1) $vDark = 1;
$rgbDark=hsv2rgb($h,$s,$vDark);
$output = "
<style>$backgroundElem { background-image: radial-gradient(" . $rgb['html'] . "," . $rgbDark['html'] . "); }</style>
<style>$backgroundElem { background-image: radial-gradient(#" . $rgb['html'] . ",#" . $rgbDark['html'] . "); }</style>
";
break;

0 comments on commit 2b786f7

Please sign in to comment.
You can’t perform that action at this time.