UNPKG

lottie-color-manager

Version:

A library for managing colors in Lottie animations

63 lines (62 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ColorUtils = void 0; /** * Utility functions for working with colors in Lottie animations */ class ColorUtils { /** * Converts a HEX color string to Lottie color format [r, g, b, a] */ static hexToLottieColor(hex, alpha = 1) { // Remove # if present const cleanHex = hex.startsWith('#') ? hex.substring(1) : hex; // Parse color components const r = parseInt(cleanHex.substring(0, 2), 16) / 255; const g = parseInt(cleanHex.substring(2, 4), 16) / 255; const b = parseInt(cleanHex.substring(4, 6), 16) / 255; // Return as Lottie color array return [r, g, b, alpha]; } /** * Converts a Lottie color to HEX format */ static lottieColorToHex(color) { // Convert each component to hex const r = Math.round(color[0] * 255).toString(16).padStart(2, '0'); const g = Math.round(color[1] * 255).toString(16).padStart(2, '0'); const b = Math.round(color[2] * 255).toString(16).padStart(2, '0'); return `#${r}${g}${b}`; } /** * Calculates the distance between two colors */ static calculateColorDistance(color1, color2) { return Math.sqrt(Math.pow(color1[0] - color2[0], 2) + Math.pow(color1[1] - color2[1], 2) + Math.pow(color1[2] - color2[2], 2)); } /** * Checks if two colors are similar within a given tolerance */ static isColorSimilar(color1, color2, tolerance = 0.1) { return (Math.abs(color1[0] - color2[0]) <= tolerance && Math.abs(color1[1] - color2[1]) <= tolerance && Math.abs(color1[2] - color2[2]) <= tolerance); } /** * Calculates the brightness of a color (0-1) */ static calculateBrightness(color) { return 0.299 * color[0] + 0.587 * color[1] + 0.114 * color[2]; } /** * Check if two HEX colors are similar based on tolerance */ static isHexColorSimilar(color1, color2, tolerance) { const lottieColor1 = this.hexToLottieColor(color1); const lottieColor2 = this.hexToLottieColor(color2); return this.isColorSimilar(lottieColor1, lottieColor2, tolerance); } } exports.ColorUtils = ColorUtils;