11import { Color } from '@nativescript/core' ;
22import { typesMap as _typesMap } from './_helpers.common' ;
3+ import { dataSerialize } from '@nativescript/core/utils' ;
34
45const typesMap = Object . assign ( { } , _typesMap , {
56 number : ( options ) => fromJSToNativePrimitive ( options ) ,
67 boolean : ( options ) => fromJSToNativePrimitive ( options ) ,
78 string : ( options ) => fromJSToNativePrimitive ( options ) ,
89 Array : ( options ) => convertJSArrayToNative ( options ) ,
910 HIColor : ( options ) => toHIColor ( options ) ,
11+ object : ( options ) => dataSerialize ( options ) ,
1012} ) ;
1113
1214export function convertJSArrayToNative ( array ) {
@@ -43,10 +45,10 @@ export function colorToString(color: any) {
4345}
4446
4547export function toHIColor ( color ) {
46- if ( color instanceof Array ) {
48+ if ( Array . isArray ( color ) ) {
4749 const colorArray = [ ] ;
4850 for ( let i = 0 ; i < color . length ; i ++ ) {
49- const c = color [ i ] ;
51+ let c = color [ i ] ;
5052
5153 if ( c . radialGradient && c . stops ) {
5254 const stops = c . stops . map ( ( stop , index ) => [ index , colorToString ( stop ) ] ) ;
@@ -67,38 +69,70 @@ export function toHIColor(color) {
6769 } )
6870 ) ;
6971 } else {
72+ if ( typeof c === 'string' && c . indexOf ( '#' ) === 0 ) {
73+ if ( c . length === 4 ) {
74+ c = `#${ c [ 1 ] } ${ c [ 1 ] } ${ c [ 2 ] } ${ c [ 2 ] } ${ c [ 3 ] } ${ c [ 3 ] } ` ;
75+ }
76+ }
77+
7078 const _c = new Color ( c ) ;
71- colorArray . push ( new HIColor ( _c . ios ) as any ) ;
79+ colorArray . push ( HIColor . alloc ( ) . initWithUIColor ( _c . ios ) ) ;
7280 }
7381 }
7482
7583 return convertJSArrayToNative ( colorArray ) ;
7684 } else {
7785 if ( color . radialGradient && color . stops ) {
78- const stops = color . stops . map ( ( stop , index ) => [ index , colorToString ( stop ) ] ) ;
86+ const stops = NSMutableArray . alloc ( ) . initWithCapacity ( color . stops . length ) ;
87+
88+ for ( const stop of color . stops ) {
89+ const item = NSMutableArray . alloc ( ) . initWithCapacity ( 2 ) ;
90+ item . addObject ( NSNumber . numberWithDouble ( stop [ 0 ] ) ) ;
91+ item . addObject ( colorToString ( stop ) ) ;
92+ stops . addObject ( item ) ;
93+ }
94+
7995 const g = color . radialGradient ;
80- return new HIColor ( {
81- radialGradient : NSDictionary . dictionaryWithObjectsForKeys ( [ g . cx , g . cy , g . r ] , [ 'cx' , 'cy' , 'r' ] ) ,
82- stops : stops ,
83- } ) ;
96+ const radialGradient = NSMutableDictionary . alloc ( ) . initWithCapacity ( 3 ) ;
97+ radialGradient . setObjectForKey ( g . cx , 'cx' ) ;
98+ radialGradient . setObjectForKey ( g . cy , 'cy' ) ;
99+ radialGradient . setObjectForKey ( g . r , 'r' ) ;
100+ return HIColor . alloc ( ) . initWithRadialGradientStops ( radialGradient , stops ) ;
84101 } else if ( color . linearGradient && color . stops ) {
85- const stops = color . stops . map ( ( stop , index ) => [ index , colorToString ( stop ) ] ) ;
86- const g = color . linearGradient ;
87- return new HIColor ( {
88- linearGradient : NSDictionary . dictionaryWithObjectsForKeys ( [ g . x1 , g . y1 , g . x2 , g . y2 ] , [ 'x1' , 'y1' , 'x2' , 'y2' ] ) ,
89- stops : stops ,
102+ const stops = NSMutableArray . alloc ( ) . initWithCapacity ( color . stops . length ) ;
103+
104+ color . stops . forEach ( ( stop , index ) => {
105+ const item = NSMutableArray . alloc ( ) . initWithCapacity ( 2 ) ;
106+ item . addObject ( NSNumber . numberWithInt ( index ) ) ;
107+ item . addObject ( colorToString ( stop ) ) ;
108+ stops . addObject ( item ) ;
90109 } ) ;
110+ const g = color . linearGradient ;
111+
112+ const linearGradient = NSMutableDictionary . alloc ( ) . initWithCapacity ( 4 ) ;
113+
114+ linearGradient . setObjectForKey ( g . x1 , 'x1' ) ;
115+ linearGradient . setObjectForKey ( g . y1 , 'y1' ) ;
116+ linearGradient . setObjectForKey ( g . x2 , 'x2' ) ;
117+ linearGradient . setObjectForKey ( g . y2 , 'y2' ) ;
118+
119+ return HIColor . alloc ( ) . initWithLinearGradientStops ( linearGradient , stops ) ;
91120 } else {
121+ if ( typeof color === 'string' && color . indexOf ( '#' ) === 0 ) {
122+ if ( color . length === 4 ) {
123+ color = `#${ color [ 1 ] } ${ color [ 1 ] } ${ color [ 2 ] } ${ color [ 2 ] } ${ color [ 3 ] } ${ color [ 3 ] } ` ;
124+ }
125+ return HIColor . alloc ( ) . initWithHexValue ( color ) ;
126+ }
92127 const c = new Color ( color ) ;
93- return new HIColor ( c . ios ) as any ;
128+ return HIColor . alloc ( ) . initWithUIColor ( c . ios ) ;
94129 }
95130 }
96131}
97132
98133export function optionsBuilder ( schema , options , containerObject ) {
99134 const schemaKeys = Object . keys ( schema ) ;
100135 const optionsKeys = Object . keys ( options ) ;
101-
102136 for ( const schemaKey of schemaKeys ) {
103137 if ( ( < any > optionsKeys ) . includes ( schemaKey ) ) {
104138 if ( typeof typesMap [ schema [ schemaKey ] ] === 'function' ) {
0 commit comments