From babdeb33ba8b4d0ca0a0b004ff103b4796b41188 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Mon, 5 Oct 2015 10:08:10 -0700 Subject: [PATCH] Make processColor more efficient Reviewed By: @nicklockwood, @vjeux Differential Revision: D2507684 --- .../StyleSheet/__tests__/processColor-test.js | 4 +-- Libraries/StyleSheet/processColor.js | 25 ++++++++++------ Libraries/vendor/tinycolor/tinycolor.js | 29 ------------------- 3 files changed, 18 insertions(+), 40 deletions(-) diff --git a/Libraries/StyleSheet/__tests__/processColor-test.js b/Libraries/StyleSheet/__tests__/processColor-test.js index 9e11931cd..9416353b7 100644 --- a/Libraries/StyleSheet/__tests__/processColor-test.js +++ b/Libraries/StyleSheet/__tests__/processColor-test.js @@ -91,13 +91,13 @@ describe('processColor', () => { describe('HSL strings', () => { - it('should convert hsl(x, y%, z%)', () => { + it('should convert hsla(x, y%, z%, a)', () => { var colorFromString = processColor('hsla(318, 69%, 55%, 0.25)'); var expectedInt = 0x40DB3DAC; expect(colorFromString).toEqual(expectedInt); }); - it('should convert hsl x, y%, z%', () => { + it('should convert hsla x, y%, z%, a', () => { var colorFromString = processColor('hsla 318, 69%, 55%, 0.25'); var expectedInt = 0x40DB3DAC; expect(colorFromString).toEqual(expectedInt); diff --git a/Libraries/StyleSheet/processColor.js b/Libraries/StyleSheet/processColor.js index cdec10904..e259113e3 100644 --- a/Libraries/StyleSheet/processColor.js +++ b/Libraries/StyleSheet/processColor.js @@ -13,22 +13,29 @@ var tinycolor = require('tinycolor'); var Platform = require('Platform'); +/* eslint no-bitwise: 0 */ function processColor(color) { if (!color || typeof color === 'number') { return color; } else if (color instanceof Array) { return color.map(processColor); } else { - var hexString = tinycolor(color).toHex8(); - var colorInt = parseInt(hexString, 16); - if (Platform.OS === 'android') { - // 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 - // *unsiged* to *signed* 32bit int that way. - colorInt = colorInt | 0x0; + var color = tinycolor(color); + if (color.isValid()) { + var rgb = color.toRgb(); + // All bitwise operations happen on 32-bit numbers, so we shift the 1 first + // then multiply it with the actual value. + var colorInt = Math.round(rgb.a * 255) * (1 << 24) + rgb.r * (1 << 16) + rgb.g * (1 << 8) + rgb.b; + if (Platform.OS === 'android') { + // 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 + // *unsiged* to *signed* 32bit int that way. + colorInt = colorInt | 0x0; + } + return colorInt; } - return colorInt; + return 0; } } diff --git a/Libraries/vendor/tinycolor/tinycolor.js b/Libraries/vendor/tinycolor/tinycolor.js index 12fe005f3..9b3fe0df1 100755 --- a/Libraries/vendor/tinycolor/tinycolor.js +++ b/Libraries/vendor/tinycolor/tinycolor.js @@ -34,9 +34,6 @@ function tinycolor (color, opts) { } tinycolor.prototype = { - toHex8: function() { - return rgbaToHex(this._r, this._g, this._b, this._a); - }, toRgb: function() { return { r: mathRound(this._r), g: mathRound(this._g), b: mathRound(this._b), a: this._a }; }, @@ -163,7 +160,6 @@ function hslToRgb(h, s, l) { // *Assumes:* h is contained in [0, 1] or [0, 360] and s and v are contained in [0, 1] or [0, 100] // *Returns:* { r, g, b } in the set [0, 255] function hsvToRgb(h, s, v) { - h = bound01(h, 360) * 6; s = bound01(s, 100); v = bound01(v, 100); @@ -181,21 +177,6 @@ function hslToRgb(h, s, l) { return { r: r * 255, g: g * 255, b: b * 255 }; } -// `rgbaToHex` -// Converts an RGBA color plus alpha transparency to hex -// Assumes r, g, b and a are contained in the set [0, 255] -// Returns an 8 character hex -function rgbaToHex(r, g, b, a) { - var hex = [ - pad2(convertDecimalToHex(a)), - pad2(mathRound(r).toString(16)), - pad2(mathRound(g).toString(16)), - pad2(mathRound(b).toString(16)) - ]; - - return hex.join(""); -} - // Big List of Colors // ------------------ // @@ -399,11 +380,6 @@ function isPercentage(n) { return typeof n === "string" && n.indexOf('%') != -1; } -// Force a hex value to have 2 characters -function pad2(c) { - return c.length == 1 ? '0' + c : '' + c; -} - // Replace a decimal with it's percentage value function convertToPercentage(n) { if (n <= 1) { @@ -413,11 +389,6 @@ function convertToPercentage(n) { return n; } -// Converts a decimal to a hex value -function convertDecimalToHex(d) { - return Math.round(parseFloat(d) * 255).toString(16); -} - var matchers = (function() { // var CSS_INTEGER = "[-\\+]?\\d+%?";