var red = document.getElementById("r");
var green = document.getElementById("g");
var blue = document.getElementById("b");
var result = document.getElementById("result");
var colorBlock = document.getElementById("colorBlock");
function buttonPress(){
var redHex = convert(red);
var greenHex = convert(green);
var blueHex = convert(blue);
if (redHex === "invalidInput" || greenHex === "invalidInput" || blueHex === "invalidInput" )
{
result.innerHTML = ("Invalid input! Make sure you have no numbers greater than 255 or less than 0, and no invalid characters.");
colorBlock.innerHTML = "<i class='fa fa-exclamation-triangle'></i>";
colorBlock.style.backgroundColor = "tomato";
colorBlock.style.color = "#B24532";
}
else
{
var converted = redHex.concat(greenHex,blueHex);
console.log(converted);
result.innerHTML = ("#" + converted);
colorBlock.innerHTML = "";
colorBlock.style.color = "none";
colorBlock.style.backgroundColor = ("#" + converted);
}
}
function convert(c)
{
var color = Number(c.value);
color = color.toString(16).toUpperCase();
console.log(c.placeholder + ":" + color);
if (color.length === 1)
{
return color.concat(color);
}
else if (color.length > 2 || c < 0 )
{
return("invalidInput");
}
else
{
return color;
}
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.