lottie-color-manager
Version:
A library for managing colors in Lottie animations
57 lines (56 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LottieColorManager = void 0;
const core_1 = require("./core");
const analyzers_1 = require("./analyzers");
const transformers_1 = require("./transformers");
/**
* Main entry point for working with Lottie colors
*/
class LottieColorManager {
static changeColors(lottieData, colorMappings) {
core_1.Logger.info('Changing colors in Lottie animation');
try {
// Extract colors from the animation
const colors = analyzers_1.ColorExtractor.extractColors(lottieData);
// Create a mapping of original Lottie colors to new Lottie colors
const colorTransformations = [];
// Process each color in the animation
colors.forEach(color => {
// Convert Lottie color to HEX for comparison
const hexColor = core_1.ColorUtils.lottieColorToHex(color);
// Find matching color mapping based on tolerance
for (const mapping of colorMappings) {
if (core_1.ColorUtils.isHexColorSimilar(hexColor, mapping.baseColor, mapping.tolerance)) {
// Add to transformations list
colorTransformations.push([
color,
core_1.ColorUtils.hexToLottieColor(mapping.newColor),
]);
break;
}
}
});
// Apply color changes
return transformers_1.ColorTransformer.applyColorTransformations(lottieData, colorTransformations);
}
catch (error) {
core_1.Logger.error('Error changing colors:', error);
// Return the original data in case of error
return lottieData;
}
}
/**
* Analyze colors in a Lottie animation
* Useful for debugging or understanding the color structure
*/
static analyzeColors(lottieData) {
const colors = analyzers_1.ColorExtractor.extractColors(lottieData);
// Convert to hex for better readability
const uniqueColors = colors.map(c => core_1.ColorUtils.lottieColorToHex(c));
return {
uniqueColors,
};
}
}
exports.LottieColorManager = LottieColorManager;