import { Utils } from 'tuya-panel-kit' ; import _ from 'lodash' ; export { default as ColorUtils } from './color' ; export { default as StorageUtils } from './storage' ; export { default as SupportUtils } from './support' ; import Strings from '../components/timer/normal-timing/i18n' ; const { CoreUtils } = Utils ; export function handleError ( error ) { console . warn ( error . message || error ) ; } type WinSize = { width : number ; height : number ; } ; export const winSize = { width : Utils . RatioUtils . width , height : Utils . RatioUtils . height , } ; export const setWinSize = ( size ) : void => { Object . assign ( winSize , size ) ; } ; export const getWinSize = ( ) : WinSize => { return winSize ; } ; interface PercentOption { min ? : number ; max ? : number ; minPercent ? : number ; } param value param option export const formatPercent = ( value , { min = 0 , max = 1000 , minPercent = 0 } = { } ) : number => { return Math . round ( ( ( 100 - minPercent ) * ( value - min ) ) / ( max - min ) + minPercent ) ; } ; export const to16 = ( value , length = 2 ) : string => { let result = Number ( value ) . toString ( 16 ) ; if ( result . length < length ) { result = _ . padStart ( result , length , '0' ) ; } return result ; } ; param isValueRandom export const randomHsv = ( isValueRandom = false ) : IColour => { return { hue : _ . random ( 360 , false ) , saturation : _ . random ( 1000 , false ) , value : isValueRandom ? _ . random ( 10 , 1000 , false ) : 1000 , } ; } ; param isGetColour export const randomSceneColor = ( isGetColour = true ) : SceneColor => { if ( isGetColour ) { return { isColour : true , ... randomHsv ( ) , brightness : 10 , temperature : 0 } ; } return { isColour : false , temperature : _ . random ( 1000 , false ) , brightness : _ . random ( 10 , 1000 , false ) , hue : 0 , saturation : 0 , value : 10 , } ; } ; export const getStrLengthByByte = ( value ) : number => { if ( ! value ) { return 0 ; } return value . replace ( /[^\u0000-\u00ff]/g , '**' ) . length ; } ; export function formatTime ( value ) { return _ . padStart ( String ( value ) , 2 , '0' ) ; } param center param radius param angle export function getCirclePointCoordinate ( center , radius , angle ) { const [ x , y ] = center ; return [ x + radius * Math . sin ( angle ) , y - radius * Math . cos ( angle ) ] ; } /**
 * transform
 */ export function * transform ( value ) { let start = 0 ; let result = 0 ; let length ; for ( ; true ; ) { length = yield result ; result = parseInt ( value . substr ( start , length ) , 16 ) || 0 ; if ( start + length >= value . length ) { break ; } start += length ; } return result ; } export const parseJSON = ( str ) => { let rst ; if ( str && { } . toString . call ( str ) === '[object String]' ) { try { rst = JSON . parse ( str ) ; } catch ( e ) { try { rst = eval ( `(${ str })` ) ; } catch ( e2 ) { rst = str ; } } } else { rst = typeof str === 'undefined' ? { } : str ; } return rst ; } ; export const parseHour12 = ( time ) => { const t = Utils . TimeUtils . parseHour12 ( time ) ; return t . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; } ; export const repeatArrStr = ( source ) => { if ( ! source ) return '' ; const days = [ ] ; let repeat = '' ; let workDay = true ; let weekend = true ; source . forEach ( ( item , index ) => { if ( index > 0 && index < 6 ) { if ( item === 0 ) { workDay = false ; } } if ( index === 0 || index === 6 ) { if ( item === 0 ) { weekend = false ; } } if ( item === 1 ) { days . push ( Strings . getLang ( `TYLamp_day${ index }` ) ) ; } } ) ; if ( workDay && weekend ) { repeat = Strings . getLang ( 'TYLamp_dayEvery' ) ; } else if ( days . length === 0 ) { repeat = Strings . getLang ( 'TYLamp_dayOnce' ) ; } else { repeat = days . join ( '、' ) ; } return repeat ; } ; export const parseHour12Data = ( second ) => { const t = second % 86400 ; const originHour = parseInt ( `${ t / 3600 }` , 10 ) ; const m = parseInt ( `${ t / 60 - originHour * 60 }` , 10 ) ; let h = originHour % 12 ; if ( h === 0 ) { h = 12 ; } return { timeStr : `${ CoreUtils . toFixed ( h , 2 ) }:${ CoreUtils . toFixed ( m , 2 ) }` , isPM : originHour >= 12 , } ; } ; export enum weeksStr { dayOnce = '0' , dayEvery = '1' , custom = '2' } export const weeksArr = [ weeksStr . dayOnce ] ; export const dataSource = [ { key : weeksStr . dayOnce , title : Strings . getLang ( 'TYLamp_dayOnce' ) , value : weeksStr . dayOnce , } , { key : weeksStr . dayEvery , title : Strings . getLang ( 'TYLamp_dayEvery' ) , value : weeksStr . dayEvery , } , { key : weeksStr . custom , title : Strings . getLang ( 'TYLamp_custom' ) , value : weeksStr . custom , } , ] ; export const colourFormat = ( data ) => { const { hue , saturation , value } = data ; return `${ to16 ( hue , 4 ) }${ to16 ( saturation , 4 ) }${ to16 ( value , 4 ) }` ; } ; export const colourParse = ( str ) => { if ( str . length === 12 ) { return { hue : parseInt ( str . slice ( 0 , 4 ) , 16 ) , saturation : parseInt ( str . slice ( 4 , 8 ) , 16 ) , value : parseInt ( str . slice ( 8 , 12 ) , 16 ) , } ; } return { hue : 0 , saturation : 1000 , value : 1000 , } ; } ; export const actionTypeStr = ( power , workMode ) => { let str = 'TYLamp_off' ; if ( power ) { if ( workMode === 'white' ) { str = 'TYLamp_white' ; } else { str = 'TYLamp_colour' ; } } return Strings . getLang ( str as any ) ; } ; export enum WORK_MODE { WHITE = 'white' , COLOUR = 'colour' , SCENE = 'scene' , MUSIC = 'music' }