Color Picker
HTML
<div id="colorWrapper">
<div id="picker"></div>
<div id="slider"></div>
</div>
<label>Hex: </label><input type="text" id="hex" readonly>
CSS
#picker {
width: 200px;
height: 200px;
display:inline-block;
}
#slider {
width: 30px;
height: 200px;
display:inline-block;
}
#colorWrapper {
width:235px;
background:#444;
border-radius:3px;
padding:5px;
}
#hex {
border:none;
background:inherit;
}
JavaScript
/**
* ColorPicker - pure JavaScript color picker without using images, external CSS or 1px divs.
* Copyright © 2011 David Durman, All rights reserved.
*/
(function(window, document, undefined) {
var type = (window.SVGAngle || document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML"),
picker, slide, hueOffset = 15, svgNS = 'http://www.w3.org/2000/svg';
// This HTML snippet is inserted into the innerHTML property of the passed color picker element
// when the no-hassle call to ColorPicker() is used, i.e. ColorPicker(function(hex, hsv, rgb) { ... });
var colorpickerHTMLSnippet = [
'<div class="picker-wrapper">',
'<div class="picker"></div>',
'<div class="picker-indicator"></div>',
'</div>',
'<div class="slide-wrapper">',
'<div class="slide"></div>',
'<div class="slide-indicator"></div>',
'</div>'
].join('');
/**
* Return mouse position relative to the element el.
*/
function mousePosition(evt) {
// IE:
if (window.event && window.event.contentOverflow !== undefined) {
return { x: window.event.offsetX, y: window.event.offsetY };
}
// Webkit:
if (evt.offsetX !== undefined && evt.offsetY !== undefined) {
return { x: evt.offsetX, y: evt.offsetY };
}
// Firefox:
var wrapper = evt.target.parentNode.parentNode;
return { x: evt.layerX - wrapper.offsetLeft, y: evt.layerY - wrapper.offsetTop };
}
/**
* Create SVG element.
*/
function $(el, attrs, children) {
el = document.createElementNS(svgNS, el);
for (var key in attrs)
el.setAttribute(key, attrs[key]);
if (Object.prototype.toString.call(children) != '[object Array]') children = [children];
var i = 0, len = (children[0] && children.length) ||...