Skip to content

Instantly share code, notes, and snippets.

@ErickPetru
Last active March 16, 2023 22:28
Show Gist options
  • Save ErickPetru/d56a698f6600d19697181fa475671ce5 to your computer and use it in GitHub Desktop.
Save ErickPetru/d56a698f6600d19697181fa475671ce5 to your computer and use it in GitHub Desktop.
Extract CSS custom properties (CSS variables) from Tailwind Color Palette by Web scrapping its documentation
/* Extract CSS custom properties (CSS variables) from Tailwind Color Palette
* Navigate to: https://tailwindcss.com/docs/customizing-colors
* Then open browser's console and run the following code
*/
const palette = [
...document
.querySelector("#content-wrapper > .grid.grid-cols-1")
.querySelectorAll(".flex.flex-col.space-y-3"),
]
.map((row) => ({
color: row
.querySelector(".w-16.shrink-0")
.textContent.toString()
.toLowerCase(),
shades: [...row.querySelectorAll(".relative.flex > .cursor-pointer")].map(
(color) => ({
shade: color.querySelector(".font-medium").textContent.toString(),
rgb: color
.querySelector(".h-10")
.style.backgroundColor.replace(/[rgb|(|)|,]/g, "")
.replace(")", ""),
hex: color
.querySelector(".font-mono")
.textContent.toString()
.toLowerCase(),
})
),
}))
.reduce(
(acc, curr) => ({
...acc,
[curr.color]: curr.shades.reduce(
(acc2, curr2) => ({
...acc2,
[curr2.shade]: { rgb: curr2.rgb, hex: curr2.hex },
}),
{}
),
}),
{}
);
console.log(
Object.keys(palette)
.map(
(color) =>
`/* ${color[0].toUpperCase()}${color.substr(1)} */\n` +
Object.keys(palette[color])
.map(
(shade) =>
`--color-${color}-${shade}-rgb: rgb(${palette[color][shade].rgb});\n--color-${color}-${shade}-hex: ${palette[color][shade].hex};`
)
.join("\n")
)
.join("\n\n")
);
@ErickPetru
Copy link
Author

ErickPetru commented Mar 2, 2023

Results targeting TailwindCSS Colors right now:

/* Slate */
--color-slate-50-rgb: rgb(248 250 252);
--color-slate-50-hex: #f8fafc;
--color-slate-100-rgb: rgb(241 245 249);
--color-slate-100-hex: #f1f5f9;
--color-slate-200-rgb: rgb(226 232 240);
--color-slate-200-hex: #e2e8f0;
--color-slate-300-rgb: rgb(203 213 225);
--color-slate-300-hex: #cbd5e1;
--color-slate-400-rgb: rgb(148 163 184);
--color-slate-400-hex: #94a3b8;
--color-slate-500-rgb: rgb(100 116 139);
--color-slate-500-hex: #64748b;
--color-slate-600-rgb: rgb(71 85 105);
--color-slate-600-hex: #475569;
--color-slate-700-rgb: rgb(51 65 85);
--color-slate-700-hex: #334155;
--color-slate-800-rgb: rgb(30 41 59);
--color-slate-800-hex: #1e293b;
--color-slate-900-rgb: rgb(15 23 42);
--color-slate-900-hex: #0f172a;

/* Gray */
--color-gray-50-rgb: rgb(249 250 251);
--color-gray-50-hex: #f9fafb;
--color-gray-100-rgb: rgb(243 244 246);
--color-gray-100-hex: #f3f4f6;
--color-gray-200-rgb: rgb(229 231 235);
--color-gray-200-hex: #e5e7eb;
--color-gray-300-rgb: rgb(209 213 219);
--color-gray-300-hex: #d1d5db;
--color-gray-400-rgb: rgb(156 163 175);
--color-gray-400-hex: #9ca3af;
--color-gray-500-rgb: rgb(107 114 128);
--color-gray-500-hex: #6b7280;
--color-gray-600-rgb: rgb(75 85 99);
--color-gray-600-hex: #4b5563;
--color-gray-700-rgb: rgb(55 65 81);
--color-gray-700-hex: #374151;
--color-gray-800-rgb: rgb(31 41 55);
--color-gray-800-hex: #1f2937;
--color-gray-900-rgb: rgb(17 24 39);
--color-gray-900-hex: #111827;

/* Zinc */
--color-zinc-50-rgb: rgb(250 250 250);
--color-zinc-50-hex: #fafafa;
--color-zinc-100-rgb: rgb(244 244 245);
--color-zinc-100-hex: #f4f4f5;
--color-zinc-200-rgb: rgb(228 228 231);
--color-zinc-200-hex: #e4e4e7;
--color-zinc-300-rgb: rgb(212 212 216);
--color-zinc-300-hex: #d4d4d8;
--color-zinc-400-rgb: rgb(161 161 170);
--color-zinc-400-hex: #a1a1aa;
--color-zinc-500-rgb: rgb(113 113 122);
--color-zinc-500-hex: #71717a;
--color-zinc-600-rgb: rgb(82 82 91);
--color-zinc-600-hex: #52525b;
--color-zinc-700-rgb: rgb(63 63 70);
--color-zinc-700-hex: #3f3f46;
--color-zinc-800-rgb: rgb(39 39 42);
--color-zinc-800-hex: #27272a;
--color-zinc-900-rgb: rgb(24 24 27);
--color-zinc-900-hex: #18181b;

/* Neutral */
--color-neutral-50-rgb: rgb(250 250 250);
--color-neutral-50-hex: #fafafa;
--color-neutral-100-rgb: rgb(245 245 245);
--color-neutral-100-hex: #f5f5f5;
--color-neutral-200-rgb: rgb(229 229 229);
--color-neutral-200-hex: #e5e5e5;
--color-neutral-300-rgb: rgb(212 212 212);
--color-neutral-300-hex: #d4d4d4;
--color-neutral-400-rgb: rgb(163 163 163);
--color-neutral-400-hex: #a3a3a3;
--color-neutral-500-rgb: rgb(115 115 115);
--color-neutral-500-hex: #737373;
--color-neutral-600-rgb: rgb(82 82 82);
--color-neutral-600-hex: #525252;
--color-neutral-700-rgb: rgb(64 64 64);
--color-neutral-700-hex: #404040;
--color-neutral-800-rgb: rgb(38 38 38);
--color-neutral-800-hex: #262626;
--color-neutral-900-rgb: rgb(23 23 23);
--color-neutral-900-hex: #171717;

/* Stone */
--color-stone-50-rgb: rgb(250 250 249);
--color-stone-50-hex: #fafaf9;
--color-stone-100-rgb: rgb(245 245 244);
--color-stone-100-hex: #f5f5f4;
--color-stone-200-rgb: rgb(231 229 228);
--color-stone-200-hex: #e7e5e4;
--color-stone-300-rgb: rgb(214 211 209);
--color-stone-300-hex: #d6d3d1;
--color-stone-400-rgb: rgb(168 162 158);
--color-stone-400-hex: #a8a29e;
--color-stone-500-rgb: rgb(120 113 108);
--color-stone-500-hex: #78716c;
--color-stone-600-rgb: rgb(87 83 78);
--color-stone-600-hex: #57534e;
--color-stone-700-rgb: rgb(68 64 60);
--color-stone-700-hex: #44403c;
--color-stone-800-rgb: rgb(41 37 36);
--color-stone-800-hex: #292524;
--color-stone-900-rgb: rgb(28 25 23);
--color-stone-900-hex: #1c1917;

/* Red */
--color-red-50-rgb: rgb(254 242 242);
--color-red-50-hex: #fef2f2;
--color-red-100-rgb: rgb(254 226 226);
--color-red-100-hex: #fee2e2;
--color-red-200-rgb: rgb(254 202 202);
--color-red-200-hex: #fecaca;
--color-red-300-rgb: rgb(252 165 165);
--color-red-300-hex: #fca5a5;
--color-red-400-rgb: rgb(248 113 113);
--color-red-400-hex: #f87171;
--color-red-500-rgb: rgb(239 68 68);
--color-red-500-hex: #ef4444;
--color-red-600-rgb: rgb(220 38 38);
--color-red-600-hex: #dc2626;
--color-red-700-rgb: rgb(185 28 28);
--color-red-700-hex: #b91c1c;
--color-red-800-rgb: rgb(153 27 27);
--color-red-800-hex: #991b1b;
--color-red-900-rgb: rgb(127 29 29);
--color-red-900-hex: #7f1d1d;

