Skip to content

Instantly share code, notes, and snippets.

function stringToColor (text, { saturation = 64, lightness = 40, alpha = 100 } = {}) {
let hue = [...text].reduce((a, c) => c.charCodeAt(0) + ((a << 5) - a), 0) % 360
return `hsl(${hue} ${saturation}% ${lightness}% / ${alpha / 100})`
}
function colorFromString (
text,
saturation = 90,
lightness = 50,
alpha = 100
) {
let hash = 0
for (let i = 0; i < text.length; i++)
function observeMutations (element, callback, options) {
const observer = new MutationObserver(mutations =>
mutations.forEach(m => callback(m))
)
observer.observe(element, Object.assign({
childList: true,
attributes: true,
attributeOldValue: true,
import tagged from 'tagged.js'
const css = tagged(function (str) {
const css = new CSSStyleSheet()
return css.replace(str.trim())
})
export default css
@protomorph
protomorph / siblings.js
Created May 12, 2023 20:49
Get all sibling elements
const getSiblings = e => Array.from(e.parentNode.children).filter(sib => sib !== e)
@protomorph
protomorph / flatten.js
Created May 12, 2023 20:46
Flatten an array
const flatten = (array, depth = Infinity) => array.flat(depth)
@protomorph
protomorph / deburr.js
Created May 12, 2023 20:45
Remove accent from chars
const deburr = str => str.normalize('NFD').replace(/\p{Diacritic}/gu, '')
@protomorph
protomorph / chunk.js
Last active June 15, 2025 22:25
Split array into chunks
const chunk = (array, size) => array.map(
(_, i) => i % size === 0 ? array.slice(i, i + size) : null
).filter(Boolean)
function getFetch(url, params = {}) {
const queryString = Object.entries(params).map(
param => `${param[0]}=${param[1]}`
).join('&')
return fetch(`${url}?${queryString}`, {
method: "GET",
headers: { "Content-Type": "application/json" }
}).then(res => res.json())
}
//
// Color contrast v2.
// --------------------
@function _color-contrast-test($color, $threshold) {
$r: red($color);
$g: green($color);
$b: blue($color);
$yiq: (($r * 299) + ($g * 587) + ($b * 114)) / 1000;