Skip to content

Instantly share code, notes, and snippets.

@liquorice
liquorice / gist:052294c8237f0b47b48e
Created August 11, 2014 06:55
Determine if a colour appears light or dark
var light_or_dark = function(hex) {
var rgb = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex),
luma = Math.sqrt(
0.299 * parseInt(rgb[1], 16) +
0.587 * parseInt(rgb[2], 16) +
0.144 * parseInt(rgb[3], 16)
);
return (luma > 13) ? "light" : "dark";
};
@liquorice
liquorice / gist:0c0a86e14c04694d9f30
Last active August 29, 2015 14:02
SASS keyframes mixin
@mixin animation($options...) {
$random: random(9999);
$animation_name: "animation_#{$random}";
-webkit-animation: $animation_name $options;
-moz-animation: $animation_name $options;
-ms-animation: $animation_name $options;
animation: $animation_name $options;
@at-root {
@liquorice
liquorice / gist:8394584
Last active January 3, 2016 02:19
set document.location.href without causing the viewport to scroll
var set_document_hash = function(hash) {
var id = hash,
temp_id = "TEMP-" + hash,
target = $("#" + id);
if (target.length) target.attr("id", temp_id);
document.location.hash = hash;
if (target.length) target.attr("id", id);
}
@liquorice
liquorice / Unrandom.js
Last active December 30, 2015 05:09
Unrandom — for when someone says they want a random selection, but what they mean is uniformly sampled without repetition.
// Usage:
// var randomiser = new Unrandom(["alpha", "bravo", "charlie", "delta"]);
// console.debug(randomiser.get());
var Unrandom = function(_items) {
var items,
last_item,
marker = 0;
@liquorice
liquorice / gist:7538042
Created November 19, 2013 00:33
SASS prefixer mixin
// Usage:
// @include prefix(transform-origin, 0 0 0, webkit o moz);
@mixin prefix($property, $value, $prefixes) {
@each $prefix in $prefixes {
-#{$prefix}-#{$property}: $value;
}
#{$property}: $value;
}
@liquorice
liquorice / _breakpoints.scss
Created September 23, 2013 01:53
SASS breakpoints mixin
@mixin breakpoint($points) {
@if index($points, large) {
@media screen and (min-width: 1000) { @content; }
html.ie-large & { @content; }
}
@if index($points, medium) {
@media screen and (max-width: 999) and (min-width: 600) { @content; }
}
@if index($points, small) {
@media screen and (max-width: 599) { @content; }
@liquorice
liquorice / gist:1440692
Created December 6, 2011 23:57
in place array shuffling with fisher-yates
Array.prototype.shuffle = function() {
var l = this.length, t, r;
if (l == 0) return;
while (l--) {
r = Math.floor(Math.random() * l + 1);
t = this[l];
this[l] = this[r]
this[r] = t;
}
@liquorice
liquorice / gist:1228474
Created September 20, 2011 06:04
truncate text to nearest word boundry
# truncate text without chopping words
# where d = text to truncate
# x = maximum amount of characters
(d.length <= x) ? d : d[0..x].split()[0...-1].join(' ') + '...'
@liquorice
liquorice / gist:1113000
Created July 29, 2011 02:14
animated hash-link scrolling
$('.scroll-to').click(function(e) {
target = $($(this).attr('href'));
if (target.offset()) {
$('html, body').animate({scrollTop: target.offset().top + 'px'}, 500);
}
e.preventDefault();
});
@liquorice
liquorice / gist:1068689
Created July 7, 2011 00:47
remove dropbox "conflict" files
find . -type f -name "* conflicted *" -exec rm -f {} \;