/* Orange */
--color-orange-50-rgb: rgb(255 247 237);
--color-orange-50-hex: #fff7ed;
--color-orange-100-rgb: rgb(255 237 213);
--color-orange-100-hex: #ffedd5;
--color-orange-200-rgb: rgb(254 215 170);
--color-orange-200-hex: #fed7aa;
--color-orange-300-rgb: rgb(253 186 116);
--color-orange-300-hex: #fdba74;
--color-orange-400-rgb: rgb(251 146 60);
--color-orange-400-hex: #fb923c;
--color-orange-500-rgb: rgb(249 115 22);
--color-orange-500-hex: #f97316;
--color-orange-600-rgb: rgb(234 88 12);
--color-orange-600-hex: #ea580c;
--color-orange-700-rgb: rgb(194 65 12);
--color-orange-700-hex: #c2410c;
--color-orange-800-rgb: rgb(154 52 18);
--color-orange-800-hex: #9a3412;
--color-orange-900-rgb: rgb(124 45 18);
--color-orange-900-hex: #7c2d12;

/* Amber */
--color-amber-50-rgb: rgb(255 251 235);
--color-amber-50-hex: #fffbeb;
--color-amber-100-rgb: rgb(254 243 199);
--color-amber-100-hex: #fef3c7;
--color-amber-200-rgb: rgb(253 230 138);
--color-amber-200-hex: #fde68a;
--color-amber-300-rgb: rgb(252 211 77);
--color-amber-300-hex: #fcd34d;
--color-amber-400-rgb: rgb(251 191 36);
--color-amber-400-hex: #fbbf24;
--color-amber-500-rgb: rgb(245 158 11);
--color-amber-500-hex: #f59e0b;
--color-amber-600-rgb: rgb(217 119 6);
--color-amber-600-hex: #d97706;
--color-amber-700-rgb: rgb(180 83 9);
--color-amber-700-hex: #b45309;
--color-amber-800-rgb: rgb(146 64 14);
--color-amber-800-hex: #92400e;
--color-amber-900-rgb: rgb(120 53 15);
--color-amber-900-hex: #78350f;

/* Yellow */
--color-yellow-50-rgb: rgb(254 252 232);
--color-yellow-50-hex: #fefce8;
--color-yellow-100-rgb: rgb(254 249 195);
--color-yellow-100-hex: #fef9c3;
--color-yellow-200-rgb: rgb(254 240 138);
--color-yellow-200-hex: #fef08a;
--color-yellow-300-rgb: rgb(253 224 71);
--color-yellow-300-hex: #fde047;
--color-yellow-400-rgb: rgb(250 204 21);
--color-yellow-400-hex: #facc15;
--color-yellow-500-rgb: rgb(234 179 8);
--color-yellow-500-hex: #eab308;
--color-yellow-600-rgb: rgb(202 138 4);
--color-yellow-600-hex: #ca8a04;
--color-yellow-700-rgb: rgb(161 98 7);
--color-yellow-700-hex: #a16207;
--color-yellow-800-rgb: rgb(133 77 14);
--color-yellow-800-hex: #854d0e;
--color-yellow-900-rgb: rgb(113 63 18);
--color-yellow-900-hex: #713f12;

/* Lime */
--color-lime-50-rgb: rgb(247 254 231);
--color-lime-50-hex: #f7fee7;
--color-lime-100-rgb: rgb(236 252 203);
--color-lime-100-hex: #ecfccb;
--color-lime-200-rgb: rgb(217 249 157);
--color-lime-200-hex: #d9f99d;
--color-lime-300-rgb: rgb(190 242 100);
--color-lime-300-hex: #bef264;
--color-lime-400-rgb: rgb(163 230 53);
--color-lime-400-hex: #a3e635;
--color-lime-500-rgb: rgb(132 204 22);
--color-lime-500-hex: #84cc16;
--color-lime-600-rgb: rgb(101 163 13);
--color-lime-600-hex: #65a30d;
--color-lime-700-rgb: rgb(77 124 15);
--color-lime-700-hex: #4d7c0f;
--color-lime-800-rgb: rgb(63 98 18);
--color-lime-800-hex: #3f6212;
--color-lime-900-rgb: rgb(54 83 20);
--color-lime-900-hex: #365314;

