UNPKG

@thangk/easythemer

Version:

Easily generate shades from a colour palette for use in your app

112 lines 4.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mergeObjects = exports.generateShades = void 0; const constants_1 = require("./constants"); const converters_1 = require("./converters"); const validators_1 = require("./validators"); function limiter(input, limitStringArray) { const mode = limitStringArray.split("-")[0]; const limit = parseInt(limitStringArray.split("-")[1]); // for max mode if (mode === "max") return input > limit ? limit : input; if (mode === "min") return input < limit ? limit : input; // for neither mode return input; } function getChannelValue({ shadeFactor, HSL_channel, HSL_letter, workingObj }) { const { upperboundDivider, lowerboundDivider, upperboundPadding, lowerboundPadding } = workingObj; let HSL_letter_MAX; switch (HSL_letter) { case "h": HSL_letter_MAX = constants_1.HSL_LIMITS.MAX_H; break; case "s": HSL_letter_MAX = constants_1.HSL_LIMITS.MAX_S; break; case "l": HSL_letter_MAX = constants_1.HSL_LIMITS.MAX_L; break; default: HSL_letter_MAX = 0; break; } const upperbound = HSL_letter_MAX - HSL_channel; const lowerbound = HSL_channel; const upperboundStep = upperboundDivider && upperboundPadding ? Math.round((upperbound / upperboundDivider) * upperboundPadding) : 0; const lowerboundStep = lowerboundDivider && lowerboundPadding ? Math.round((lowerbound / lowerboundDivider) * lowerboundPadding) : 0; if (shadeFactor > 0) HSL_channel = limiter(HSL_channel + upperboundStep * shadeFactor, `max-${HSL_letter_MAX - 5}`); if (shadeFactor < 0) HSL_channel = limiter(HSL_channel + lowerboundStep * shadeFactor, `min-${0 + 15}`); return HSL_channel; } function getBoundSteps2(HSLInput, inputShadeOption, shadeFactor) { const hslArray = ["h", "s", "l"]; let hslArrayIndex = 0; inputShadeOption.channelParams?.forEach((channelParam) => { for (const [key, value] of Object.entries(channelParam)) { if (hslArray[hslArrayIndex] === value) { // use default bounds if (!channelParam.useBounds) { const mergedChannelParams = Object.assign(constants_1.defaultChannelParams, channelParam); HSLInput[hslArray[hslArrayIndex]] = getChannelValue({ shadeFactor, HSL_channel: HSLInput[hslArray[hslArrayIndex]], HSL_letter: hslArray[hslArrayIndex], workingObj: mergedChannelParams, }); } // use custom bounds if (channelParam.useBounds) { HSLInput[hslArray[hslArrayIndex]] = getChannelValue({ shadeFactor, HSL_channel: HSLInput[hslArray[hslArrayIndex]], HSL_letter: hslArray[hslArrayIndex], workingObj: constants_1.defaultChannelParams, }); } } } hslArrayIndex++; }); return HSLInput; } function generateShades(inputOption, inputShadeFactorsSet) { let shadeSet = {}; const { hex } = inputOption; const workingShadeFactorsSet = inputShadeFactorsSet ? inputShadeFactorsSet : constants_1.defaultShadeFactorsSet; for (const [key, value] of Object.entries(workingShadeFactorsSet.shadeFactors)) { if (!value) { shadeSet[key] = hex; continue; } const { h: hh, s: ss, l: ll } = (0, converters_1.HexToHSL)(hex); const { h, s, l } = getBoundSteps2({ h: hh, s: ss, l: ll }, inputOption, value); shadeSet[key] = (0, converters_1.HSLToHex)({ type: constants_1.TYPE.HSL, h, s, l }); } return shadeSet; } exports.generateShades = generateShades; function mergeObjects(object1, object2) { if (!object2) return; const workingObject = structuredClone(object1); console.log("workingObject", workingObject); for (const [key, value] of Object.entries(workingObject)) { const isValueAnObject = new validators_1.ValidateIsObject(value); const valueKeysLen = Object.keys(workingObject[key]).length; console.log(`${key}: ${value}`); console.log("isValueAnObject", isValueAnObject); console.log("valueKeysLen", valueKeysLen); if (valueKeysLen && isValueAnObject.getResult) { workingObject[key] = mergeObjects(workingObject[key], object2[key]); continue; } workingObject[key] = object2[key] ?? workingObject[key]; } return workingObject; } exports.mergeObjects = mergeObjects; //# sourceMappingURL=utils.js.map