Convert RGB to HEX using HTML, CSS & JavaScript

Convert RGB to HEX using HTML, CSS & JavaScript

Convert RGB to HEX using HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make a convertor which will convert the value from RGB(red green blue) to HEX Code and provide the accurate value for the color in different shade. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make RGB To Hex / Hex To RGB.

Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the RGB To Hex / Hex To RGB project.

At last we will use JS (JavaScript) which will add a logic to make the RGB To Hex / Hex To RGB project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for RGB to HEX

<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RGB-Hex Converter</title>
    <!--Google Font-->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="wrapper">
            <label for="rgb">RGB</label>
            <input type="text" id="rgb" oninput="toHex()" placeholder="Enter RGB Value">
        </div>
        <div class="wrapper">
            <label for="hex">HEX</label>
            <input type="text" id="hex" oninput="toRgb()" placeholder="Enter Hex Value">
        </div>
    </div>
    <!--Script-->
    <script src="script.js"></script>
</body>
</html>

First we’ll start with creating the structure of the project for that as you can see the above code we have used all the necessary elements & attributes. Let us know code the CSS part to add styling and aligned the tags.

50+ Html ,Css & Javascript Projects With Source Code

CSS Code for RGB to HEX

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: "Poppins",sans-serif;
}
body{
    width: 100%;
    background-color: #976efa;
}
.container{
    background-color: #ffffff;
    width: 80vmin;
    min-width: 250px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 50px;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    padding: 50px 10px;
    border-radius: 5px;
}
.wrapper{
    width: 230px;
}
label{
    font-weight: 600;
}
input{
    width: 100%;
    padding: 5px 0;
    outline: none;
    border: none;
    border-bottom: 2px solid #404050;
    font-size: 20px;
    font-weight: 400;
    margin-top: 5px;
}

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

Portfolio Website using HTML and CSS (Source Code)

JavaScript Code for RGB to HEX

let hexInput = document.getElementById("hex");
let rgbInput = document.getElementById("rgb");

window.onload=()=>{
    hexInput.value = "";
    rgbInput.value = "";
}

function valid(element){
    element.style.color = "#202040";
}

function invalid(element,otherElement){
    element.style.color = "#f04624";
    otherElement.value = 0;
}

function toRgb(){
    let hexCode = hexInput.value;
    let rgbArr = [];
    if(/^#?[A-Fa-f0-9]{6}$/.test(hexCode)){
        valid(hexInput);
        hexCode = hexCode.split("#")[1] || hexCode;
        for(let i=0; i<hexCode.length;i+=2){
            rgbArr.push(parseInt(hexCode[i] + hexCode[i+1], 16));
            console.log(rgbArr);
        }
        rgbInput.value = "rgb(" + rgbArr + ")";
        document.body.style.backgroundColor = "rgb(" + rgbArr + ")";
    }
    else{
        invalid(hexInput,rgbInput);
    }
}

function toHex(){
    let rgbCode = rgbInput.value;
    let rgbRegex1 = /^rgb\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)$/;
    let rgbRegex2 = /^[0-9]{1,3},[0-9]{1,3},[0-9]{1,3}$/
    let hex = "#";
    if(rgbRegex1.test(rgbCode) || rgbRegex2.test(rgbCode)){
        rgbCode = rgbCode.replace(/[rgb()]+/g,"") || rgbCode;
        rgbCode = rgbCode.split(",");
        let condition = rgbCode.every((value) => {
            return parseInt(value) <= 255;
        });
        if(condition){
            valid(rgbInput);
            rgbCode.forEach(value => {
                value = parseInt(value).toString(16);
                hex += value.length == 1? "0"+value : value;
            });
            hexInput.value = hex;
            document.body.style.backgroundColor = hex;
        }
        else{
            invalid(rgbInput,hexInput);
        }
    }
    else{
        invalid(rgbInput,hexInput);
    }

}

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project RGB to HEX Convertor using HTML, CSS & JavaScript (Source Code).

Final Output Convert RGB to HEX using HTML, CSS & JavaScript

We have Successfully created our RGB to HEX Convertor using HTML, CSS & JavaScript (Source Code) | RGB To Hex / Hex To RGB. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to Search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply