From b3b6b2b3d3d5303f090b237b214d08882483c171 Mon Sep 17 00:00:00 2001 From: Kacper Wiszczuk Date: Wed, 18 Dec 2019 17:24:40 +0100 Subject: [PATCH] fix: toHex not working --- lib/helpers.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) 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) {