/* Green */
--color-green-50-rgb: rgb(240 253 244);
--color-green-50-hex: #f0fdf4;
--color-green-100-rgb: rgb(220 252 231);
--color-green-100-hex: #dcfce7;
--color-green-200-rgb: rgb(187 247 208);
--color-green-200-hex: #bbf7d0;
--color-green-300-rgb: rgb(134 239 172);
--color-green-300-hex: #86efac;
--color-green-400-rgb: rgb(74 222 128);
--color-green-400-hex: #4ade80;
--color-green-500-rgb: rgb(34 197 94);
--color-green-500-hex: #22c55e;
--color-green-600-rgb: rgb(22 163 74);
--color-green-600-hex: #16a34a;
--color-green-700-rgb: rgb(21 128 61);
--color-green-700-hex: #15803d;
--color-green-800-rgb: rgb(22 101 52);
--color-green-800-hex: #166534;
--color-green-900-rgb: rgb(20 83 45);
--color-green-900-hex: #14532d;

/* Emerald */
--color-emerald-50-rgb: rgb(236 253 245);
--color-emerald-50-hex: #ecfdf5;
--color-emerald-100-rgb: rgb(209 250 229);
--color-emerald-100-hex: #d1fae5;
--color-emerald-200-rgb: rgb(167 243 208);
--color-emerald-200-hex: #a7f3d0;
--color-emerald-300-rgb: rgb(110 231 183);
--color-emerald-300-hex: #6ee7b7;
--color-emerald-400-rgb: rgb(52 211 153);
--color-emerald-400-hex: #34d399;
--color-emerald-500-rgb: rgb(16 185 129);
--color-emerald-500-hex: #10b981;
--color-emerald-600-rgb: rgb(5 150 105);
--color-emerald-600-hex: #059669;
--color-emerald-700-rgb: rgb(4 120 87);
--color-emerald-700-hex: #047857;
--color-emerald-800-rgb: rgb(6 95 70);
--color-emerald-800-hex: #065f46;
--color-emerald-900-rgb: rgb(6 78 59);
--color-emerald-900-hex: #064e3b;

/* Teal */
--color-teal-50-rgb: rgb(240 253 250);
--color-teal-50-hex: #f0fdfa;
--color-teal-100-rgb: rgb(204 251 241);
--color-teal-100-hex: #ccfbf1;
--color-teal-200-rgb: rgb(153 246 228);
--color-teal-200-hex: #99f6e4;
--color-teal-300-rgb: rgb(94 234 212);
--color-teal-300-hex: #5eead4;
--color-teal-400-rgb: rgb(45 212 191);
--color-teal-400-hex: #2dd4bf;
--color-teal-500-rgb: rgb(20 184 166);
--color-teal-500-hex: #14b8a6;
--color-teal-600-rgb: rgb(13 148 136);
--color-teal-600-hex: #0d9488;
--color-teal-700-rgb: rgb(15 118 110);
--color-teal-700-hex: #0f766e;
--color-teal-800-rgb: rgb(17 94 89);
--color-teal-800-hex: #115e59;
--color-teal-900-rgb: rgb(19 78 74);
--color-teal-900-hex: #134e4a;

/* Cyan */
--color-cyan-50-rgb: rgb(236 254 255);
--color-cyan-50-hex: #ecfeff;
--color-cyan-100-rgb: rgb(207 250 254);
--color-cyan-100-hex: #cffafe;
--color-cyan-200-rgb: rgb(165 243 252);
--color-cyan-200-hex: #a5f3fc;
--color-cyan-300-rgb: rgb(103 232 249);
--color-cyan-300-hex: #67e8f9;
--color-cyan-400-rgb: rgb(34 211 238);
--color-cyan-400-hex: #22d3ee;
--color-cyan-500-rgb: rgb(6 182 212);
--color-cyan-500-hex: #06b6d4;
--color-cyan-600-rgb: rgb(8 145 178);
--color-cyan-600-hex: #0891b2;
--color-cyan-700-rgb: rgb(14 116 144);
--color-cyan-700-hex: #0e7490;
--color-cyan-800-rgb: rgb(21 94 117);
--color-cyan-800-hex: #155e75;
--color-cyan-900-rgb: rgb(22 78 99);
--color-cyan-900-hex: #164e63;

