import { ILottieProperty , IRGBColor , LordiconFeature , LottieColor } from '../interfaces.js' ; import { handleColor } from './colors.js' ; import { has , get , set } from './utils.js' ; /**
 * Scale factor for supported slider properties.
 */ const LORDICON_SCALE = 50 ; function componentToHex ( c ) { var hex = c . toString ( 16 ) ; return hex . length == 1 ? '0' + hex : hex ; } param value returns export function rgbToHex ( value ) { return ( '#' + componentToHex ( value . r ) + componentToHex ( value . g ) + componentToHex ( value . b ) ) ; } param hex returns export function hexToRgb ( hex ) { let data = parseInt ( hex [ 0 ] != '#' ? hex : hex . substring ( 1 ) , 16 ) ; return { r : ( data >> 16 ) & 255 , g : ( data >> 8 ) & 255 , b : data & 255 , } ; } param n returns export function toUnitVector ( n ) { return Math . round ( ( n / 255 ) * 1000 ) / 1000 ; } param n returns export function fromUnitVector ( n ) { return Math . round ( n * 255 ) ; } param hex returns export function hexToLottieColor ( hex ) { const { r , g , b } = hexToRgb ( hex ) ; return [ toUnitVector ( r ) , toUnitVector ( g ) , toUnitVector ( b ) ] ; } param value returns export function lottieColorToHex ( value ) { const color = { r : fromUnitVector ( value [ 0 ] ) , g : fromUnitVector ( value [ 1 ] ) , b : fromUnitVector ( value [ 2 ] ) , } ; return rgbToHex ( color ) ; } param data param lottieInstance returns export function allProperties ( data , lottieInstance = false ) { const result = [ ] ; if ( ! data || ! data . layers ) { return result ; } for ( const [ layerIndex , layer ] of Object . entries ( data . layers ) as any ) { if ( ! layer . nm ) { continue ; } if ( ! layer . nm . toLowerCase ( ) . includes ( 'change' ) ) { continue ; } if ( ! layer . ef ) { continue ; } for ( const [ fieldIndex , field ] of Object . entries ( layer . ef ) as any ) { const subpath = 'ef.0.v.k' ; let path ; if ( lottieInstance ) { path = `renderer.elements.${ layerIndex }.effectsManager.effectElements.${ fieldIndex }.effectElements.0.p.v` ; } else { path = `layers.${ layerIndex }.ef.${ fieldIndex }.${ subpath }` ; } const hasValue = has ( field , subpath ) ; if ( ! hasValue ) { continue ; } const value = get ( field , subpath ) ; let type = 'unkown' ; if ( field . mn === 'ADBE Color Control' ) { type = 'color' ; } else if ( field . mn === 'ADBE Slider Control' ) { type = 'slider' ; } else if ( field . mn === 'ADBE Point Control' ) { type = 'point' ; } else if ( field . mn === 'ADBE Checkbox Control' ) { type = 'checkbox' ; } if ( type === 'unkown' ) { continue ; } const name = field . nm ; result . push ( { name , path , value , type , } ) ; } } return result ; } param data param properties param name export function resetColor ( data , properties , name ) { for ( const field of properties ) { if ( field . type !== 'color' || field . name . toLowerCase ( ) !== name . toLowerCase ( ) ) { continue ; } set ( data , field . path , field . value ) ; } } param data param properties param name param value export function updateColor ( data , properties , name , value ) { for ( const field of properties ) { if ( field . type !== 'color' || field . name . toLowerCase ( ) !== name . toLowerCase ( ) ) { continue ; } if ( typeof value === 'object' ) { if ( 'r' in value && 'g' in value && 'b' in value ) { set ( data , field . path , [ toUnitVector ( value . r ) , toUnitVector ( value . g ) , toUnitVector ( value . b ) ] ) ; } else if ( Array . isArray ( value ) ) { set ( data , field . path , value ) ; } } else if ( typeof value === 'string' ) { set ( data , field . path , hexToLottieColor ( handleColor ( value ) ) ) ; } } } param data param properties export function resetColors ( data , properties ) { for ( const field of properties ) { if ( field . type !== 'color' ) { continue ; } set ( data , field . path , field . value ) ; } } param data param properties param colors export function updateColors ( data , properties , colors ) { const parsedColors = colors . split ( ',' ) ; if ( parsedColors . length ) { for ( const color of parsedColors ) { const parts = color . split ( ':' ) ; if ( parts . length !== 2 ) { continue ; } for ( const field of properties ) { if ( field . type !== 'color' ) { continue ; } if ( field . name . toLowerCase ( ) === parts [ 0 ] . toLowerCase ( ) ) { set ( data , field . path , hexToLottieColor ( handleColor ( parts [ 1 ] ) ) ) ; } } } } } param data param properties param name param extraPath export function resetProperty ( data , properties , name , extraPath ? ) { for ( const field of properties ) { if ( field . name . toLowerCase ( ) !== name . toLowerCase ( ) ) { continue ; } if ( extraPath ) { set ( data , field . path + `.${ extraPath }` , get ( field . value , extraPath ) ) ; } else { set ( data , field . path , field . value ) ; } } } param data param properties param name param value param extraPath export function updateProperty ( data , properties , name , value , extraPath ? ) { for ( const field of properties ) { if ( field . name . toLowerCase ( ) !== name . toLowerCase ( ) ) { continue ; } const newPath = field . path + ( extraPath ? `.${ extraPath }` : '' ) ; let ratio = 1 ; if ( field . type === 'slider' ) { ratio = field . value / LORDICON_SCALE ; } else if ( field . type === 'point' ) { ratio = ( ( field . value [ 0 ] + field . value [ 1 ] ) / 2 ) / LORDICON_SCALE ; } set ( data , newPath , value * ratio ) ; } } param data param properties param name param value param extraPath export function replaceProperty ( data , properties , name , value , extraPath ? ) { for ( const field of properties ) { if ( field . name . toLowerCase ( ) !== name . toLowerCase ( ) ) { continue ; } const newPath = field . path + ( extraPath ? `.${ extraPath }` : '' ) ; set ( data , newPath , value ) ; } } param data returns export function iconFeatures ( data ) { if ( data && data . features && Array . isArray ( data . features ) ) { return data . features ; } return [ ] ; }