JavaScript RGB to HEX Converter

02/13/2021

Contents

Demo

Full Screen

Code

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>RGB to HEX Converter</title>
    <link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
    <link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <div id="container">
      <div class="box">
        <div class="rgb">
          <div>
            <label for="red">R</label>
            <input id="red" type="number" min="0" max="255" value="0">
          </div>
          <div>
            <label for="green">G</label>
            <input id="green" type="number" min="0" max="255" value="0">
          </div>
          <div>
            <label for="blue">B</label>
            <input id="blue" type="number" min="0" max="255" value="0">
          </div>
        </div>
        <button id="convert" class="button">Convert to Hex</button>
        <div class="color-code">#<span id="hex"></span></div>
      </div>
    </div>
    <script src="converter.js"></script>
  </body>
</html>

CSS

@charset "utf-8";
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
html {
  font-size: 16px;
}
body {
  background-color: #e7e7e7;
  font-family: futura-pt, sans-serif;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}
#container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100vh;
  padding: 10px;
}
.box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 280px;
}
.rgb {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  color: #3d3935;
  font-size: 1.25rem;
}
input {
  width: 55px;
  height: 30px;
}
.button {
  width: 100%;
  margin: 20px 0;
  padding: 10px;
  border: none;
  border-radius: 4px;
  background-color: #000;
  color: #fff;
  font-size: 1.25rem;
  outline: none;
  cursor: pointer;
}
.color-code {
  width: 100%;
  border-bottom: solid 1px #3d3935;
  color: #3d3935;
  font-weight: bold;
  font-size: 2rem;
  text-align: center;
}

JavaScript

var redEl = document.getElementById('red');
var greenEl = document.getElementById('green');
var blueEl = document.getElementById('blue');
var convert = document.getElementById('convert');
var hex = document.getElementById('hex');

convert.addEventListener('click', () => {
  var redVal = redEl.value;
  var greenVal = greenEl.value;
  var blueVal = blueEl.value;

  if(redVal.length > 3 || greenVal.length > 3 || blueVal.length > 3) {
    alert('this RGB is invalid.');
  } else {
    var r = ("0" + parseInt(redVal).toString(16)).slice(-2);
    var g = ("0" + parseInt(greenVal).toString(16)).slice(-2);
    var b = ("0" + parseInt(blueVal).toString(16)).slice(-2);
    hex.innerHTML = r + g + b;
  }
});