Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>HEX to RGB or RGB to HEX</h1>
<h2>conversion tool</h2>
<div class="form-container">
  <form onsubmit="convertColor();return false;">
    <label for="inputColor">Enter color (<span class="tooltip">HEX<span class="tooltip-icon">?</span><span class="tooltiptext">
          <strong>Examples of HEX format:</strong><br>
          #ffffff;<br>
          #ffffff<br>
          fff<br>
          FFF<br>
        </span></span> or
      <span class="tooltip">RGB<span class="tooltip-icon">?</span><span class="tooltiptext">
          <strong>Examples of RGB format:</strong><br>
          0,0,0<br>
          rgb(0, 0, 0);<br>
          rgb(0,0,0)<br>
          RGB(0,0,0)<br>
        </span></span>):
    </label>
    <div class="input-container">
      <input type="text" id="inputColor" />
      <button type="button" onclick="convertColor()">Convert</button>
    </div>
  </form>
</div>

<div class="result-container">
  <div id="result">
    <div class="result-color-out"></div>
    <div class="result-color-in"></div>
  </div>
</div>

<footer>
  Created by <a href="https://www.otik.cz/" target="_blank">Otík</a>, you can also find the source files on <a href="https://codepen.io/OtiKcz/pen/zYyZOjg" target="_blank">Codepen</a>.
</footer>
              
            
!

CSS

              
                :root {
            --color-text-out: #000000;
            --color-text-in: #FFFFFF;
            --color-solved: #455A64;
        }

        .form-container {
            margin: 20px auto;
            width: 300px;
        }

        .form-container label {
            font-size: 18px;
            text-align: left;
            letter-spacing: 2px;
        }

        .form-container input,
        .form-container button {
            padding: 10px;
            font-size: 16px;
            border-radius: 5px;
        }

        .form-container button {
            margin-top: 10px;
            background-color: var(--color-solved);
            color: var(--color-text-in);
            border: none;
            cursor: pointer;
        }

        .result-container {
            text-align: center;
            margin: 20px auto;
            width: 300px;
        }

        #result {
            font-size: 20px;
            border: 1px solid #ccc;
            padding: 10px;
            border-radius: 5px;
        }

        .result-color-out {
            color: var(--color-text-out);
        }

        .result-color-in {
            border: 1px solid #dddddd;
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            border-radius: 0 0 5px 5px;
            margin-top: 5px;
            background: var(--color-solved);
            color: var(--color-text-in);
        }

        /* unimportant style: */
        body {
            font-family: Arial, sans-serif;
        }

        h1 {
            font-size: 3.5em;
            text-align: center;
            margin-bottom: 0;
        }

        h2 {
            font-size: 2.5em;
            text-align: center;
            margin-top: 0;
            letter-spacing: 1px;
        }

        .tooltip {
            position: relative;
            cursor: help;
        }

        .tooltip-icon {
            display: inline-block;
            width: 10px;
            height: 10px;
            background-color: #f9f9f9;
            color: #333;
            text-align: center;
            line-height: 10px;
            border-radius: 50%;
            border: 1px solid #ccc;
            font-size: 10px;
            vertical-align: top;
        }

        .tooltiptext {
            visibility: hidden;
            width: 200px;
            background-color: #f9f9f9;
            color: #333;
            font-size: 0.9em;
            text-align: left;
            padding: 15px;
            border-radius: 5px;
            border: 1px solid #ccc;
            box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.2);
            position: absolute;
            z-index: 1;
            bottom: 125%;
            left: 0;
            opacity: 0;
            transition: opacity 0.3s;
        }

        .tooltiptext strong {
            display: inline-block;
            margin-bottom: 7px;
        }

        .tooltip:hover .tooltiptext {
            visibility: visible;
            opacity: 1;
        }

        footer {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            background-color: var(--color-solved);
            color: var(--color-text-in);
            text-align: center;
            padding: 10px;
            border-top: 1px solid var(--color-text-in);
        }

        footer a {
            color: var(--color-text-in);
            text-decoration: underline;
        }
              
            
