diff --git a/lib/helpers.js b/lib/helpers.js index 136d377..8514900 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -9,6 +9,7 @@ import Color from 'art/core/color'; import Transform from 'art/core/transform'; +import {Platform} from 'react-native'; import type { Alignment, Brush, @@ -73,12 +74,26 @@ export function extractTransform(props: TransformProps): Array { ]; } +function toHex(color: Color) { + const intValues = [color.red, color.green, color.blue]; + if (color.alpha < 1) { + // Android uses AARRGGBB ; iOS uses RRGGBBAA + // https://developer.android.com/reference/android/graphics/Color.html#parseColor(java.lang.String) + const position = Platform.OS === 'android' ? 0 : 3; + intValues.splice(position, 0, Math.round(color.alpha * 255)); + } + const hexValues = intValues.map(iv => { + const sv = iv.toString(16); + return sv.length === 1 ? '0' + sv : sv; + }); + return '#' + hexValues.join(''); +} + export function extractColor(color?: ColorType) { if (color == null) { return null; } - const c = new Color(color); - return c.toHEX(); + return toHex(new Color(color)); } export function extractStrokeJoin(strokeJoin?: StrokeJoin) {