diff --git a/lib/extract/extractBrush.js b/lib/extract/extractBrush.js index 882fadfe..38e8f12b 100644 --- a/lib/extract/extractBrush.js +++ b/lib/extract/extractBrush.js @@ -1,5 +1,4 @@ -import extractColor from './extractColor'; -import { Platform } from 'react-native'; +import extractColor, { integerColor } from './extractColor'; const urlIdPattern = /^url\(#(.+)\)$/; @@ -8,7 +7,7 @@ const currentColorBrush = [2]; export default function extractBrush(color) { if (typeof color === 'number') { if (color >>> 0 === color && color >= 0 && color <= 0xffffffff) { - return [0, Platform.OS === 'android' ? color | 0x0 : color]; + return [0, integerColor(color)]; } } diff --git a/lib/extract/extractColor.js b/lib/extract/extractColor.js index f2539279..9b1167b9 100644 --- a/lib/extract/extractColor.js +++ b/lib/extract/extractColor.js @@ -345,7 +345,7 @@ function rgbFromString(string) { return null; } - return Platform.OS === 'android' ? rgb | 0x0 : rgb; + return integerColor(rgb); } else { return null; } @@ -403,11 +403,22 @@ function colorFromString(string) { } } +const identity = x => x; + +const toSignedInt32 = x => x | 0x0; + +// Android use 32 bit *signed* integer to represent the color +// We utilize the fact that bitwise operations in JS also operates on +// signed 32 bit integers, so that we can use those to convert from +// *unsigned* to *signed* 32bit in that way. +export const integerColor = + Platform.OS === 'android' ? toSignedInt32 : identity; + // Returns 0xaarrggbb or null export default function extractColor(color) { if (typeof color === 'number') { if (color >>> 0 === color && color >= 0 && color <= 0xffffffff) { - return Platform.OS === 'android' ? color | 0x0 : color; + return integerColor(color); } return null; } @@ -430,9 +441,5 @@ export default function extractColor(color) { Math.round(b * 255)) >>> 0; - // Android use 32 bit *signed* integer to represent the color - // We utilize the fact that bitwise operations in JS also operates on - // signed 32 bit integers, so that we can use those to convert from - // *unsigned* to *signed* 32bit int that way. - return Platform.OS === 'android' ? int32Color | 0x0 : int32Color; + return integerColor(int32Color); } diff --git a/lib/extract/extractFill.js b/lib/extract/extractFill.js index 313b88b8..18ec9e40 100644 --- a/lib/extract/extractFill.js +++ b/lib/extract/extractFill.js @@ -1,7 +1,6 @@ import extractBrush from './extractBrush'; import extractOpacity from './extractOpacity'; -import { colorNames } from './extractColor'; -import { Platform } from 'react-native'; +import { colorNames, integerColor } from './extractColor'; const fillRules = { evenodd: 0, @@ -10,10 +9,7 @@ const fillRules = { // default fill is black const black = colorNames.black; -const defaultFill = [ - 0, - Platform.OS === 'android' ? black | 0x0 : black, -]; +const defaultFill = [0, integerColor(black)]; export default function extractFill(props, styleProperties) { if (props.fill != null) {