@nish1896/rhf-mui-components
Version:
A suite of 20+ production-ready react-hook-form components built with material-ui. Fully typed, tree-shakable, and optimized for enterprise-grade forms.
21 lines (20 loc) • 1.28 kB
JavaScript
//#region src/utils/color.ts
/**
* Converts a color object to a string in `rgb` or `hsv` format.
*
* @param color - An object containing values as `rgb` or `hsv` components.
* @param type - `'rgb'` or `'hsv'` to specify the color model.
* @param excludeAlpha - Whether to exclude the alpha value in the output.
* @returns The formatted color string.
*/
function colorToString(color, excludeAlpha) {
const isRGBColor = color.r !== void 0 && color.g !== void 0 && color.b !== void 0;
const isHSVColor = color.h !== void 0 && color.s !== void 0 && color.v !== void 0;
if (!isRGBColor && !isHSVColor) throw new Error("Invalid color object or type");
const shouldExcludeAlpha = (color.a === void 0 || color.a === 1) && excludeAlpha;
if (isRGBColor) return shouldExcludeAlpha ? `rgb(${Math.round(color.r)}, ${Math.round(color.g)}, ${Math.round(color.b)})` : `rgba(${Math.round(color.r)}, ${Math.round(color.g)}, ${Math.round(color.b)}, ${color.a ?? 1})`;
if (isHSVColor) return shouldExcludeAlpha ? `hsl(${Math.round(color.h)} ${Math.round(color.s)}% ${Math.round(color.v)}%)` : `hsl(${Math.round(color.h)} ${Math.round(color.s)}% ${Math.round(color.v)}% / ${color.a ?? 1})`;
throw new Error("Unexpected error processing the color object");
}
//#endregion
export { colorToString };