Linking a dynamic hex color change to a html background style












0















I have looked over: Programmatically Lighten or Darken a hex color (or rgb, and blend colors), and more specifically: https://github.com/PimpTrizkit/PJs/wiki/12.-Shade,-Blend-and-Convert-a-Web-Color-(pSBC.js) to try and implement a similar change in color to my program.



The issue I seem to be having is that when I store the resulted rgb values from my newRGB function to a variable named c and use the value from c to implement on my cold function, the color change is not happening.



I am able to store the convertedRGB array and see 100 rgb values in the console, but am unable to use those 100 values to replace the color value for my cold() function.



The goal: when a user increases a value in the browser, the resulting background color of all three input boxes should either grow more red(increasing to full red, starting at black from the halfway point between 0 and 100 for celsius and the opposite when decreasing.



Basically, from 50 to 100 C, the color background color should start at full black and grow to full red. And from 50 to 0 C, the color background should go from full black to full blue.



Note: The 100 rgb values I computed in the console go from a blueish to white.






const celciusInput_input = document.querySelector('#celcius > input');
const farenheitInput_input = document.querySelector('#farenheit > input');
const kelvinInput_input = document.querySelector('#kelvin > input');

function roundNum(num) {
return Math.round(num * 100) / 100; //round to 100th place (where num is any float number)
}

const pSBC = function (p, from, to) {
if (typeof (p) != "number" || p < -1 || p > 1 || typeof (from) != "string" || (from[0] != 'r' && from[0] != '#') || (to && typeof (to) != "string")) return null; //ErrorCheck
if (!this.pSBCr) this.pSBCr = (d) => {
let l = d.length,
RGB = {};
if (l > 9) {
d = d.split(",");
if (d.length < 3 || d.length > 4) return null; //ErrorCheck
RGB[0] = i(d[0].split("(")[1]), RGB[1] = i(d[1]), RGB[2] = i(d[2]), RGB[3] = d[3] ? parseFloat(d[3]) : -1;
} else {
if (l == 8 || l == 6 || l < 4) return null; //ErrorCheck
if (l < 6) d = "#" + d[1] + d[1] + d[2] + d[2] + d[3] + d[3] + (l > 4 ? d[4] + "" + d[4] : ""); //3 or 4 digit
d = i(d.slice(1), 16), RGB[0] = d >> 16 & 255, RGB[1] = d >> 8 & 255, RGB[2] = d & 255, RGB[3] = -1;
if (l == 9 || l == 5) RGB[3] = r((RGB[2] / 255) * 10000) / 10000, RGB[2] = RGB[1], RGB[1] = RGB[0], RGB[0] = d >> 24 & 255;
}
return RGB;
}
var i = parseInt,
r = Math.round,
h = from.length > 9,
h = typeof (to) == "string" ? to.length > 9 ? true : to == "c" ? !h : false : h,
b = p < 0,
p = b ? p * -1 : p,
to = to && to != "c" ? to : b ? "#000000" : "#FFFFFF",
f = this.pSBCr(from),
t = this.pSBCr(to);
if (!f || !t) return null; //ErrorCheck
if (h) return "rgb" + (f[3] > -1 || t[3] > -1 ? "a(" : "(") + r((t[0] - f[0]) * p + f[0]) + "," + r((t[1] - f[1]) * p + f[1]) + "," + r((t[2] - f[2]) * p + f[2]) + (f[3] < 0 && t[3] < 0 ? ")" : "," + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 10000) / 10000 : t[3] < 0 ? f[3] : t[3]) + ")");
else return "#" + (0x100000000 + r((t[0] - f[0]) * p + f[0]) * 0x1000000 + r((t[1] - f[1]) * p + f[1]) * 0x10000 + r((t[2] - f[2]) * p + f[2]) * 0x100 + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 255) : t[3] > -1 ? r(t[3] * 255) : f[3] > -1 ? r(f[3] * 255) : 255)).toString(16).slice(1, f[3] > -1 || t[3] > -1 ? undefined : -2);
}

let color1 = "rgb(153, 175, 255)";

let newRGB = function numChange() {
const convertedRGB = ;
for (let i = 0; i < 100; i++) {
let percentage = i / 100;
c = pSBC(percentage, color1);
convertedRGB.push(c);
}
return convertedRGB;
}
console.log(newRGB(100));

c = pSBC(newRGB(100), color1); // rgb(20,60,200) + [42% Lighter] => rgb(119,142,223)


function hot() {
farenheitInput_input.style.background = 'red';
celciusInput_input.style.background = 'red';
kelvinInput_input.style.background = 'red';
}

function cold() {
farenheitInput_input.style.background = '#89e3ff';
celciusInput_input.style.background = '#89e3ff';
kelvinInput_input.style.background = '#89e3ff';
}

function defaultColor() {
farenheitInput_input.style.background = 'black';
celciusInput_input.style.background = 'black';
kelvinInput_input.style.background = 'black';
}

function converterCtoFK() {
const cTemp = parseFloat(celciusInput_input.value);
const fTemp = (cTemp * (9 / 5) + 32);
const kTemp = (cTemp + 273.15);
farenheitInput_input.value = (roundNum(fTemp));
kelvinInput_input.value = roundNum(kTemp);
if (cTemp >= 100) {
hot();
} else if (cTemp <= 0) {
cold();
} else {
defaultColor();
}
}


function converterFtoCK() {
const fTemp = parseFloat(farenheitInput_input.value);
const cTemp = ((fTemp - 32) * 5 / 9);
const kTemp = ((fTemp + 459.67) * 5 / 9);
celciusInput_input.value = roundNum(cTemp);
kelvinInput_input.value = roundNum(kTemp);
if (fTemp >= 212) {
hot();
} else if (32 > fTemp) {
cold();
} else {
defaultColor();
}
}