!

JS

              
                function resolveConvertColor(inputColor) {
            // Function for converting a short hex to a full hex
            function shortHexToFullHex(shortHex) {
                return shortHex
                    .split("")
                    .map((char) => char + char)
                    .join("");
            }

            // Function for converting hex to RGB
            function hexToRgb(hex) {
                const r = parseInt(hex.substring(0, 2), 16);
                const g = parseInt(hex.substring(2, 4), 16);
                const b = parseInt(hex.substring(4, 6), 16);
                return {r, g, b};
            }

            // Function for converting RGB to hex
            function rgbToHex(rgb) {
                const rHex = rgb.r.toString(16).padStart(2, "0");
                const gHex = rgb.g.toString(16).padStart(2, "0");
                const bHex = rgb.b.toString(16).padStart(2, "0");
                return (rHex + gHex + bHex).toUpperCase();
            }

            // Function for checking if a color is bright
            function isBrightColor(r, g, b) {
                const brightness = (r * 299 + g * 587 + b * 114) / 1000;
                return brightness > 128;
            }

            // Function for displaying the result and color block
            function displayResult(resultColor, inputColor, rgb, isError = false) {
                let backgroundColor = `white`;
                let textColor = 'black';
                let styleColorResult = "black";

                if (isError) {
                    backgroundColor = `red`;
                    textColor = 'white';
                    styleColorResult = "red";
                }

                const resultColorElement = document.getElementById("result");
                resultColorElement.querySelector('.result-color-out').textContent = resultColor;
                resultColorElement.querySelector('.result-color-in').textContent = inputColor;

                if (rgb) {
                    backgroundColor = `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
                    if (!isBrightColor(rgb.r, rgb.g, rgb.b)) {
                        textColor = "white";
                    }
                }

                document.documentElement.style.setProperty('--color-text-out', styleColorResult);
                document.documentElement.style.setProperty('--color-text-in', textColor);
                document.documentElement.style.setProperty('--color-solved', backgroundColor);
            }

            // Function for displaying an error message
            function displayError(message) {
                displayResult(message, null, null, true);
            }

            // Regular expression for validating the color code format
            const hexRegex = /^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})*;? *$/;
            const rgbRegex = /^(rgb|RGB)? *\(*(\d{1,3}) *, *(\d{1,3}) *, *(\d{1,3}) *\)* *;? *$/;

            // Function to check if RGB values are valid
            function isValidRgb(r, g, b) {
                return r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255;
            }

            // Function to build an RGB object from an RGB string
            function buildRgb() {
                const match = inputColor.match(rgbRegex);
                const r = parseInt(match[2]);
                const g = parseInt(match[3]);
                const b = parseInt(match[4]);

                return {r, g, b};
            }

            // Function to get an RGB string from an RGB object
            function getRgbText(rgb) {
                return `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
            }

            // Function to get a HEX string from a HEX code
            function getHexText(hex) {
                return `#${hex}`;
            }

            if (!inputColor) {
                displayError("Enter the color!");
            } else if (hexRegex.test(inputColor)) {
                let hex = inputColor.replace("#", "").toUpperCase();
                if (hex.length === 3) {
                    hex = shortHexToFullHex(hex);
                }

                const rgb = hexToRgb(hex);
                displayResult(getRgbText(rgb), getHexText(hex), rgb);
            } else if (rgbRegex.test(inputColor)) {
                const rgb = buildRgb();
                if (!isValidRgb(rgb.r, rgb.g, rgb.b)) {
                    displayError("Invalid RGB values (0-255)!");
                    return;
                }

                const hex = rgbToHex(rgb);
                displayResult(getHexText(hex), getRgbText(rgb), rgb);
            } else {
                displayError("Invalid Color Format!");
            }
        }

        function convertColor() {
            // Get user input
            const inputColor = document.getElementById("inputColor").value;

            resolveConvertColor(inputColor);
        }

        addEventListener( "DOMContentLoaded", () => {
            // Initialize with my best color
            resolveConvertColor('#455A64');
        })
              
            
!
999px

Console