<div class="wrapper">
  <div class="element">
    <h2>πŸ’ƒπŸ»πŸ’ƒπŸ»RGB πŸ‘ˆ πŸ‘‰ HSL πŸ•ΊπŸ» πŸ•ΊπŸ»</h2>
    <div class="form">
      <div class="control">
        <label for="rgb">RGB:</label>
        <input type="text" id="rgb" name="rgb" value="rgb(120 144 250)" />
        <span class="help" id="rgbError">βœ…πŸ‘πŸ½</span>
      </div>
    </div>
  </div>
</div>
@import url("https://fonts.googleapis.com/css?family=Gochi+Hand");

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

body {
  margin: 0;
  padding: 0;
  background-color: #291642;
  font-family: "Gochi Hand", sans-serif;
  color: #fff;
  font-size: 130%;
  letter-spacing: 0.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 100vw;
  min-height: 100vh;
  padding: 10px;
}

.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  margin: 3vh 0;
}

.element {
  padding: 4vh;
  min-width: 50vw;
  min-height: 30vh;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  border-radius: 8px;
  font-size: 3rem;
  color: #a3afaa;
}

h2 {
  margin: 4vh;
  font-size: 2rem;
  color: #fff;
  text-shadow: 2px 2px 1px var(--color);
}

.form {
  display: flex;
  justify-content: center;
  font-size: 1.5rem;
}

.control {
  display: grid;
  gap: 1vh;
  grid-template-columns: auto 1fr auto;
}

span,
label {
  align-self: center;
  color: #fff;
  text-shadow: 2px 2px 1px var(--color);
}

input {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 3px;
  width: 100%;
  color: #2c3e50;
  font-family: "Gochi Hand", sans-serif;
}

.error input {
  border-color: red;
  color: red;
}

:root {
  --color: #7890fa;
}

body {
  background-color: var(--color);
  transition: all 0.2s ease;
}

.wrapper {
  background-image: radial-gradient(circle at center, var(--color), #291642),
    linear-gradient(to bottom, #291642, #291642);
  background-repeat: no-repeat;
  background-position: center;
  background-clip: padding-box;
  border-radius: 8px;
  background-blend-mode: multiply, screen;
}
View Compiled
/**
 * Check the RGB Function
 * @param {string} rgb - <rgb-color>, eg, rgb(r g b) | rgb(r g b / a) | rgb(r, g, b, a) | rgba(r g b / a)
 */

function checkRgb(rgb) {
  const rgbRegex = /^rgb\((((((((1?[1-9]?\d)|10\d|(2[0-4]\d)|25[0-5]),\s?)){2}|((((1?[1-9]?\d)|10\d|(2[0-4]\d)|25[0-5])\s)){2})((1?[1-9]?\d)|10\d|(2[0-4]\d)|25[0-5]))|((((([1-9]?\d(\.\d+)?)|100|(\.\d+))%,\s?){2}|((([1-9]?\d(\.\d+)?)|100|(\.\d+))%\s){2})(([1-9]?\d(\.\d+)?)|100|(\.\d+))%))\)$/i;

  if (rgbRegex.test(rgb)) {
    return true;
  }
}

const RGBToLightness = (r, g, b) => {
  r = r / 255;
  g = g / 255;
  b = b / 255;

  let M = Math.max(r, g, b);
  let m = Math.min(r, g, b);

  return `${Math.round(((M + m) / 2) * 100)}%`;
};

const RGBToSaturation = (r, g, b) => {
  r = r / 255;
  g = g / 255;
  b = b / 255;

  let M = Math.max(r, g, b);
  let m = Math.min(r, g, b);
  let delta = M - m;
  let l = (M + m) / 2;

  return delta == 0
    ? `0%`
    : `${Math.round((delta / (1 - Math.abs(2 * l - 1))) * 100)}%`;
};

const RGBToHue = (r, g, b) => {
  r = r / 255;
  g = g / 255;
  b = b / 255;

  let h = Math.round(
    (Math.atan2(Math.sqrt(3) * (g - b), 2 * r - g - b) * 180) / Math.PI
  );
  if (h < 0) {
    return h + 360;
  } else {
    return h;
  }
};

const RGBToHSL = (rgb) => {
  let sep = rgb.indexOf(",") > -1 ? "," : " ";
  rgb = rgb.substr(4).split(")")[0].split(sep);

  for (let R in rgb) {
    let r = rgb[R];

    if (r.indexOf("%") > -1) {
      rgb[R] = Math.round((r.substr(0, r.length - 1) / 100) * 255);
    }
  }

  let r = rgb[0];
  let g = rgb[1];
  let b = rgb[2];

  const h = RGBToHue(r, g, b);
  const s = RGBToSaturation(r, g, b);
  const l = RGBToLightness(r, g, b);

  return `hsl(${h}, ${s}, ${l})`;
};

const rgb = document.getElementById("rgb");

rgb.addEventListener("change", function () {
  let color = rgb.value;
  if (checkRgb(color)) {
    document.querySelector("h2").textContent = `πŸ’ƒπŸ»πŸ’ƒ ${color} ❯❯❯ ${RGBToHSL(
      color
    )} πŸ•ΊπŸ» πŸ•Ί`;
    document.documentElement.style.setProperty("--color", color);
    rgb.parentNode.classList.remove("error");
  } else {
    rgb.parentNode.classList.add("error");
    document.getElementById("rgbError").textContent = "βŽπŸ‘ŽπŸ½";
  }
});
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.