Skip to content

Commit babdeb3

Browse files
javachefacebook-github-bot-7
authored andcommitted
Make processColor more efficient
Reviewed By: @nicklockwood, @vjeux Differential Revision: D2507684
1 parent 787895e commit babdeb3

File tree

3 files changed

+18
-40
lines changed

3 files changed

+18
-40
lines changed

Libraries/StyleSheet/__tests__/processColor-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ describe('processColor', () => {
9191

9292
describe('HSL strings', () => {
9393

94-
it('should convert hsl(x, y%, z%)', () => {
94+
it('should convert hsla(x, y%, z%, a)', () => {
9595
var colorFromString = processColor('hsla(318, 69%, 55%, 0.25)');
9696
var expectedInt = 0x40DB3DAC;
9797
expect(colorFromString).toEqual(expectedInt);
9898
});
9999

100-
it('should convert hsl x, y%, z%', () => {
100+
it('should convert hsla x, y%, z%, a', () => {
101101
var colorFromString = processColor('hsla 318, 69%, 55%, 0.25');
102102
var expectedInt = 0x40DB3DAC;
103103
expect(colorFromString).toEqual(expectedInt);

Libraries/StyleSheet/processColor.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,29 @@
1313
var tinycolor = require('tinycolor');
1414
var Platform = require('Platform');
1515

16+
/* eslint no-bitwise: 0 */
1617
function processColor(color) {
1718
if (!color || typeof color === 'number') {
1819
return color;
1920
} else if (color instanceof Array) {
2021
return color.map(processColor);
2122
} else {
22-
var hexString = tinycolor(color).toHex8();
23-
var colorInt = parseInt(hexString, 16);
24-
if (Platform.OS === 'android') {
25-
// Android use 32 bit *signed* integer to represent the color
26-
// We utilize the fact that bitwise operations in JS also operates on
27-
// signed 32 bit integers, so that we can use those to convert from
28-
// *unsiged* to *signed* 32bit int that way.
29-
colorInt = colorInt | 0x0;
23+
var color = tinycolor(color);
24+
if (color.isValid()) {
25+
var rgb = color.toRgb();
26+
// All bitwise operations happen on 32-bit numbers, so we shift the 1 first
27+
// then multiply it with the actual value.
28+
var colorInt = Math.round(rgb.a * 255) * (1 << 24) + rgb.r * (1 << 16) + rgb.g * (1 << 8) + rgb.b;
29+
if (Platform.OS === 'android') {
30+
// Android use 32 bit *signed* integer to represent the color
31+
// We utilize the fact that bitwise operations in JS also operates on
32+
// signed 32 bit integers, so that we can use those to convert from
33+
// *unsiged* to *signed* 32bit int that way.
34+
colorInt = colorInt | 0x0;
35+
}
36+
return colorInt;
3037
}
31-
return colorInt;
38+
return 0;
3239
}
3340
}
3441

Libraries/vendor/tinycolor/tinycolor.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ function tinycolor (color, opts) {
3434
}
3535

3636
tinycolor.prototype = {
37-
toHex8: function() {
38-
return rgbaToHex(this._r, this._g, this._b, this._a);
39-
},
4037
toRgb: function() {
4138
return { r: mathRound(this._r), g: mathRound(this._g), b: mathRound(this._b), a: this._a };
4239
},
@@ -163,7 +160,6 @@ function hslToRgb(h, s, l) {
163160
// *Assumes:* h is contained in [0, 1] or [0, 360] and s and v are contained in [0, 1] or [0, 100]
164161
// *Returns:* { r, g, b } in the set [0, 255]
165162
function hsvToRgb(h, s, v) {
166-
167163
h = bound01(h, 360) * 6;
168164
s = bound01(s, 100);
169165
v = bound01(v, 100);
@@ -181,21 +177,6 @@ function hslToRgb(h, s, l) {
181177
return { r: r * 255, g: g * 255, b: b * 255 };
182178
}
183179

184-
// `rgbaToHex`
185-
// Converts an RGBA color plus alpha transparency to hex
186-
// Assumes r, g, b and a are contained in the set [0, 255]
187-
// Returns an 8 character hex
188-
function rgbaToHex(r, g, b, a) {
189-
var hex = [
190-
pad2(convertDecimalToHex(a)),
191-
pad2(mathRound(r).toString(16)),
192-
pad2(mathRound(g).toString(16)),
193-
pad2(mathRound(b).toString(16))
194-
];
195-
196-
return hex.join("");
197-
}
198-
199180
// Big List of Colors
200181
// ------------------
201182
// <http://www.w3.org/TR/css3-color/#svg-color>
@@ -399,11 +380,6 @@ function isPercentage(n) {
399380
return typeof n === "string" && n.indexOf('%') != -1;
400381
}
401382

402-
// Force a hex value to have 2 characters
403-
function pad2(c) {
404-
return c.length == 1 ? '0' + c : '' + c;
405-
}
406-
407383
// Replace a decimal with it's percentage value
408384
function convertToPercentage(n) {
409385
if (n <= 1) {
@@ -413,11 +389,6 @@ function convertToPercentage(n) {
413389
return n;
414390
}
415391

416-
// Converts a decimal to a hex value
417-
function convertDecimalToHex(d) {
418-
return Math.round(parseFloat(d) * 255).toString(16);
419-
}
420-
421392
var matchers = (function() {
422393
// <http://www.w3.org/TR/css3-values/#integers>
423394
var CSS_INTEGER = "[-\\+]?\\d+%?";

0 commit comments

Comments
 (0)