Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const culori = require("culori");
- let data = require("./user.json");
- for (const k in data.schematic) {
- const item = data.schematic[k];
- let match_start = 0;
- if (typeof item !== 'string') {
- continue;
- } else if (item.startsWith('rgb(')) {
- match_start = 4;
- } else if (item.startsWith('rgba(')) {
- match_start = 5;
- } else {
- continue;
- }
- const parsed = item.substring(match_start, item.length - 1)
- .replace(/ /g, '')
- .split(',')
- .map(v => parseFloat(v));
- // 1. Create an sRGB object from the parsed array
- const srgbColor = {
- mode: 'rgb',
- r: parsed[0] / 255,
- g: parsed[1] / 255,
- b: parsed[2] / 255,
- alpha: parsed[3] !== undefined ? parsed[3] : 1
- };
- // 2. Convert to Oklch space
- const oklchColor = culori.oklch(srgbColor);
- // 3. Invert only the Lightness (L) component (range 0.0 - 1.0)
- oklchColor.l = 1.0 - oklchColor.l;
- // make everything darker
- oklchColor.l = Math.max(oklchColor.l * 1.2 - 0.2, 0);
- // Shift hue
- // oklchColor.h += 180;
- // 4. Convert back to sRGB
- const invertedSrgb = culori.rgb(oklchColor);
- // 5. Re-scale to 0-255 bounds and format values
- const r = Math.round(invertedSrgb.r * 255);
- const g = Math.round(invertedSrgb.g * 255);
- const b = Math.round(invertedSrgb.b * 255);
- let stringified;
- if (parsed.length === 4) {
- stringified = `rgba(${r},${g},${b},${parsed[3]})`;
- } else {
- stringified = `rgb(${r},${g},${b})`;
- }
- data.schematic[k] = stringified;
- }
- console.log(JSON.stringify(data, null, '\t'));
Advertisement