material-chalk
Version:
Generate beautiful colors from namespaces based on color theory
40 lines (39 loc) • 1.74 kB
JavaScript
/**
* File for opinionated ways to format common cases
*/
import { Hct, argbFromHex } from "@material/material-color-utilities";
import { Format, TONE_RANGE, colorToNamespace, createMaterial, } from "./core.mjs";
export function chainedMessage(chalk, namespace, ...s) {
const header = [];
const chalkFormat = Format.Chalk(chalk);
if (typeof namespace === "string") {
const fullNamespace = createMaterial(namespace).formatAs(chalkFormat);
return `${fullNamespace.inverse(namespace)}: ${fullNamespace(...s)}`;
}
for (let i = 0; i < namespace.length; i++) {
const currStep = createMaterial(namespace[i]);
const mix = i + 1 < namespace.length ? currStep.subMaterial(namespace[i + 1]) : null;
const baseChalk = currStep.formatAs(chalkFormat);
header.push(i === namespace.length - 1 || i === 0
? baseChalk.inverse(namespace[i])
: baseChalk(namespace[i]));
if (mix != null) {
header.push(mix.formatAs(chalkFormat)("-"));
}
}
const fullNamespace = createMaterial(namespace[namespace.length - 1]).formatAs(chalkFormat);
return `${header.join("")}: ${fullNamespace(...s)}`;
}
export function matchColor(color) {
const desiredColor = (() => {
const desiredColor = typeof color === "string" ? Hct.fromInt(argbFromHex(color)) : color;
if (desiredColor.tone < TONE_RANGE.Min) {
return Hct.from(desiredColor.hue, desiredColor.chroma, TONE_RANGE.Min);
}
if (desiredColor.tone > TONE_RANGE.Max) {
return Hct.from(desiredColor.hue, desiredColor.chroma, TONE_RANGE.Max);
}
return desiredColor;
})();
return colorToNamespace(desiredColor);
}