Add comments, tune color darkening

This commit is contained in:
2023-11-16 17:47:30 -06:00
parent d511e5966d
commit a05ef4bb49

View File

@@ -1,6 +1,7 @@
const colors = require('tailwindcss/colors'); const colors = require('tailwindcss/colors');
const Color = require('color'); const Color = require('color');
// Range generation function
const range = (lower, upper, step) => { const range = (lower, upper, step) => {
return Array.from( return Array.from(
new Array(Math.floor(upper / step - lower / step) + 1), new Array(Math.floor(upper / step - lower / step) + 1),
@@ -9,32 +10,38 @@ const range = (lower, upper, step) => {
}; };
function generateColor(base, max, min, step) { function generateColor(base, max, min, step) {
// The heaviest weight color defined in the base palette
const baseMax = Math.max(...Object.keys(base).map((v) => parseInt(v))); const baseMax = Math.max(...Object.keys(base).map((v) => parseInt(v)));
// Color object of baseMax
const baseMaxColor = Color(base[baseMax]);
// Total number of steps from baseMax to max by step
const totalSteps = (max - baseMax) / step;
// Generate step colors, or for colors beyond baseMax, a darkened color
const generated = range(min, max, step) const generated = range(min, max, step)
// Don't try to redefine colors or anything
.filter((v) => base[v] === undefined) .filter((v) => base[v] === undefined)
.map((weight) => { .map((weight) => {
// Determine if we're mixing or darkening
const isHighWeight = weight + step > baseMax; const isHighWeight = weight + step > baseMax;
if (isHighWeight) { if (isHighWeight) {
const baseDarkest = Color(base[baseMax]); // The Nth step from baseMax on the way to max
const weightDifference = weight - baseMax; const baseMaxStep = (weight - baseMax) / step;
const darkened = baseDarkest.darken(0.005 * weightDifference); const darkened = baseMaxColor.darken(baseMaxStep / totalSteps);
// console.log({
// dark: baseDarkest.hex(),
// darkLight: baseDarkest.luminosity(),
// weightDifference,
// darkened: darkened.hex()
// });
return [weight, darkened.hex()]; return [weight, darkened.hex()];
} }
// Get lighter and darker colors, then mix the two for an intermediary color
const lighter = Color(base[weight - step]); const lighter = Color(base[weight - step]);
const darker = Color(base[weight + step]); const darker = Color(base[weight + step]);
return [weight, lighter.mix(darker).hex()]; return [weight, lighter.mix(darker).hex()];
}); });
// Build a new object, skip null entries
return Object.fromEntries(generated.filter((v) => v != null)); return Object.fromEntries(generated.filter((v) => v != null));
} }
// Colors that only have one value, or are special/meta. We don't want to try and generate them.
const skipColors = new Set([ const skipColors = new Set([
'inherit', 'inherit',
'transparent', 'transparent',
@@ -48,6 +55,7 @@ const skipColors = new Set([
'blueGray' 'blueGray'
]); ]);
// Invoke color generation on each color that isn't meta/singular
const generatedColors = Object.fromEntries( const generatedColors = Object.fromEntries(
Object.entries(colors) Object.entries(colors)
.filter(([key, _]) => !skipColors.has(key)) .filter(([key, _]) => !skipColors.has(key))
@@ -56,14 +64,13 @@ const generatedColors = Object.fromEntries(
}) })
); );
console.log({ ...generatedColors });
/** @type {import('tailwindcss').Config} */ /** @type {import('tailwindcss').Config} */
module.exports = { module.exports = {
content: ['./src/**/*.{js,ts,jsx,tsx}'], content: ['./src/**/*.{js,ts,jsx,tsx}'],
theme: { theme: {
extend: { extend: {
colors: { colors: {
// Expand, allow for custom colors to be mixed with generated ones
...generatedColors ...generatedColors
} }
} }