JavaScript Color Converter Script

JavaScript Color Converter Script, Hex color converter RGB color converter HSL color converter, HEXT to RGB HEXT to HSL RGB to HEX

Hex Converter
1

Color Hex To RGB Converter

JavaScript
function HEXtoRGB(hex) {
    hex = hex.replace(/#/g, '');
    if (hex.length === 3) {
        hex = hex.split('').map(function (hex) {
            return hex + hex;
        }).join('');
    }
	// validate hex format
    var result = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})[\da-z]{0,0}$/i.exec(hex);
    if (result) {
        var red = parseInt(result[1], 16);
        var green = parseInt(result[2], 16);
        var blue = parseInt(result[3], 16);

        return [red, green, blue];
    } else {
	    // invalid color
        return null;
    }
}
Usage
HTML
<h1 id="preview-color"></h1>

<input type="text" id="hex-color" value="#076591">
<button id="convert-hex-to-rgb">Convert</button>


<script>
// support hex format (#f00, #ff0000, f00, ff0000)
document.getElementById("convert-hex-to-rgb").onclick = function(){
  let hex_color = document.getElementById("hex-color").value;
  var preview_color = document.getElementById("preview-color");
  let result = HEXtoRGB(hex_color );

  if(!result){
    preview_color.innerHTML= 'invalid color';
    preview_color.style.backgroundColor = '';
    preview_color.style.color = 'red';
    return;
  }

  /*
  var red = result[0];
  var green = result[1];
  var blue = result[2];

  let rgb = 'rgb('+red+','+green+','+blue+')';
  */

  // shorthand result
  let rgb = 'rgb('+result.join(', ')+')';
  preview_color.innerHTML= rgb;
  preview_color.style.backgroundColor = rgb;
};
</script>
2

Color Hex To HSL Converter

3

Color Hex To RGBA Converter

RGB Converter
4

Color RBG To Hex Converter

5

Color R B G To Hex Converter

6

Color RBG To HSL Converter

7

Color R B G To HSL Converter

8

Color RBGA To HSLA Converter

9

Color R B G A To HSLA Converter

HSL Converter
10

Color H S L to Hex Converter

11

Color HSL to Hex Converter

12

Color HSL to RGB Converter

13

Color HSL(A) to RGBA Converter