Skip to content

Instantly share code, notes, and snippets.

View zazaulola's full-sized avatar
🏠
Working from home

zazaulola

🏠
Working from home
View GitHub Profile
@zazaulola
zazaulola / worker.js
Created July 20, 2024 05:31
Create local worker without HTTP request
export function createWorker(callable) {
const code = `(${callable.toString()})()`;
const blob = new Blob([code], { type: 'text/javascript' });
const url = URL.createObjectURL(blob);
const worker = new Worker(url);
URL.revokeObjectURL(url);
return worker;
}
export function executeWorker(callable, data) {
@zazaulola
zazaulola / hermite.js
Created July 20, 2024 05:33
Basic one-thread image hermite resample
const s2l = c => (c > 0.04045 ? ((c + 0.055) / 1.055) ** 2.4 : c / 12.92);
const l2s = c => (c > 0.0031308 ? 1.055 * Math.pow(c, 1 / 2.4) - 0.055 : 12.92 * c);
const LINEAR = new Array(256).fill().map((_, i) => s2l(i / 255));
onmessage = ({ data: { srcData, srcWidth, srcHeight, dstWidth, dstHeight } }) => {
const { ceil, floor, abs, min, hypot } = Math;
const ratioH = srcHeight / dstHeight;
const ratioW = srcWidth / dstWidth;
const halfRatioW = ceil(ratioW / 2);
const halfRatioH = ceil(ratioH / 2);
@zazaulola
zazaulola / atkinson.js
Created July 20, 2024 05:35
Grayscale Image Halftone dithering // Best algorithm
onmessage = ({ data: { data, width, height } }) => {
const add = (d, i, v) => {
if (i >= d.length) return;
d[i] += v;
};
let i = 0,
w = width * 4;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++, i += 4) {
@zazaulola
zazaulola / bayer.js
Created July 20, 2024 05:37
Grayscale image halftone dithering
onmessage = ({ data: { data, width, height } }) => {
const thresholdMap = [
[15, 135, 45, 165],
[195, 75, 225, 105],
[60, 180, 30, 150],
[240, 120, 210, 90],
];
let min = 255,
max = 0,
@zazaulola
zazaulola / floyd-steinberg.js
Created July 20, 2024 05:38
Grayscale image halftone dithering
onmessage = ({ data: { data, width, height } }) => {
const add = (d, i, v) => {
if (i >= d.length) return;
d[i] += v;
};
const w = width * 4;
let i = 0;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++, i += 4) {
// Matrices multiply
const multiply = (a, b) => a.map((_, r) => b[0].map((_, c) => a[r].reduce((s,_,i) => s + a[r][i] * b[i][c], 0)));
@zazaulola
zazaulola / rgb-oklab-converter.js
Last active July 30, 2024 03:21
True color difference formula is calculated using OKLab color space
const color_distance = ([ L1, a1, b1 ], [ L2, a2, b2 ] ) => Math.hypot( L1 - L2, a1 - a2, b1 - b2 );
const linear = x => x >= 0.04045 ? ((x + 0.055) / (1 + 0.055)) ** 2.4 : x / 12.92;
const gamma = x => x >= 0.0031308 ? 1.055 * x ** (1 / 2.4) - 0.055 : 12.92 * x;
const clamp = x => Math.min ( 255, Math.max ( 0, x ) );
const multiply = (a, b) =>
@zazaulola
zazaulola / SearchInTheShadows.js
Last active August 14, 2024 01:12 — forked from Spencer-Doak/SearchInTheShadows.js
Recursively find all shadow roots in a page.
/**
* Find all elements with openned ShadowRoots
*
* @param {Node} e - An element that we should search for ShadowRoots within.
* @returns {Array<Element>} Array of Elements that holds ShadowRoot
*/
const findRoots = (e = document.documentElement) =>
[e,...e.querySelectorAll('*')]
.filter(e => e.shadowRoot)
.flatMap(e => [e, ...findRoots(e.shadowRoot)]);
@zazaulola
zazaulola / calendar-util.js
Created October 7, 2024 11:25
Some calendar functions
// calculate week number for date
function calcWN(y, m, d) {
let J = gc2jd(y, m, d);
let d4 = (J + 31741 - J % 7) % 146097 % 36524 % 1461;
let L = 0 | d4 / 1460;
let d1 = (d4 - L) % 365 + L;
return (0 | d1 / 7) + 1;
}
// Debug mode
const DEBUG = true;
const log = (...args) => {
if (DEBUG) console.log(...args);
}
// regular expression to parse numbers
const rxNumberStr = '[+-]?(?:(?:[0-9]+\\.[0-9]+)|(?:\\.[0-9]+)|(?:[0-9]+))(?:[eE][+-]?[0-9]+)?';
// regular expression to parse next lemma from d attribute of <path>
const rxDLemmaStr = `[MmLlHhVvCcSsQqTtAaZz]|${rxNumberStr}`;