@@ -43,15 +43,44 @@ const faAcorn = {
43
43
]
44
44
}
45
45
46
+ const blueHex = '0000ff'
47
+ const redHex = 'ff0000'
46
48
47
- // react-native-svg>9.x changed the way it uses the `fill` attribute. In earlier versions,
48
- // it used an array ([0, DECIMAL_COLOR]), whereas newer versions dropped the array
49
- // in favor of a scalar value %DECIMAL_COLOR%
49
+ function rgbToHex ( r , g , b ) {
50
+ return [ r , g , b ]
51
+ . map ( ( c ) => {
52
+ const hex = c . toString ( 16 ) ;
53
+ return hex . length == 1 ? `0${ hex } ` : hex ;
54
+ } )
55
+ . join ( "" ) ;
56
+ }
57
+
58
+ function decimalToHex ( decimal ) {
59
+ return decimal . toString ( 16 ) . substr ( 2 , 6 ) ;
60
+ }
61
+
62
+ // react-native-svg changed the way it uses the `fill` attribute across versions. Older versions
63
+ // return [_, r, g, b, _] (where 0 <= {r,g,b} <= 1), while other versions return arrays, and even
64
+ // scalar values... We, much like the Borg, will adapt.
50
65
function getActualFillColorHex ( element ) {
51
- const fillProp = element . props . fill
52
- const decimalColor = Array . isArray ( fillProp ) ? fillProp [ 1 ] : fillProp ;
53
- // convert to hex
54
- return decimalColor . toString ( 16 ) ;
66
+ const fillProp = element . props . fill ;
67
+ if ( ! Array . isArray ( fillProp ) ) {
68
+ // rn-svg v11 use a simple sclar value, representing the decimal value of the color
69
+ // @link https://github.com/react-native-community/react-native-svg/blob/v11.0.1/__tests__/__snapshots__/css.test.tsx.snap
70
+ // https://github.com/react-native-community/react-native-svg/blob/v12.1.0/__tests__/__snapshots__/css.test.tsx.snap#L158
71
+ return decimalToHex ( fillProp ) ;
72
+ } else if ( fillProp . length === 5 ) {
73
+ // rn-svg <= v8 return an array [_, r, g, b, _], where {rgb} are in the range of {0,1}
74
+ // @note no links provided because rn-svg didn't include any tests in those versions
75
+ return rgbToHex ( fillProp [ 1 ] * 255 , fillProp [ 2 ] * 255 , fillProp [ 3 ] * 255 ) ;
76
+ } else if ( fillProp . length === 2 ) {
77
+ // rn-svg v9, and v10 return an array with shape [_, DECIMAL_COLOR]
78
+ // @link https://github.com/react-native-community/react-native-svg/blob/v9.14.0/__tests__/__snapshots__/css.test.tsx.snap#L159
79
+ // @link https://github.com/react-native-community/react-native-svg/blob/v10.1.0/__tests__/__snapshots__/css.test.tsx.snap#L159
80
+ return decimalToHex ( fillProp [ 1 ] ) ;
81
+ }
82
+
83
+ return null ;
55
84
}
56
85
57
86
fontawesome . library . add ( faCoffee , faCircle )
@@ -247,7 +276,7 @@ describe('duotone support', () => {
247
276
const tree = renderer . create ( < FontAwesomeIcon icon = { faAcorn } style = { styles . icon } /> ) . toJSON ( )
248
277
const primaryLayer = tree . children [ 0 ] . children [ 0 ] . children [ 1 ]
249
278
const secondaryLayer = tree . children [ 0 ] . children [ 0 ] . children [ 0 ]
250
- expect ( getActualFillColorHex ( primaryLayer ) ) . toEqual ( 'ff0000ff' )
279
+ expect ( getActualFillColorHex ( primaryLayer ) ) . toEqual ( blueHex )
251
280
expect ( secondaryLayer . props . fillOpacity ) . toEqual ( 0.4 )
252
281
} )
253
282
} )
@@ -261,7 +290,7 @@ describe('duotone support', () => {
261
290
const tree = renderer . create ( < FontAwesomeIcon icon = { faAcorn } style = { styles . icon } secondaryOpacity = { 0.123 } /> ) . toJSON ( )
262
291
const primaryLayer = tree . children [ 0 ] . children [ 0 ] . children [ 1 ]
263
292
const secondaryLayer = tree . children [ 0 ] . children [ 0 ] . children [ 0 ]
264
- expect ( getActualFillColorHex ( primaryLayer ) ) . toEqual ( 'ff0000ff' )
293
+ expect ( getActualFillColorHex ( primaryLayer ) ) . toEqual ( blueHex )
265
294
expect ( secondaryLayer . props . fillOpacity ) . toEqual ( 0.123 )
266
295
} )
267
296
} )
@@ -274,7 +303,7 @@ describe('duotone support', () => {
274
303
} )
275
304
const tree = renderer . create ( < FontAwesomeIcon icon = { faAcorn } style = { styles . icon } secondaryColor = { "red" } /> ) . toJSON ( )
276
305
const secondaryLayer = tree . children [ 0 ] . children [ 0 ] . children [ 0 ]
277
- expect ( getActualFillColorHex ( secondaryLayer ) ) . toEqual ( 'ffff0000' )
306
+ expect ( getActualFillColorHex ( secondaryLayer ) ) . toEqual ( redHex )
278
307
expect ( secondaryLayer . props . fillOpacity ) . toEqual ( 1 )
279
308
} )
280
309
} )
@@ -287,7 +316,7 @@ describe('duotone support', () => {
287
316
} )
288
317
const tree = renderer . create ( < FontAwesomeIcon icon = { faAcorn } style = { styles . icon } secondaryColor = { "red" } secondaryOpacity = { 0.123 } /> ) . toJSON ( )
289
318
const secondaryLayer = tree . children [ 0 ] . children [ 0 ] . children [ 0 ]
290
- expect ( getActualFillColorHex ( secondaryLayer ) ) . toEqual ( 'ffff0000' )
319
+ expect ( getActualFillColorHex ( secondaryLayer ) ) . toEqual ( redHex )
291
320
expect ( secondaryLayer . props . fillOpacity ) . toEqual ( 0.123 )
292
321
} )
293
322
} )
0 commit comments