HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
: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;
}
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');
})
Also see: Tab Triggers