/* Sky */
--color-sky-50-rgb: rgb(240 249 255);
--color-sky-50-hex: #f0f9ff;
--color-sky-100-rgb: rgb(224 242 254);
--color-sky-100-hex: #e0f2fe;
--color-sky-200-rgb: rgb(186 230 253);
--color-sky-200-hex: #bae6fd;
--color-sky-300-rgb: rgb(125 211 252);
--color-sky-300-hex: #7dd3fc;
--color-sky-400-rgb: rgb(56 189 248);
--color-sky-400-hex: #38bdf8;
--color-sky-500-rgb: rgb(14 165 233);
--color-sky-500-hex: #0ea5e9;
--color-sky-600-rgb: rgb(2 132 199);
--color-sky-600-hex: #0284c7;
--color-sky-700-rgb: rgb(3 105 161);
--color-sky-700-hex: #0369a1;
--color-sky-800-rgb: rgb(7 89 133);
--color-sky-800-hex: #075985;
--color-sky-900-rgb: rgb(12 74 110);
--color-sky-900-hex: #0c4a6e;

/* Blue */
--color-blue-50-rgb: rgb(239 246 255);
--color-blue-50-hex: #eff6ff;
--color-blue-100-rgb: rgb(219 234 254);
--color-blue-100-hex: #dbeafe;
--color-blue-200-rgb: rgb(191 219 254);
--color-blue-200-hex: #bfdbfe;
--color-blue-300-rgb: rgb(147 197 253);
--color-blue-300-hex: #93c5fd;
--color-blue-400-rgb: rgb(96 165 250);
--color-blue-400-hex: #60a5fa;
--color-blue-500-rgb: rgb(59 130 246);
--color-blue-500-hex: #3b82f6;
--color-blue-600-rgb: rgb(37 99 235);
--color-blue-600-hex: #2563eb;
--color-blue-700-rgb: rgb(29 78 216);
--color-blue-700-hex: #1d4ed8;
--color-blue-800-rgb: rgb(30 64 175);
--color-blue-800-hex: #1e40af;
--color-blue-900-rgb: rgb(30 58 138);
--color-blue-900-hex: #1e3a8a;

/* Indigo */
--color-indigo-50-rgb: rgb(238 242 255);
--color-indigo-50-hex: #eef2ff;
--color-indigo-100-rgb: rgb(224 231 255);
--color-indigo-100-hex: #e0e7ff;
--color-indigo-200-rgb: rgb(199 210 254);
--color-indigo-200-hex: #c7d2fe;
--color-indigo-300-rgb: rgb(165 180 252);
--color-indigo-300-hex: #a5b4fc;
--color-indigo-400-rgb: rgb(129 140 248);
--color-indigo-400-hex: #818cf8;
--color-indigo-500-rgb: rgb(99 102 241);
--color-indigo-500-hex: #6366f1;
--color-indigo-600-rgb: rgb(79 70 229);
--color-indigo-600-hex: #4f46e5;
--color-indigo-700-rgb: rgb(67 56 202);
--color-indigo-700-hex: #4338ca;
--color-indigo-800-rgb: rgb(55 48 163);
--color-indigo-800-hex: #3730a3;
--color-indigo-900-rgb: rgb(49 46 129);
--color-indigo-900-hex: #312e81;

/* Violet */
--color-violet-50-rgb: rgb(245 243 255);
--color-violet-50-hex: #f5f3ff;
--color-violet-100-rgb: rgb(237 233 254);
--color-violet-100-hex: #ede9fe;
--color-violet-200-rgb: rgb(221 214 254);
--color-violet-200-hex: #ddd6fe;
--color-violet-300-rgb: rgb(196 181 253);
--color-violet-300-hex: #c4b5fd;
--color-violet-400-rgb: rgb(167 139 250);
--color-violet-400-hex: #a78bfa;
--color-violet-500-rgb: rgb(139 92 246);
--color-violet-500-hex: #8b5cf6;
--color-violet-600-rgb: rgb(124 58 237);
--color-violet-600-hex: #7c3aed;
--color-violet-700-rgb: rgb(109 40 217);
--color-violet-700-hex: #6d28d9;
--color-violet-800-rgb: rgb(91 33 182);
--color-violet-800-hex: #5b21b6;
--color-violet-900-rgb: rgb(76 29 149);
--color-violet-900-hex: #4c1d95;

