@mintlify/common
Version:
Commonly shared code within Mintlify
101 lines (100 loc) • 5.42 kB
JavaScript
import { normal } from 'color-blend';
import hexRgb from 'hex-rgb';
export const hexToRgbString = (hex) => {
try {
const rgb = hexRgb(hex);
return `${rgb.red} ${rgb.green} ${rgb.blue}`;
}
catch (_a) {
return '0 0 0';
}
};
export const rgbToRgbString = (rgb) => {
return `${rgb.r} ${rgb.g} ${rgb.b}`;
};
export const rgbToHex = (rgb) => {
return `#${rgb.r.toString(16).padStart(2, '0')}${rgb.g.toString(16).padStart(2, '0')}${rgb.b
.toString(16)
.padStart(2, '0')}`;
};
export const generateDynamicBackgroundDarkColor = (primaryLight, backgroundDark, isHex) => {
if (backgroundDark) {
return isHex ? backgroundDark : hexToRgbString(backgroundDark);
}
const combinedBackground = combineColors('#09090b', 1, primaryLight, 0.02);
return isHex ? rgbToHex(combinedBackground) : rgbToRgbString(combinedBackground);
};
export const combineColors = (color1, color1Opacity, color2, color2Opacity) => {
const color1Rgb = hexRgb(color1);
const color2Rgb = hexRgb(color2);
return normal({ r: color1Rgb.red, g: color1Rgb.green, b: color1Rgb.blue, a: color1Opacity }, { r: color2Rgb.red, g: color2Rgb.green, b: color2Rgb.blue, a: color2Opacity });
};
export const generateDynamicGrayRgb = (primaryRgb, grayColor) => {
const grayRgb = hexRgb(grayColor);
const combinedGray = normal({ r: primaryRgb.red, g: primaryRgb.green, b: primaryRgb.blue, a: 0.6 }, { r: grayRgb.red, g: grayRgb.green, b: grayRgb.blue, a: 0.95 });
return rgbToRgbString(combinedGray);
};
export const generateDynamicGrayScale = (primaryColor) => {
const neutralScale = {
'50': '#fafafa',
'100': '#f5f5f5',
'200': '#e5e5e5',
'300': '#d4d4d4',
'400': '#a3a3a3',
'500': '#737373',
'600': '#525252',
'700': '#404040',
'800': '#262626',
'900': '#171717',
'950': '#0a0a0a',
};
const primaryRgb = hexRgb(primaryColor);
return {
'50': generateDynamicGrayRgb(primaryRgb, neutralScale[50]),
'100': generateDynamicGrayRgb(primaryRgb, neutralScale[100]),
'200': generateDynamicGrayRgb(primaryRgb, neutralScale[200]),
'300': generateDynamicGrayRgb(primaryRgb, neutralScale[300]),
'400': generateDynamicGrayRgb(primaryRgb, neutralScale[400]),
'500': generateDynamicGrayRgb(primaryRgb, neutralScale[500]),
'600': generateDynamicGrayRgb(primaryRgb, neutralScale[600]),
'700': generateDynamicGrayRgb(primaryRgb, neutralScale[700]),
'800': generateDynamicGrayRgb(primaryRgb, neutralScale[800]),
'900': generateDynamicGrayRgb(primaryRgb, neutralScale[900]),
'950': generateDynamicGrayRgb(primaryRgb, neutralScale[950]),
};
};
/**
* The function `convertColorsToRgbWithCommas` takes a color string with spaces and converts it to RGB
* format with commas.
* @param {string} color - The `convertColorsToRgbWithCommas` function takes a color string as input
* and converts it to RGB format with commas separating the values. For example, if the input color is
* "255 0 128", the function will return "255, 0, 128".
*/
export const convertColorsToRgbWithCommas = (color) => {
return color.split(' ').join(', ');
};
export function getBackgroundColors(docsConfig) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
const primaryColor = (docsConfig === null || docsConfig === void 0 ? void 0 : docsConfig.colors.primary) || '#16A34A';
const primaryLight = (_a = docsConfig === null || docsConfig === void 0 ? void 0 : docsConfig.colors.light) !== null && _a !== void 0 ? _a : '#4ADE80';
if ((docsConfig === null || docsConfig === void 0 ? void 0 : docsConfig.theme) === 'linden') {
docsConfig.background = Object.assign(Object.assign({}, docsConfig.background), { color: {
light: ((_c = (_b = docsConfig.background) === null || _b === void 0 ? void 0 : _b.color) === null || _c === void 0 ? void 0 : _c.light) ||
rgbToHex(combineColors('#FFFFFF', 1, primaryColor, 0.03)),
dark: ((_e = (_d = docsConfig.background) === null || _d === void 0 ? void 0 : _d.color) === null || _e === void 0 ? void 0 : _e.dark) ||
rgbToHex(combineColors('#09090B', 1, primaryLight, 0.03)),
} });
}
const lightHex = (_h = (_g = (_f = docsConfig === null || docsConfig === void 0 ? void 0 : docsConfig.background) === null || _f === void 0 ? void 0 : _f.color) === null || _g === void 0 ? void 0 : _g.light) !== null && _h !== void 0 ? _h : '#ffffff';
const light = hexToRgbString(lightHex);
const dark = generateDynamicBackgroundDarkColor(primaryLight, (_k = (_j = docsConfig === null || docsConfig === void 0 ? void 0 : docsConfig.background) === null || _j === void 0 ? void 0 : _j.color) === null || _k === void 0 ? void 0 : _k.dark);
const darkHex = generateDynamicBackgroundDarkColor(primaryLight, (_m = (_l = docsConfig === null || docsConfig === void 0 ? void 0 : docsConfig.background) === null || _l === void 0 ? void 0 : _l.color) === null || _m === void 0 ? void 0 : _m.dark, true);
const background = (_o = docsConfig === null || docsConfig === void 0 ? void 0 : docsConfig.thumbnails) === null || _o === void 0 ? void 0 : _o.background;
return {
light,
dark,
lightHex,
darkHex,
background,
};
}