This is a non dropdown color chooser.
Here is an example:
This is the code:
<!DOCTYPE HTML>
<html>
<body style=”background:black”>
<div id = “frame2” style = “position: absolute; width: 100%; height: 100%”>
<div id=”d3″ style=”position: absolute; top:100px; left: 100px; width: 300px; height: 300px; border: 1px solid white; border-radius: 20px”></div>
<div id=”d4″ style=”position: absolute; top:100px; left: 500px; width: 300px; height: 300px; border: 1px solid white; border-radius: 20px”></div>
</div>
<center><div id=”frame” style=”position: relative; width: 210px”>
<div id=”d1″ style=”position: absolute; left: 10px;width:30px; height: 30px; background: red;” ></div><div id=”d2″ style=” position: absolute; left: 40px; width:30px; height: 30px; background: black;” ></div><input type = “text” id=”t1″ style = “position: absolute; left: 70px; width: 120px” value = “” ondblclick=’chngCol();’/>
</div></center>
<script>
var cnt1 = 0; var d1 = document.getElementById(“d1”); var d2 = document.getElementById(“d2”); var r = 0; var g = 0; var b = 0; var ccnt = -1; var clrs = []; var a = 0; var b = 0; var oldURL = “tmpColor.html”;
document.onkeydown = setOption;
function setOption(e) {
if (e.keyCode == 187) {
if (d1.style.backgroundColor == “red”) r += 2;
if (d1.style.backgroundColor == “green”) g += 2;
if (d1.style.backgroundColor == “blue”) b += 2;
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
d2.style.backgroundColor = “rgb(” + r + “,” + g + “,” + b + “)”;
document.getElementById(“t1”).value = d2.style.backgroundColor;
}
if (e.keyCode == 189) {
if (d1.style.backgroundColor == “red”) r -= 2;
if (d1.style.backgroundColor == “green”) g -= 2;
if (d1.style.backgroundColor == “blue”) b -= 2;
if (r < 0) r = 0;
if (g < 0) g = 0;
if (b < 0) b = 0;
d2.style.backgroundColor = “rgb(” + r + “,” + g + “,” + b + “)”;
document.getElementById(“t1”).value = d2.style.backgroundColor;
}
if (e.keyCode == 18) {
cnt1 ++;
if (cnt1 == 0) d1.style.backgroundColor = “red”;
if (cnt1 == 1) d1.style.backgroundColor = “green”;
if (cnt1 == 2) d1.style.backgroundColor = “blue”;
if (cnt1 == 2) cnt1 = -1;
}
if (e.keyCode== 13) makeColors();
if (e.keyCode== 17) setColors();
if (e.keyCode== 32) sve();
}
function makeColors() {
ccnt ++;
clrs[ccnt] = d2.style.backgroundColor;
}
function setColors() {
document.body.style.backgroundColor = clrs[0];
document.getElementById(“d3”).style.backgroundColor = clrs[1];
document.getElementById(“d4”).style.backgroundImage = ‘-webkit-linear-gradient(left,’ + clrs[2] + ‘,’ + clrs[3] + ‘,’ + clrs[2] + ‘)’;
document.getElementById(“d4”).style.backgroundColor = ‘linear-gradient(left,’ + clrs[2] + ‘,’ + clrs[3] + ‘,’ + clrs[2] + ‘)’;
}
function chngCol() {
d2.style.backgroundColor = document.getElementById(“t1”).value;
a = d2.style.backgroundColor.indexOf(“,”);
b = a + 2;
r = d2.style.backgroundColor.substr(4,a-4);
a = d2.style.backgroundColor.indexOf(“,”, b);
g = d2.style.backgroundColor.substr(b,a-b);
b = a + 2;
a = d2.style.backgroundColor.indexOf(“)”, b);
b = d2.style.backgroundColor.substr(b,a-b);
}
function sve() {
var textToSave = ‘<!DOCTYPE HTML> <html> <style> body{background-color: ‘ + clrs[0] + ‘} </style> <body> ‘ + document.getElementById(“frame2”).innerHTML;
textToSave += ‘</body></html>’;
document.getElementById(“t1”).value = textToSave;
alert(textToSave);
var textToSaveAsBlob = new Blob([textToSave], {type:”text/plain”});
var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
fileNameToSaveAs = prompt(“FileName to save as”,oldURL);
oldURL = fileNameToSaveAs;
var downloadLink = document.createElement(“a”);
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = “Download File”;
downloadLink.href = textToSaveAsURL;
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = “none”;
document.body.appendChild(downloadLink);
downloadLink.click();
}
function destroyClickedElement(event) {
document.body.removeChild(event.target);
}
</script>
</body></html>
There is a square that shows the active color. Clicking Alt cycles between red, green and blue. The = key increases the value and the – key decreases it. This can be done for red,green and blue. Another square show the composite square. A text box shows the rgb vale of the composite:
if (e.keyCode == 18) {
cnt1 ++;
if (cnt1 == 0) d1.style.backgroundColor = “red”;
if (cnt1 == 1) d1.style.backgroundColor = “green”;
if (cnt1 == 2) d1.style.backgroundColor = “blue”;
if (cnt1 == 2) cnt1 = -1;
}
function setOption(e) {
if (e.keyCode == 187) {
if (d1.style.backgroundColor == “red”) r += 2;
if (d1.style.backgroundColor == “green”) g += 2;
if (d1.style.backgroundColor == “blue”) b += 2;
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
d2.style.backgroundColor = “rgb(” + r + “,” + g + “,” + b + “)”;
document.getElementById(“t1”).value = d2.style.backgroundColor;
}
if (e.keyCode == 189) {
if (d1.style.backgroundColor == “red”) r -= 2;
if (d1.style.backgroundColor == “green”) g -= 2;
if (d1.style.backgroundColor == “blue”) b -= 2;
if (r < 0) r = 0;
if (g < 0) g = 0;
if (b < 0) b = 0;
d2.style.backgroundColor = “rgb(” + r + “,” + g + “,” + b + “)”;
document.getElementById(“t1”).value = d2.style.backgroundColor;
}
This can also be done by entering a value in the text box and doubleclicking it:
function chngCol() {
d2.style.backgroundColor = document.getElementById(“t1”).value;
a = d2.style.backgroundColor.indexOf(“,”);
b = a + 2;
r = d2.style.backgroundColor.substr(4,a-4);
a = d2.style.backgroundColor.indexOf(“,”, b);
g = d2.style.backgroundColor.substr(b,a-b);
b = a + 2;
a = d2.style.backgroundColor.indexOf(“)”, b);
b = d2.style.backgroundColor.substr(b,a-b);
}
Hitting Enter than adds the selected color to an array:
if (e.keyCode== 13) makeColors();
function makeColors() {
ccnt ++;
clrs[ccnt] = d2.style.backgroundColor;
}
Hitting Control then finishes the operation:
if (e.keyCode== 17) setColors();
function setColors() {
document.body.style.backgroundColor = clrs[0];
document.getElementById(“d3”).style.backgroundColor = clrs[1];
document.getElementById(“d4”).style.backgroundImage = ‘-webkit-linear-gradient(left,’ + clrs[2] + ‘,’ + clrs[3] + ‘,’ + clrs[2] + ‘)’;
document.getElementById(“d4”).style.backgroundColor = ‘linear-gradient(left,’ + clrs[2] + ‘,’ + clrs[3] + ‘,’ + clrs[2] + ‘)’;
}
Hitting s saves the result.