tailwind-colorize-plugin
Version:
The fluent and expressive way to manipulate colors in Tailwind CSS.
111 lines • 4.01 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useConfig = exports.useColors = void 0;
const color_1 = __importDefault(require("color"));
const get_js_1 = __importDefault(require("lodash/get.js"));
const toColorValue_js_1 = __importDefault(require("tailwindcss/lib/util/toColorValue.js"));
const resolveConfig_1 = __importDefault(require("tailwindcss/resolveConfig"));
const grammar_js_1 = __importDefault(require("../grammar.js"));
function useColors(colors) {
return useConfig({
theme: {
colors
}
});
}
exports.useColors = useColors;
function useConfig(config) {
config = (0, resolveConfig_1.default)(config);
function transformArgument(arg) {
if (Array.isArray(arg)) {
return transform(arg);
}
switch (arg.id) {
case 'ShortHexCode':
case 'LongHexCode':
case 'RgbFunction':
case 'HslFunction':
return (0, color_1.default)(arg.code);
case 'LiteralValue':
return arg.value;
case 'ThemeColor':
return themeColor(arg.name, arg.weight);
}
}
function transform([subject, calls]) {
return calls.reduce((carry, call) => {
const args = call.args.map(transformArgument)
.filter((value) => value !== undefined);
if (typeof carry[call.name] !== 'function') {
throw new Error(`${call.name} is not a method on ${carry}`);
}
return carry[call.name](...args);
}, transformArgument(subject));
}
function parse(value) {
value = (0, toColorValue_js_1.default)(value);
try {
return transform(grammar_js_1.default.parse(value)).rgb().toString();
}
catch (e) {
return value;
}
}
;
// function parse(subject) {
// return parseString(String(subject));
// }
function themeColor(path, weight, defaultValue) {
const parsedPath = [path, weight].filter(value => !!value).join('.');
const color = (0, get_js_1.default)(config?.theme.colors, parsedPath, defaultValue);
if (!color) {
return;
}
return (0, color_1.default)(color);
}
function expand(values, interval = 10) {
if (!values) {
values = Object.keys(config?.theme.colors);
}
function decimal(i, start) {
i = parseInt(i);
start = parseInt(start);
return start < 100 ? i / 100 : (i - start) / 100;
}
function walk([start, color], remaining) {
const [next] = remaining, items = [
[start, color]
];
for (let i = parseInt(start) + interval; i < parseInt(next[0]); i += interval) {
items.push([i, () => (0, color_1.default)(color).mix((0, color_1.default)(next[1]), decimal(i, start)).rgb().toString()]);
}
const unordered = items.reduce((carry, [i, item]) => Object.assign(carry, { [i]: item }), {});
const ordered = Object.keys(unordered).sort().reduce((obj, key) => {
obj[key] = unordered[key];
return obj;
}, {});
return ordered;
}
return Object.fromEntries([].concat(values)
.map(value => [value, Object.entries(config?.theme.colors[value])])
.map(([key, colors]) => {
const subject = {};
while (colors.length > 1) {
Object.assign(subject, walk(colors.shift(), colors));
}
return [key, subject];
}));
}
return {
config,
expand,
parse,
themeColor,
transform
};
}
exports.useConfig = useConfig;
//# sourceMappingURL=lib.js.map