function converterKtoCF() {
const kTemp = parseFloat(kelvinInput_input.value);
const cTemp = (kTemp - 273.5);
const fTemp = (9 / 5 * (kTemp - 273) + 32);
celciusInput_input.value = roundNum(cTemp);
farenheitInput_input.value = roundNum(fTemp);
if (kTemp >= 373.15) {
hot();
} else if (273.15 > kTemp) {
cold();
} else {
defaultColor();
}

}

function main() {
celciusInput_input.addEventListener('input', converterCtoFK);
farenheitInput_input.addEventListener('input', converterFtoCK)
kelvinInput_input.addEventListener('input', converterKtoCF)
}

main();

* {
padding: 0;
margin: 0;
box-sizing: border-box;
}

body {
background: black;
}
div {
height: 33.3vh;
}

#farenheit {
border-top: 4px solid white;
border-bottom: 4px solid white;
}

input[type=number] {
width: 100%;
height: 100%;
background: black;
outline: none;
color: white;
font-size: 10em;
text-align: center;
border: 0;
font-family: avenir, sans-serif;
}

/*browser support for chrome/ safari & opera*/
::-webkit-input-placeholder {
color: #222222;
}

h1{
color:#ff0000;
}

<!DOCTYPE html>
<html>

<head>
<title>Temperature Converter</title>
<link rel="stylesheet" href="styles.css">
</head>

<body>
<div class="wrap">
<div id="celcius">
<input type="number" placeholder="celcius...">
</div>
<div id="farenheit">
<input type="number" placeholder="farenheit...">
</div>
<div id="kelvin">
<input type="number" placeholder="kelvin...">
</div>
</div>
<script src="app.js"></script>
</body>

</html>