/* Purple */
--color-purple-50-rgb: rgb(250 245 255);
--color-purple-50-hex: #faf5ff;
--color-purple-100-rgb: rgb(243 232 255);
--color-purple-100-hex: #f3e8ff;
--color-purple-200-rgb: rgb(233 213 255);
--color-purple-200-hex: #e9d5ff;
--color-purple-300-rgb: rgb(216 180 254);
--color-purple-300-hex: #d8b4fe;
--color-purple-400-rgb: rgb(192 132 252);
--color-purple-400-hex: #c084fc;
--color-purple-500-rgb: rgb(168 85 247);
--color-purple-500-hex: #a855f7;
--color-purple-600-rgb: rgb(147 51 234);
--color-purple-600-hex: #9333ea;
--color-purple-700-rgb: rgb(126 34 206);
--color-purple-700-hex: #7e22ce;
--color-purple-800-rgb: rgb(107 33 168);
--color-purple-800-hex: #6b21a8;
--color-purple-900-rgb: rgb(88 28 135);
--color-purple-900-hex: #581c87;

/* Fuchsia */
--color-fuchsia-50-rgb: rgb(253 244 255);
--color-fuchsia-50-hex: #fdf4ff;
--color-fuchsia-100-rgb: rgb(250 232 255);
--color-fuchsia-100-hex: #fae8ff;
--color-fuchsia-200-rgb: rgb(245 208 254);
--color-fuchsia-200-hex: #f5d0fe;
--color-fuchsia-300-rgb: rgb(240 171 252);
--color-fuchsia-300-hex: #f0abfc;
--color-fuchsia-400-rgb: rgb(232 121 249);
--color-fuchsia-400-hex: #e879f9;
--color-fuchsia-500-rgb: rgb(217 70 239);
--color-fuchsia-500-hex: #d946ef;
--color-fuchsia-600-rgb: rgb(192 38 211);
--color-fuchsia-600-hex: #c026d3;
--color-fuchsia-700-rgb: rgb(162 28 175);
--color-fuchsia-700-hex: #a21caf;
--color-fuchsia-800-rgb: rgb(134 25 143);
--color-fuchsia-800-hex: #86198f;
--color-fuchsia-900-rgb: rgb(112 26 117);
--color-fuchsia-900-hex: #701a75;

/* Pink */
--color-pink-50-rgb: rgb(253 242 248);
--color-pink-50-hex: #fdf2f8;
--color-pink-100-rgb: rgb(252 231 243);
--color-pink-100-hex: #fce7f3;
--color-pink-200-rgb: rgb(251 207 232);
--color-pink-200-hex: #fbcfe8;
--color-pink-300-rgb: rgb(249 168 212);
--color-pink-300-hex: #f9a8d4;
--color-pink-400-rgb: rgb(244 114 182);
--color-pink-400-hex: #f472b6;
--color-pink-500-rgb: rgb(236 72 153);
--color-pink-500-hex: #ec4899;
--color-pink-600-rgb: rgb(219 39 119);
--color-pink-600-hex: #db2777;
--color-pink-700-rgb: rgb(190 24 93);
--color-pink-700-hex: #be185d;
--color-pink-800-rgb: rgb(157 23 77);
--color-pink-800-hex: #9d174d;
--color-pink-900-rgb: rgb(131 24 67);
--color-pink-900-hex: #831843;

/* Rose */
--color-rose-50-rgb: rgb(255 241 242);
--color-rose-50-hex: #fff1f2;
--color-rose-100-rgb: rgb(255 228 230);
--color-rose-100-hex: #ffe4e6;
--color-rose-200-rgb: rgb(254 205 211);
--color-rose-200-hex: #fecdd3;
--color-rose-300-rgb: rgb(253 164 175);
--color-rose-300-hex: #fda4af;
--color-rose-400-rgb: rgb(251 113 133);
--color-rose-400-hex: #fb7185;
--color-rose-500-rgb: rgb(244 63 94);
--color-rose-500-hex: #f43f5e;
--color-rose-600-rgb: rgb(225 29 72);
--color-rose-600-hex: #e11d48;
--color-rose-700-rgb: rgb(190 18 60);
--color-rose-700-hex: #be123c;
--color-rose-800-rgb: rgb(159 18 57);
--color-rose-800-hex: #9f1239;
--color-rose-900-rgb: rgb(136 19 55);
--color-rose-900-hex: #881337;

