Skip to content

Instantly share code, notes, and snippets.

@MadeByMike
MadeByMike / css-less-text-contrast
Created February 14, 2014 04:15
less mixin for contrasting text background colours
.text-contrast(@background) when (abs(lightness(@background)) > 50) {
color: #000;
}
.text-contrast(@background) when (abs(lightness(@background)) < 51) {
color: #FFF;
}
function text_contrast(hexcolor) {
//parse hex
var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(color);
// Convert to RGB value between 0 and 1, and retrieve luminance
// Alternative methods: (0.2126*R) + (0.7152*G) + (0.0722*B) or (0.299*R + 0.587*G + 0.114*B)
var L = (0.3 * parseInt(rgb[1], 16)/255) + (0.59 * parseInt(rgb[2], 16)/255) + (0.11 * parseInt(rgb[3], 16)/255);
console.log(L);
return L > 0.5 ? "#000": "#FFF";
}
// I was looking to highlight words in text nodes today and came across:
// http://bartaz.github.io/sandbox.js/jquery.highlight.html
// http://johannburkard.de/
//
// Both seemed a little verbose so I came up with the solution below.
// Please note this has not yet been fully tested, but worked for my needs.
var words = ['words','to','mark'];
$.each(words, function(i, word){
// This method is kind of cheeky in its implementation.
// It uses the JavaScript’s object to add every item in the array as key.
// As we all know, objects accepts only unique keys and sure we did capitalize on that.
Array.prototype.unique = function() {
var o = {}, i, l = this.length, r = [];
for(i=0; i<l;i+=1) o[this[i]] = this[i];
for(i in o) r.push(o[i]);
return r;
};
@MadeByMike
MadeByMike / transition-end
Last active August 29, 2015 14:02
Custom event 'transition-end', forget browser prefixes
//Custom transition end event,
(function(window){
// Detect which transition event
var whichTransitionEvent = function(){
var t;
var el = document.createElement('fakeelement');
var transitions = {
'transition':'transitionend',
'OTransition':'oTransitionEnd',