Skip to content

Instantly share code, notes, and snippets.

View nblackburn's full-sized avatar

Nathaniel Blackburn nblackburn

View GitHub Profile
module.exports = (red, green, blue) => {
const rs = red / 255;
const gs = green / 255;
const bs = blue / 255;
const rn = rs <= 0.03928 ? rs / 12.92 : Math.pow((rs + 0.055) / 1.055, 2.4);
const gn = gs <= 0.03928 ? gs / 12.92 : Math.pow((gs + 0.055) / 1.055, 2.4);
const bn = bs <= 0.03928 ? bs / 12.92 : Math.pow((bs + 0.055) / 1.055, 2.4);
return (0.2126 * rn) + (0.7152 * gn) + (0.0722 * bn);
module.exports = hex => {
if (hex.charAt(0) === '#') {
hex = hex.substring(1);
}
if (hex.length !== 3 && hex.length !== 6) {
return false;
}
if (hex.length === 3) {
module.exports = (red, green, blue, preferShorthand = false) => {
let colours = [red, green, blue].map(value => value.toString(16));
let canBeCondensed = colours.every(value => value.charAt(0) === value.charAt(1));
if (canBeCondensed && preferShorthand) {
colours = colours.map(v => v.charAt(0));
}
return '#' + colours.join('');
};
@nblackburn
nblackburn / scheduler.js
Created January 30, 2019 00:02
Scheduler
let events = [];
let timeout = null;
let interval = 100;
const getScheduled = () => {
let now = Date.now();
return events.filter(event => {
return (event.lastInvoked + event.interval) < now;
});
@nblackburn
nblackburn / randomWeight.js
Last active October 15, 2018 14:41
Select a random item from an array of weights
module.exports = weights => {
let seed = Math.random() * weights.length;
return weights.find(weight => (seed - weight) <= 0);
};
@nblackburn
nblackburn / consecutive-search.js
Created August 3, 2018 13:27
Consecutive search
module.exports = (query, result, insensitive) => {
let index = 0;
let last = 0;
let matched = false;
// Normalize the strings we are comparing to each other.
if (insensitive) {
query = query.toLowerCase();
result = result.toLowerCase();
}
module.exports = (node, callback) => {
if (node.childNodes) {
for (let index = 0; index < node.childNodes.length; index++) {
const child = node.childNodes[index]
const next = callback(child)
if (!next) {
walk(child, callback)
}
}
input::-webkit-input-placeholder {
color: #666666;
}
input :-moz-placeholder {
color: #666666;
}
input::-moz-placeholder {
color: #666666;
@nblackburn
nblackburn / camelToKebab.js
Last active March 5, 2025 17:25
Convert a string from camel case to kebab case.
module.exports = (string) => {
return string.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase();
};
@nblackburn
nblackburn / wildcard-match.js
Last active October 21, 2017 20:07
Check if a string matches a wildcard pattern.
module.exports = (test, pattern) => {
return new RegExp(test.replace(/\*/g, '([^*]+)'), 'g').test(pattern);
};