@ErickPetru
Copy link
Author

Generating a new 950 shade for each color, and outputting a reversed color palette for automatic dark themes:

function getLowestMiddleHighest(rgbArray) {
  let highest = { val: -1, index: -1 };
  let lowest = { val: Infinity, index: -1 };

  rgbArray.forEach((val, index) => {
    if (val > highest.val) highest = { val: val, index: index };
    if (val < lowest.val) lowest = { val: val, index: index };
  });

  if (lowest.index === highest.index) {
    lowest.index = highest.index + 1;
  }

  const middle = { index: 3 - highest.index - lowest.index };
  middle.val = rgbArray[middle.index];
  return [lowest, middle, highest];
}

function darken(color) {
  const rgbArray = [...color.rgb.matchAll(/(\d+),? (\d+),? (\d+)/gi)][0]
    .slice(1)
    .map((n) => parseInt(n, 10));

  const [lowest, middle, highest] = getLowestMiddleHighest(rgbArray);

  if (highest.val === 0) return color;

  const result = [];

  const deviation = highest.val > 50 ? 30.5 : 12.5;
  result[highest.index] = highest.val - Math.min(highest.val, deviation);
  const decreaseFraction = (highest.val - result[highest.index]) / highest.val;
  result[middle.index] = middle.val - middle.val * decreaseFraction;
  result[lowest.index] = lowest.val - lowest.val * decreaseFraction;

  return {
    rgb: `${result.map((n) => Math.round(n).toString(10)).join(" ")}`,
    hex: `#${result.map((n) => n.toString(16).padStart(2, "0")).join("")}`,
  };
}

const palette = [
  ...document
    .querySelector("#content-wrapper > .grid.grid-cols-1")
    .querySelectorAll(".flex.flex-col.space-y-3"),
]
  .map((row) => ({
    color: row
      .querySelector(".w-16.shrink-0")
      .textContent.toString()
      .toLowerCase(),
    shades: [...row.querySelectorAll(".relative.flex > .cursor-pointer")].map(
      (color) => ({
        shade: color.querySelector(".font-medium").textContent.toString(),
        rgb: color
          .querySelector(".h-10")
          .style.backgroundColor.replace(/[rgb|(|)|,]/g, "")
          .replace(")", ""),
        hex: color
          .querySelector(".font-mono")
          .textContent.toString()
          .toLowerCase(),
      })
    ),
  }))
  .reduce(
    (acc, curr) => ({
      ...acc,
      [curr.color]: curr.shades.reduce(
        (acc2, curr2) => ({
          ...acc2,
          [curr2.shade]: { rgb: curr2.rgb, hex: curr2.hex },
        }),
        { 950: darken(curr.shades[curr.shades.length - 1]) }
      ),
    }),
    {}
  );

console.log(
  Object.keys(palette)
    .map(
      (color) =>
        `/* ${color[0].toUpperCase()}${color.substring(1)} */\n` +
        Object.keys(palette[color])
          .map(
            (shade) =>
              `--color-${color}-${shade}: rgb(${palette[color][
                shade
              ].rgb.replaceAll(" ", ", ")});`
          )
          .join("\n")
    )
    .join("\n\n")
);

const reversedShade = {
  50: "950",
  100: "900",
  200: "800",
  300: "700",
  400: "600",
  500: "500",
  600: "400",
  700: "300",
  800: "200",
  900: "100",
  950: "50",
};

console.log(
  Object.keys(palette)
    .map(
      (color) =>
        `/* ${color[0].toUpperCase()}${color.substring(1)} */\n` +
        Object.keys(palette[color])
          .map(
            (shade) =>
              `--color-${color}-${reversedShade[shade]}: rgb(${palette[color][
                shade
              ].rgb.replaceAll(" ", ", ")});`
          )
          .reverse()
          .join("\n")
    )
    .join("\n\n")
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment