Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
    <title>COLOUR</title>
    <link rel="stylesheet" type="text/css" href="main.css">
    <script src="json.js"></script>
</head>
<body>
<h2 id ="colordisplay">#000000</h2>
<form name="myform">
    <label for="name">Name</label>
    <input type="text" id="name" name="name" value=""/>
    <label for="red">RED</label>
    <input type="range" min="0" max="255" step="1"
           value="0" id="red" name="red" onchange="changeColors()"/>
    <label for="green">Green</label>
    <input type="range" min="0" max="255" step="1"
           value="0" id="green" name="green" onchange="changeColors()"/>
    <label for="blue">Blue</label>
    <input type="range" min="0" max="255" step="1"
           value="0" id="blue" name="blue" onchange="changeColors()"/>
    <label for="opacity">Alpha</label>
    <input type="range" min="0" max="1" step="0.1"
           value="1" id="opacity" name="opacity" onchange="changeColors()"/>
    <input name="button1" type="button" value="Save" onclick="Save()"/>
    <select id="selectColor">
            <option></option>
    </select>
    <input name="button2" type="button" value="Place" onclick="Place();"/>
    <input name="button3" type="button" value="Remove" onclick="Remove();"/>
  </form>
<div id="div"><div>
</body>
 
function changeColors() {
    //get the numbers from the html
    var rd = parseInt(document.getElementById("red").value);
    var gr = parseInt(document.getElementById("green").value);
    var bl = parseInt(document.getElementById("blue").value);
    var op = parseFloat(document.getElementById("opacity").value);
    //convert the decimal into hexadecimal
    var rdhex = (rd < 16) ? "0" + rd.toString(16) : rd.toString(16);
    var grhex = (gr < 16) ? "0" + gr.toString(16) : gr.toString(16);
    var blhex = (bl < 16) ? "0" + bl.toString(16) : bl.toString(16);
    //concatenate all hex to generate a color
    var hexcode = "#" + rdhex + grhex + blhex;
    //set the background of the div
    setColor(hexcode, op);
}
function setColor(hexcode, opacity) {
    //view the change in the browser
    document.getElementById("div").style.backgroundColor = hexcode;
    document.getElementById("colordisplay").innerHTML = hexcode;
    //change opacity
    document.getElementById("div").style.opacity = opacity;
}
var colors = [];
function Save() {
    var name = document.getElementById("name").value;
    var rgb = document.getElementById("colordisplay").innerHTML;
    var opacity = document.getElementById("div").style.opacity;
    colors.push({
        name_prop:name,
        rgb_prop:rgb,
        opacity_prop:opacity
    });
    //pass the object to the drop down list
    var select = document.getElementById("selectColor");
    var opt = name;
    var el = document.createElement("option");
    el.innerHTML = opt;
    el.value = opt;
    select.appendChild(el);
    console.log(colors);
}
function Place() {
    var select = document.getElementById("selectColor");
    colors.forEach(function(colorObject) {
        if (colorObject.name_prop == select.value) {
            setColor(colorObject.rgb_prop, colorObject.opacity_prop);
        }
    });
}
function Remove () {
    var select = document.getElementById("selectColor");
    for(var i = 0; i < colors.length; i --) {
            var selectIndex = colors.indexOf(select.value);
            if(selectIndex !== -1){
            colors.splice(i, 1);
        }
        return false;
     }
}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
OMISpro
0viewers