Refactor and simplify color processing

This commit is contained in:
Mikael Sand
2019-02-27 23:06:53 +02:00
parent 014e2375fa
commit d80ab86c18
3 changed files with 18 additions and 16 deletions
+2 -3
View File
@@ -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)];
}
}
+14 -7
View File
@@ -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);
}
+2 -6
View File
@@ -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) {