share|improve this question



























    0















    I have looked over: Programmatically Lighten or Darken a hex color (or rgb, and blend colors), and more specifically: https://github.com/PimpTrizkit/PJs/wiki/12.-Shade,-Blend-and-Convert-a-Web-Color-(pSBC.js) to try and implement a similar change in color to my program.



    The issue I seem to be having is that when I store the resulted rgb values from my newRGB function to a variable named c and use the value from c to implement on my cold function, the color change is not happening.



    I am able to store the convertedRGB array and see 100 rgb values in the console, but am unable to use those 100 values to replace the color value for my cold() function.



    The goal: when a user increases a value in the browser, the resulting background color of all three input boxes should either grow more red(increasing to full red, starting at black from the halfway point between 0 and 100 for celsius and the opposite when decreasing.



    Basically, from 50 to 100 C, the color background color should start at full black and grow to full red. And from 50 to 0 C, the color background should go from full black to full blue.



    Note: The 100 rgb values I computed in the console go from a blueish to white.






    const celciusInput_input = document.querySelector('#celcius > input');
    const farenheitInput_input = document.querySelector('#farenheit > input');
    const kelvinInput_input = document.querySelector('#kelvin > input');

    function roundNum(num) {
    return Math.round(num * 100) / 100; //round to 100th place (where num is any float number)
    }

    const pSBC = function (p, from, to) {
    if (typeof (p) != "number" || p < -1 || p > 1 || typeof (from) != "string" || (from[0] != 'r' && from[0] != '#') || (to && typeof (to) != "string")) return null; //ErrorCheck
    if (!this.pSBCr) this.pSBCr = (d) => {
    let l = d.length,
    RGB = {};
    if (l > 9) {
    d = d.split(",");
    if (d.length < 3 || d.length > 4) return null; //ErrorCheck
    RGB[0] = i(d[0].split("(")[1]), RGB[1] = i(d[1]), RGB[2] = i(d[2]), RGB[3] = d[3] ? parseFloat(d[3]) : -1;
    } else {
    if (l == 8 || l == 6 || l < 4) return null; //ErrorCheck
    if (l < 6) d = "#" + d[1] + d[1] + d[2] + d[2] + d[3] + d[3] + (l > 4 ? d[4] + "" + d[4] : ""); //3 or 4 digit
    d = i(d.slice(1), 16), RGB[0] = d >> 16 & 255, RGB[1] = d >> 8 & 255, RGB[2] = d & 255, RGB[3] = -1;
    if (l == 9 || l == 5) RGB[3] = r((RGB[2] / 255) * 10000) / 10000, RGB[2] = RGB[1], RGB[1] = RGB[0], RGB[0] = d >> 24 & 255;
    }
    return RGB;
    }
    var i = parseInt,
    r = Math.round,
    h = from.length > 9,
    h = typeof (to) == "string" ? to.length > 9 ? true : to == "c" ? !h : false : h,
    b = p < 0,
    p = b ? p * -1 : p,
    to = to && to != "c" ? to : b ? "#000000" : "#FFFFFF",
    f = this.pSBCr(from),
    t = this.pSBCr(to);
    if (!f || !t) return null; //ErrorCheck
    if (h) return "rgb" + (f[3] > -1 || t[3] > -1 ? "a(" : "(") + r((t[0] - f[0]) * p + f[0]) + "," + r((t[1] - f[1]) * p + f[1]) + "," + r((t[2] - f[2]) * p + f[2]) + (f[3] < 0 && t[3] < 0 ? ")" : "," + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 10000) / 10000 : t[3] < 0 ? f[3] : t[3]) + ")");
    else return "#" + (0x100000000 + r((t[0] - f[0]) * p + f[0]) * 0x1000000 + r((t[1] - f[1]) * p + f[1]) * 0x10000 + r((t[2] - f[2]) * p + f[2]) * 0x100 + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 255) : t[3] > -1 ? r(t[3] * 255) : f[3] > -1 ? r(f[3] * 255) : 255)).toString(16).slice(1, f[3] > -1 || t[3] > -1 ? undefined : -2);
    }

    let color1 = "rgb(153, 175, 255)";

    let newRGB = function numChange() {
    const convertedRGB = ;
    for (let i = 0; i < 100; i++) {
    let percentage = i / 100;
    c = pSBC(percentage, color1);
    convertedRGB.push(c);
    }
    return convertedRGB;
    }
    console.log(newRGB(100));

    c = pSBC(newRGB(100), color1); // rgb(20,60,200) + [42% Lighter] => rgb(119,142,223)


    function hot() {
    farenheitInput_input.style.background = 'red';
    celciusInput_input.style.background = 'red';
    kelvinInput_input.style.background = 'red';
    }

    function cold() {
    farenheitInput_input.style.background = '#89e3ff';
    celciusInput_input.style.background = '#89e3ff';
    kelvinInput_input.style.background = '#89e3ff';
    }

    function defaultColor() {
    farenheitInput_input.style.background = 'black';
    celciusInput_input.style.background = 'black';
    kelvinInput_input.style.background = 'black';
    }

    function converterCtoFK() {
    const cTemp = parseFloat(celciusInput_input.value);
    const fTemp = (cTemp * (9 / 5) + 32);
    const kTemp = (cTemp + 273.15);
    farenheitInput_input.value = (roundNum(fTemp));
    kelvinInput_input.value = roundNum(kTemp);
    if (cTemp >= 100) {
    hot();
    } else if (cTemp <= 0) {
    cold();
    } else {
    defaultColor();
    }
    }


    function converterFtoCK() {
    const fTemp = parseFloat(farenheitInput_input.value);
    const cTemp = ((fTemp - 32) * 5 / 9);
    const kTemp = ((fTemp + 459.67) * 5 / 9);
    celciusInput_input.value = roundNum(cTemp);
    kelvinInput_input.value = roundNum(kTemp);
    if (fTemp >= 212) {
    hot();
    } else if (32 > fTemp) {
    cold();
    } else {
    defaultColor();
    }
    }

    function converterKtoCF() {
    const kTemp = parseFloat(kelvinInput_input.value);
    const cTemp = (kTemp - 273.5);
    const fTemp = (9 / 5 * (kTemp - 273) + 32);
    celciusInput_input.value = roundNum(cTemp);
    farenheitInput_input.value = roundNum(fTemp);
    if (kTemp >= 373.15) {
    hot();
    } else if (273.15 > kTemp) {
    cold();
    } else {
    defaultColor();
    }

    }

    function main() {
    celciusInput_input.addEventListener('input', converterCtoFK);
    farenheitInput_input.addEventListener('input', converterFtoCK)
    kelvinInput_input.addEventListener('input', converterKtoCF)
    }

    main();

    * {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    }

    body {
    background: black;
    }
    div {
    height: 33.3vh;
    }

    #farenheit {
    border-top: 4px solid white;
    border-bottom: 4px solid white;
    }

    input[type=number] {
    width: 100%;
    height: 100%;
    background: black;
    outline: none;
    color: white;
    font-size: 10em;
    text-align: center;
    border: 0;
    font-family: avenir, sans-serif;
    }

    /*browser support for chrome/ safari & opera*/
    ::-webkit-input-placeholder {
    color: #222222;
    }

    h1{
    color:#ff0000;
    }

    <!DOCTYPE html>
    <html>

    <head>
    <title>Temperature Converter</title>
    <link rel="stylesheet" href="styles.css">
    </head>

    <body>
    <div class="wrap">
    <div id="celcius">
    <input type="number" placeholder="celcius...">
    </div>
    <div id="farenheit">
    <input type="number" placeholder="farenheit...">
    </div>
    <div id="kelvin">
    <input type="number" placeholder="kelvin...">
    </div>
    </div>
    <script src="app.js"></script>
    </body>

    </html>












    share|improve this question

























      0












      0








      0








      I have looked over: Programmatically Lighten or Darken a hex color (or rgb, and blend colors), and more specifically: https://github.com/PimpTrizkit/PJs/wiki/12.-Shade,-Blend-and-Convert-a-Web-Color-(pSBC.js) to try and implement a similar change in color to my program.



      The issue I seem to be having is that when I store the resulted rgb values from my newRGB function to a variable named c and use the value from c to implement on my cold function, the color change is not happening.



      I am able to store the convertedRGB array and see 100 rgb values in the console, but am unable to use those 100 values to replace the color value for my cold() function.



      The goal: when a user increases a value in the browser, the resulting background color of all three input boxes should either grow more red(increasing to full red, starting at black from the halfway point between 0 and 100 for celsius and the opposite when decreasing.



      Basically, from 50 to 100 C, the color background color should start at full black and grow to full red. And from 50 to 0 C, the color background should go from full black to full blue.



      Note: The 100 rgb values I computed in the console go from a blueish to white.






      const celciusInput_input = document.querySelector('#celcius > input');
      const farenheitInput_input = document.querySelector('#farenheit > input');
      const kelvinInput_input = document.querySelector('#kelvin > input');

      function roundNum(num) {
      return Math.round(num * 100) / 100; //round to 100th place (where num is any float number)
      }

      const pSBC = function (p, from, to) {
      if (typeof (p) != "number" || p < -1 || p > 1 || typeof (from) != "string" || (from[0] != 'r' && from[0] != '#') || (to && typeof (to) != "string")) return null; //ErrorCheck
      if (!this.pSBCr) this.pSBCr = (d) => {
      let l = d.length,
      RGB = {};
      if (l > 9) {
      d = d.split(",");
      if (d.length < 3 || d.length > 4) return null; //ErrorCheck
      RGB[0] = i(d[0].split("(")[1]), RGB[1] = i(d[1]), RGB[2] = i(d[2]), RGB[3] = d[3] ? parseFloat(d[3]) : -1;
      } else {
      if (l == 8 || l == 6 || l < 4) return null; //ErrorCheck
      if (l < 6) d = "#" + d[1] + d[1] + d[2] + d[2] + d[3] + d[3] + (l > 4 ? d[4] + "" + d[4] : ""); //3 or 4 digit
      d = i(d.slice(1), 16), RGB[0] = d >> 16 & 255, RGB[1] = d >> 8 & 255, RGB[2] = d & 255, RGB[3] = -1;
      if (l == 9 || l == 5) RGB[3] = r((RGB[2] / 255) * 10000) / 10000, RGB[2] = RGB[1], RGB[1] = RGB[0], RGB[0] = d >> 24 & 255;
      }
      return RGB;
      }
      var i = parseInt,
      r = Math.round,
      h = from.length > 9,
      h = typeof (to) == "string" ? to.length > 9 ? true : to == "c" ? !h : false : h,
      b = p < 0,
      p = b ? p * -1 : p,
      to = to && to != "c" ? to : b ? "#000000" : "#FFFFFF",
      f = this.pSBCr(from),
      t = this.pSBCr(to);
      if (!f || !t) return null; //ErrorCheck
      if (h) return "rgb" + (f[3] > -1 || t[3] > -1 ? "a(" : "(") + r((t[0] - f[0]) * p + f[0]) + "," + r((t[1] - f[1]) * p + f[1]) + "," + r((t[2] - f[2]) * p + f[2]) + (f[3] < 0 && t[3] < 0 ? ")" : "," + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 10000) / 10000 : t[3] < 0 ? f[3] : t[3]) + ")");
      else return "#" + (0x100000000 + r((t[0] - f[0]) * p + f[0]) * 0x1000000 + r((t[1] - f[1]) * p + f[1]) * 0x10000 + r((t[2] - f[2]) * p + f[2]) * 0x100 + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 255) : t[3] > -1 ? r(t[3] * 255) : f[3] > -1 ? r(f[3] * 255) : 255)).toString(16).slice(1, f[3] > -1 || t[3] > -1 ? undefined : -2);
      }

      let color1 = "rgb(153, 175, 255)";

      let newRGB = function numChange() {
      const convertedRGB = ;
      for (let i = 0; i < 100; i++) {
      let percentage = i / 100;
      c = pSBC(percentage, color1);
      convertedRGB.push(c);
      }
      return convertedRGB;
      }
      console.log(newRGB(100));

      c = pSBC(newRGB(100), color1); // rgb(20,60,200) + [42% Lighter] => rgb(119,142,223)


      function hot() {
      farenheitInput_input.style.background = 'red';
      celciusInput_input.style.background = 'red';
      kelvinInput_input.style.background = 'red';
      }

      function cold() {
      farenheitInput_input.style.background = '#89e3ff';
      celciusInput_input.style.background = '#89e3ff';
      kelvinInput_input.style.background = '#89e3ff';
      }

      function defaultColor() {
      farenheitInput_input.style.background = 'black';
      celciusInput_input.style.background = 'black';
      kelvinInput_input.style.background = 'black';
      }

      function converterCtoFK() {
      const cTemp = parseFloat(celciusInput_input.value);
      const fTemp = (cTemp * (9 / 5) + 32);
      const kTemp = (cTemp + 273.15);
      farenheitInput_input.value = (roundNum(fTemp));
      kelvinInput_input.value = roundNum(kTemp);
      if (cTemp >= 100) {
      hot();
      } else if (cTemp <= 0) {
      cold();
      } else {
      defaultColor();
      }
      }


      function converterFtoCK() {
      const fTemp = parseFloat(farenheitInput_input.value);
      const cTemp = ((fTemp - 32) * 5 / 9);
      const kTemp = ((fTemp + 459.67) * 5 / 9);
      celciusInput_input.value = roundNum(cTemp);
      kelvinInput_input.value = roundNum(kTemp);
      if (fTemp >= 212) {
      hot();
      } else if (32 > fTemp) {
      cold();
      } else {
      defaultColor();
      }
      }

      function converterKtoCF() {
      const kTemp = parseFloat(kelvinInput_input.value);
      const cTemp = (kTemp - 273.5);
      const fTemp = (9 / 5 * (kTemp - 273) + 32);
      celciusInput_input.value = roundNum(cTemp);
      farenheitInput_input.value = roundNum(fTemp);
      if (kTemp >= 373.15) {
      hot();
      } else if (273.15 > kTemp) {
      cold();
      } else {
      defaultColor();
      }

      }

      function main() {
      celciusInput_input.addEventListener('input', converterCtoFK);
      farenheitInput_input.addEventListener('input', converterFtoCK)
      kelvinInput_input.addEventListener('input', converterKtoCF)
      }

      main();

      * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
      }

      body {
      background: black;
      }
      div {
      height: 33.3vh;
      }

      #farenheit {
      border-top: 4px solid white;
      border-bottom: 4px solid white;
      }

      input[type=number] {
      width: 100%;
      height: 100%;
      background: black;
      outline: none;
      color: white;
      font-size: 10em;
      text-align: center;
      border: 0;
      font-family: avenir, sans-serif;
      }

      /*browser support for chrome/ safari & opera*/
      ::-webkit-input-placeholder {
      color: #222222;
      }

      h1{
      color:#ff0000;
      }

      <!DOCTYPE html>
      <html>

      <head>
      <title>Temperature Converter</title>
      <link rel="stylesheet" href="styles.css">
      </head>

      <body>
      <div class="wrap">
      <div id="celcius">
      <input type="number" placeholder="celcius...">
      </div>
      <div id="farenheit">
      <input type="number" placeholder="farenheit...">
      </div>
      <div id="kelvin">
      <input type="number" placeholder="kelvin...">
      </div>
      </div>
      <script src="app.js"></script>
      </body>

      </html>












      share|improve this question














      I have looked over: Programmatically Lighten or Darken a hex color (or rgb, and blend colors), and more specifically: https://github.com/PimpTrizkit/PJs/wiki/12.-Shade,-Blend-and-Convert-a-Web-Color-(pSBC.js) to try and implement a similar change in color to my program.



      The issue I seem to be having is that when I store the resulted rgb values from my newRGB function to a variable named c and use the value from c to implement on my cold function, the color change is not happening.



      I am able to store the convertedRGB array and see 100 rgb values in the console, but am unable to use those 100 values to replace the color value for my cold() function.



      The goal: when a user increases a value in the browser, the resulting background color of all three input boxes should either grow more red(increasing to full red, starting at black from the halfway point between 0 and 100 for celsius and the opposite when decreasing.



      Basically, from 50 to 100 C, the color background color should start at full black and grow to full red. And from 50 to 0 C, the color background should go from full black to full blue.



      Note: The 100 rgb values I computed in the console go from a blueish to white.






      const celciusInput_input = document.querySelector('#celcius > input');
      const farenheitInput_input = document.querySelector('#farenheit > input');
      const kelvinInput_input = document.querySelector('#kelvin > input');

      function roundNum(num) {
      return Math.round(num * 100) / 100; //round to 100th place (where num is any float number)
      }

      const pSBC = function (p, from, to) {
      if (typeof (p) != "number" || p < -1 || p > 1 || typeof (from) != "string" || (from[0] != 'r' && from[0] != '#') || (to && typeof (to) != "string")) return null; //ErrorCheck
      if (!this.pSBCr) this.pSBCr = (d) => {
      let l = d.length,
      RGB = {};
      if (l > 9) {
      d = d.split(",");
      if (d.length < 3 || d.length > 4) return null; //ErrorCheck
      RGB[0] = i(d[0].split("(")[1]), RGB[1] = i(d[1]), RGB[2] = i(d[2]), RGB[3] = d[3] ? parseFloat(d[3]) : -1;
      } else {
      if (l == 8 || l == 6 || l < 4) return null; //ErrorCheck
      if (l < 6) d = "#" + d[1] + d[1] + d[2] + d[2] + d[3] + d[3] + (l > 4 ? d[4] + "" + d[4] : ""); //3 or 4 digit
      d = i(d.slice(1), 16), RGB[0] = d >> 16 & 255, RGB[1] = d >> 8 & 255, RGB[2] = d & 255, RGB[3] = -1;
      if (l == 9 || l == 5) RGB[3] = r((RGB[2] / 255) * 10000) / 10000, RGB[2] = RGB[1], RGB[1] = RGB[0], RGB[0] = d >> 24 & 255;
      }
      return RGB;
      }
      var i = parseInt,
      r = Math.round,
      h = from.length > 9,
      h = typeof (to) == "string" ? to.length > 9 ? true : to == "c" ? !h : false : h,
      b = p < 0,
      p = b ? p * -1 : p,
      to = to && to != "c" ? to : b ? "#000000" : "#FFFFFF",
      f = this.pSBCr(from),
      t = this.pSBCr(to);
      if (!f || !t) return null; //ErrorCheck
      if (h) return "rgb" + (f[3] > -1 || t[3] > -1 ? "a(" : "(") + r((t[0] - f[0]) * p + f[0]) + "," + r((t[1] - f[1]) * p + f[1]) + "," + r((t[2] - f[2]) * p + f[2]) + (f[3] < 0 && t[3] < 0 ? ")" : "," + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 10000) / 10000 : t[3] < 0 ? f[3] : t[3]) + ")");
      else return "#" + (0x100000000 + r((t[0] - f[0]) * p + f[0]) * 0x1000000 + r((t[1] - f[1]) * p + f[1]) * 0x10000 + r((t[2] - f[2]) * p + f[2]) * 0x100 + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 255) : t[3] > -1 ? r(t[3] * 255) : f[3] > -1 ? r(f[3] * 255) : 255)).toString(16).slice(1, f[3] > -1 || t[3] > -1 ? undefined : -2);
      }

      let color1 = "rgb(153, 175, 255)";

      let newRGB = function numChange() {
      const convertedRGB = ;
      for (let i = 0; i < 100; i++) {
      let percentage = i / 100;
      c = pSBC(percentage, color1);
      convertedRGB.push(c);
      }
      return convertedRGB;
      }
      console.log(newRGB(100));

      c = pSBC(newRGB(100), color1); // rgb(20,60,200) + [42% Lighter] => rgb(119,142,223)


      function hot() {
      farenheitInput_input.style.background = 'red';
      celciusInput_input.style.background = 'red';
      kelvinInput_input.style.background = 'red';
      }

      function cold() {
      farenheitInput_input.style.background = '#89e3ff';
      celciusInput_input.style.background = '#89e3ff';
      kelvinInput_input.style.background = '#89e3ff';
      }

      function defaultColor() {
      farenheitInput_input.style.background = 'black';
      celciusInput_input.style.background = 'black';
      kelvinInput_input.style.background = 'black';
      }

      function converterCtoFK() {
      const cTemp = parseFloat(celciusInput_input.value);
      const fTemp = (cTemp * (9 / 5) + 32);
      const kTemp = (cTemp + 273.15);
      farenheitInput_input.value = (roundNum(fTemp));
      kelvinInput_input.value = roundNum(kTemp);
      if (cTemp >= 100) {
      hot();
      } else if (cTemp <= 0) {
      cold();
      } else {
      defaultColor();
      }
      }


      function converterFtoCK() {
      const fTemp = parseFloat(farenheitInput_input.value);
      const cTemp = ((fTemp - 32) * 5 / 9);
      const kTemp = ((fTemp + 459.67) * 5 / 9);
      celciusInput_input.value = roundNum(cTemp);
      kelvinInput_input.value = roundNum(kTemp);
      if (fTemp >= 212) {
      hot();
      } else if (32 > fTemp) {
      cold();
      } else {
      defaultColor();
      }
      }

      function converterKtoCF() {
      const kTemp = parseFloat(kelvinInput_input.value);
      const cTemp = (kTemp - 273.5);
      const fTemp = (9 / 5 * (kTemp - 273) + 32);
      celciusInput_input.value = roundNum(cTemp);
      farenheitInput_input.value = roundNum(fTemp);
      if (kTemp >= 373.15) {
      hot();
      } else if (273.15 > kTemp) {
      cold();
      } else {
      defaultColor();
      }

      }

      function main() {
      celciusInput_input.addEventListener('input', converterCtoFK);
      farenheitInput_input.addEventListener('input', converterFtoCK)
      kelvinInput_input.addEventListener('input', converterKtoCF)
      }

      main();

      * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
      }

      body {
      background: black;
      }
      div {
      height: 33.3vh;
      }

      #farenheit {
      border-top: 4px solid white;
      border-bottom: 4px solid white;
      }

      input[type=number] {
      width: 100%;
      height: 100%;
      background: black;
      outline: none;
      color: white;
      font-size: 10em;
      text-align: center;
      border: 0;
      font-family: avenir, sans-serif;
      }

      /*browser support for chrome/ safari & opera*/
      ::-webkit-input-placeholder {
      color: #222222;
      }

      h1{
      color:#ff0000;
      }

      <!DOCTYPE html>
      <html>

      <head>
      <title>Temperature Converter</title>
      <link rel="stylesheet" href="styles.css">
      </head>

      <body>
      <div class="wrap">
      <div id="celcius">
      <input type="number" placeholder="celcius...">
      </div>
      <div id="farenheit">
      <input type="number" placeholder="farenheit...">
      </div>
      <div id="kelvin">
      <input type="number" placeholder="kelvin...">
      </div>
      </div>
      <script src="app.js"></script>
      </body>

      </html>








      const celciusInput_input = document.querySelector('#celcius > input');
      const farenheitInput_input = document.querySelector('#farenheit > input');
      const kelvinInput_input = document.querySelector('#kelvin > input');

      function roundNum(num) {
      return Math.round(num * 100) / 100; //round to 100th place (where num is any float number)
      }

      const pSBC = function (p, from, to) {
      if (typeof (p) != "number" || p < -1 || p > 1 || typeof (from) != "string" || (from[0] != 'r' && from[0] != '#') || (to && typeof (to) != "string")) return null; //ErrorCheck
      if (!this.pSBCr) this.pSBCr = (d) => {
      let l = d.length,
      RGB = {};
      if (l > 9) {
      d = d.split(",");
      if (d.length < 3 || d.length > 4) return null; //ErrorCheck
      RGB[0] = i(d[0].split("(")[1]), RGB[1] = i(d[1]), RGB[2] = i(d[2]), RGB[3] = d[3] ? parseFloat(d[3]) : -1;
      } else {
      if (l == 8 || l == 6 || l < 4) return null; //ErrorCheck
      if (l < 6) d = "#" + d[1] + d[1] + d[2] + d[2] + d[3] + d[3] + (l > 4 ? d[4] + "" + d[4] : ""); //3 or 4 digit
      d = i(d.slice(1), 16), RGB[0] = d >> 16 & 255, RGB[1] = d >> 8 & 255, RGB[2] = d & 255, RGB[3] = -1;
      if (l == 9 || l == 5) RGB[3] = r((RGB[2] / 255) * 10000) / 10000, RGB[2] = RGB[1], RGB[1] = RGB[0], RGB[0] = d >> 24 & 255;
      }
      return RGB;
      }
      var i = parseInt,
      r = Math.round,
      h = from.length > 9,
      h = typeof (to) == "string" ? to.length > 9 ? true : to == "c" ? !h : false : h,
      b = p < 0,
      p = b ? p * -1 : p,
      to = to && to != "c" ? to : b ? "#000000" : "#FFFFFF",
      f = this.pSBCr(from),
      t = this.pSBCr(to);
      if (!f || !t) return null; //ErrorCheck
      if (h) return "rgb" + (f[3] > -1 || t[3] > -1 ? "a(" : "(") + r((t[0] - f[0]) * p + f[0]) + "," + r((t[1] - f[1]) * p + f[1]) + "," + r((t[2] - f[2]) * p + f[2]) + (f[3] < 0 && t[3] < 0 ? ")" : "," + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 10000) / 10000 : t[3] < 0 ? f[3] : t[3]) + ")");
      else return "#" + (0x100000000 + r((t[0] - f[0]) * p + f[0]) * 0x1000000 + r((t[1] - f[1]) * p + f[1]) * 0x10000 + r((t[2] - f[2]) * p + f[2]) * 0x100 + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 255) : t[3] > -1 ? r(t[3] * 255) : f[3] > -1 ? r(f[3] * 255) : 255)).toString(16).slice(1, f[3] > -1 || t[3] > -1 ? undefined : -2);
      }

      let color1 = "rgb(153, 175, 255)";

      let newRGB = function numChange() {
      const convertedRGB = ;
      for (let i = 0; i < 100; i++) {
      let percentage = i / 100;
      c = pSBC(percentage, color1);
      convertedRGB.push(c);
      }
      return convertedRGB;
      }
      console.log(newRGB(100));

      c = pSBC(newRGB(100), color1); // rgb(20,60,200) + [42% Lighter] => rgb(119,142,223)


      function hot() {
      farenheitInput_input.style.background = 'red';
      celciusInput_input.style.background = 'red';
      kelvinInput_input.style.background = 'red';
      }

      function cold() {
      farenheitInput_input.style.background = '#89e3ff';
      celciusInput_input.style.background = '#89e3ff';
      kelvinInput_input.style.background = '#89e3ff';
      }

      function defaultColor() {
      farenheitInput_input.style.background = 'black';
      celciusInput_input.style.background = 'black';
      kelvinInput_input.style.background = 'black';
      }

      function converterCtoFK() {
      const cTemp = parseFloat(celciusInput_input.value);
      const fTemp = (cTemp * (9 / 5) + 32);
      const kTemp = (cTemp + 273.15);
      farenheitInput_input.value = (roundNum(fTemp));
      kelvinInput_input.value = roundNum(kTemp);
      if (cTemp >= 100) {
      hot();
      } else if (cTemp <= 0) {
      cold();
      } else {
      defaultColor();
      }
      }


      function converterFtoCK() {
      const fTemp = parseFloat(farenheitInput_input.value);
      const cTemp = ((fTemp - 32) * 5 / 9);
      const kTemp = ((fTemp + 459.67) * 5 / 9);
      celciusInput_input.value = roundNum(cTemp);
      kelvinInput_input.value = roundNum(kTemp);
      if (fTemp >= 212) {
      hot();
      } else if (32 > fTemp) {
      cold();
      } else {
      defaultColor();
      }
      }

      function converterKtoCF() {
      const kTemp = parseFloat(kelvinInput_input.value);
      const cTemp = (kTemp - 273.5);
      const fTemp = (9 / 5 * (kTemp - 273) + 32);
      celciusInput_input.value = roundNum(cTemp);
      farenheitInput_input.value = roundNum(fTemp);
      if (kTemp >= 373.15) {
      hot();
      } else if (273.15 > kTemp) {
      cold();
      } else {
      defaultColor();
      }

      }

      function main() {
      celciusInput_input.addEventListener('input', converterCtoFK);
      farenheitInput_input.addEventListener('input', converterFtoCK)
      kelvinInput_input.addEventListener('input', converterKtoCF)
      }

      main();

      * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
      }

      body {
      background: black;
      }
      div {
      height: 33.3vh;
      }

      #farenheit {
      border-top: 4px solid white;
      border-bottom: 4px solid white;
      }

      input[type=number] {
      width: 100%;
      height: 100%;
      background: black;
      outline: none;
      color: white;
      font-size: 10em;
      text-align: center;
      border: 0;
      font-family: avenir, sans-serif;
      }

      /*browser support for chrome/ safari & opera*/
      ::-webkit-input-placeholder {
      color: #222222;
      }

      h1{
      color:#ff0000;
      }

      <!DOCTYPE html>
      <html>

      <head>
      <title>Temperature Converter</title>
      <link rel="stylesheet" href="styles.css">
      </head>

      <body>
      <div class="wrap">
      <div id="celcius">
      <input type="number" placeholder="celcius...">
      </div>
      <div id="farenheit">
      <input type="number" placeholder="farenheit...">
      </div>
      <div id="kelvin">
      <input type="number" placeholder="kelvin...">
      </div>
      </div>
      <script src="app.js"></script>
      </body>

      </html>





      const celciusInput_input = document.querySelector('#celcius > input');
      const farenheitInput_input = document.querySelector('#farenheit > input');
      const kelvinInput_input = document.querySelector('#kelvin > input');

      function roundNum(num) {
      return Math.round(num * 100) / 100; //round to 100th place (where num is any float number)
      }

      const pSBC = function (p, from, to) {
      if (typeof (p) != "number" || p < -1 || p > 1 || typeof (from) != "string" || (from[0] != 'r' && from[0] != '#') || (to && typeof (to) != "string")) return null; //ErrorCheck
      if (!this.pSBCr) this.pSBCr = (d) => {
      let l = d.length,
      RGB = {};
      if (l > 9) {
      d = d.split(",");
      if (d.length < 3 || d.length > 4) return null; //ErrorCheck
      RGB[0] = i(d[0].split("(")[1]), RGB[1] = i(d[1]), RGB[2] = i(d[2]), RGB[3] = d[3] ? parseFloat(d[3]) : -1;
      } else {
      if (l == 8 || l == 6 || l < 4) return null; //ErrorCheck
      if (l < 6) d = "#" + d[1] + d[1] + d[2] + d[2] + d[3] + d[3] + (l > 4 ? d[4] + "" + d[4] : ""); //3 or 4 digit
      d = i(d.slice(1), 16), RGB[0] = d >> 16 & 255, RGB[1] = d >> 8 & 255, RGB[2] = d & 255, RGB[3] = -1;
      if (l == 9 || l == 5) RGB[3] = r((RGB[2] / 255) * 10000) / 10000, RGB[2] = RGB[1], RGB[1] = RGB[0], RGB[0] = d >> 24 & 255;
      }
      return RGB;
      }
      var i = parseInt,
      r = Math.round,
      h = from.length > 9,
      h = typeof (to) == "string" ? to.length > 9 ? true : to == "c" ? !h : false : h,
      b = p < 0,
      p = b ? p * -1 : p,
      to = to && to != "c" ? to : b ? "#000000" : "#FFFFFF",
      f = this.pSBCr(from),
      t = this.pSBCr(to);
      if (!f || !t) return null; //ErrorCheck
      if (h) return "rgb" + (f[3] > -1 || t[3] > -1 ? "a(" : "(") + r((t[0] - f[0]) * p + f[0]) + "," + r((t[1] - f[1]) * p + f[1]) + "," + r((t[2] - f[2]) * p + f[2]) + (f[3] < 0 && t[3] < 0 ? ")" : "," + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 10000) / 10000 : t[3] < 0 ? f[3] : t[3]) + ")");
      else return "#" + (0x100000000 + r((t[0] - f[0]) * p + f[0]) * 0x1000000 + r((t[1] - f[1]) * p + f[1]) * 0x10000 + r((t[2] - f[2]) * p + f[2]) * 0x100 + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 255) : t[3] > -1 ? r(t[3] * 255) : f[3] > -1 ? r(f[3] * 255) : 255)).toString(16).slice(1, f[3] > -1 || t[3] > -1 ? undefined : -2);
      }

      let color1 = "rgb(153, 175, 255)";

      let newRGB = function numChange() {
      const convertedRGB = ;
      for (let i = 0; i < 100; i++) {
      let percentage = i / 100;
      c = pSBC(percentage, color1);
      convertedRGB.push(c);
      }
      return convertedRGB;
      }
      console.log(newRGB(100));

      c = pSBC(newRGB(100), color1); // rgb(20,60,200) + [42% Lighter] => rgb(119,142,223)


      function hot() {
      farenheitInput_input.style.background = 'red';
      celciusInput_input.style.background = 'red';
      kelvinInput_input.style.background = 'red';
      }

      function cold() {
      farenheitInput_input.style.background = '#89e3ff';
      celciusInput_input.style.background = '#89e3ff';
      kelvinInput_input.style.background = '#89e3ff';
      }

      function defaultColor() {
      farenheitInput_input.style.background = 'black';
      celciusInput_input.style.background = 'black';
      kelvinInput_input.style.background = 'black';
      }

      function converterCtoFK() {
      const cTemp = parseFloat(celciusInput_input.value);
      const fTemp = (cTemp * (9 / 5) + 32);
      const kTemp = (cTemp + 273.15);
      farenheitInput_input.value = (roundNum(fTemp));
      kelvinInput_input.value = roundNum(kTemp);
      if (cTemp >= 100) {
      hot();
      } else if (cTemp <= 0) {
      cold();
      } else {
      defaultColor();
      }
      }


      function converterFtoCK() {
      const fTemp = parseFloat(farenheitInput_input.value);
      const cTemp = ((fTemp - 32) * 5 / 9);
      const kTemp = ((fTemp + 459.67) * 5 / 9);
      celciusInput_input.value = roundNum(cTemp);
      kelvinInput_input.value = roundNum(kTemp);
      if (fTemp >= 212) {
      hot();
      } else if (32 > fTemp) {
      cold();
      } else {
      defaultColor();
      }
      }

      function converterKtoCF() {
      const kTemp = parseFloat(kelvinInput_input.value);
      const cTemp = (kTemp - 273.5);
      const fTemp = (9 / 5 * (kTemp - 273) + 32);
      celciusInput_input.value = roundNum(cTemp);
      farenheitInput_input.value = roundNum(fTemp);
      if (kTemp >= 373.15) {
      hot();
      } else if (273.15 > kTemp) {
      cold();
      } else {
      defaultColor();
      }

      }

      function main() {
      celciusInput_input.addEventListener('input', converterCtoFK);
      farenheitInput_input.addEventListener('input', converterFtoCK)
      kelvinInput_input.addEventListener('input', converterKtoCF)
      }

      main();

      * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
      }

      body {
      background: black;
      }
      div {
      height: 33.3vh;
      }

      #farenheit {
      border-top: 4px solid white;
      border-bottom: 4px solid white;
      }

      input[type=number] {
      width: 100%;
      height: 100%;
      background: black;
      outline: none;
      color: white;
      font-size: 10em;
      text-align: center;
      border: 0;
      font-family: avenir, sans-serif;
      }

      /*browser support for chrome/ safari & opera*/
      ::-webkit-input-placeholder {
      color: #222222;
      }

      h1{
      color:#ff0000;
      }

      <!DOCTYPE html>
      <html>

      <head>
      <title>Temperature Converter</title>
      <link rel="stylesheet" href="styles.css">
      </head>

      <body>
      <div class="wrap">
      <div id="celcius">
      <input type="number" placeholder="celcius...">
      </div>
      <div id="farenheit">
      <input type="number" placeholder="farenheit...">
      </div>
      <div id="kelvin">
      <input type="number" placeholder="kelvin...">
      </div>
      </div>
      <script src="app.js"></script>
      </body>

      </html>






      javascript html css dynamic colors






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 6:48









      Tyler MoralesTyler Morales

      898




      898
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993558%2flinking-a-dynamic-hex-color-change-to-a-html-background-style%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993558%2flinking-a-dynamic-hex-color-change-to-a-html-background-style%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas