Skip to content

Instantly share code, notes, and snippets.

@SafeerH
Last active August 30, 2017 19:02
Show Gist options
  • Save SafeerH/f52eb58961203c8113686ad629127c0d to your computer and use it in GitHub Desktop.
Save SafeerH/f52eb58961203c8113686ad629127c0d to your computer and use it in GitHub Desktop.
HTML colour palette (Javascript/CSS)
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<style>
#container {
display: inline-block;
position: relative;
}
#selection {
display: inline-block;
background: #fff;
border: 5px solid #fff;
border-radius: 3px;
box-shadow: 0px 0px 1px 1px rgba(0, 0, 0, .2);
width: 150px;
height: 150px;
margin-left: 50px;
}
#current,
#selected {
width: 150px;
height: 75px;
display: block;
}
r {
display: block;
}
c,
point {
display: inline-block;
width: 3px;
height: 6px;
}
point {
position: absolute;
background-color: red;
display: none;
}
</style>
</head>
<body>
<div id="container">
<point></point>
</div>
<div id="selection">
<div id="selected"></div>
<div id="current"></div>
</div>
<script>
$(function () {
var lock = false;
var $container = $('#container');
var saturation = 100;
for (var l = 0; l <= 100; l++) {
var $r = $('<r></r>');
for (var h = 0; h <= 360; h++) {
var $c = $('<c></c>').css('background-color', `hsl(${h},${saturation}%,${l}%)`);
$r.append($c);
}
$container.append($r);
}
$(document).on('mouseover', 'c', function () {
var color = $(this).css('background-color');
$('#current').css('background-color', color);
});
$(document).on('click', 'c', function () {
var color = $(this).css('background-color');
$('#selected').css('background-color', color);
var $point = $('point');
$point.css('top', this.offsetTop);
$point.css('left', this.offsetLeft);
$point.css('display', 'block');
})
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment