diff --git a/Libraries/StyleSheet/__tests__/normalizeColor-test.js b/Libraries/StyleSheet/__tests__/normalizeColor-test.js index dc12de12ff4fa4..fd8d7d14746093 100644 --- a/Libraries/StyleSheet/__tests__/normalizeColor-test.js +++ b/Libraries/StyleSheet/__tests__/normalizeColor-test.js @@ -123,4 +123,9 @@ describe('normalizeColor', function() { expect(normalizeColor(0xffffffff)).toBe(0xffffffff); expect(normalizeColor(0x01234567)).toBe(0x01234567); }); + + it('should return the same color when it\'s already normalized', function() { + const normalizedColor = normalizeColor('red') || 0; + expect(normalizeColor(normalizedColor)).toBe(normalizedColor); + }); }); diff --git a/Libraries/StyleSheet/__tests__/setNormalizedColorAlpha-test.js b/Libraries/StyleSheet/__tests__/setNormalizedColorAlpha-test.js new file mode 100644 index 00000000000000..598652dbba7b35 --- /dev/null +++ b/Libraries/StyleSheet/__tests__/setNormalizedColorAlpha-test.js @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ +'use strict'; + +jest.dontMock('setNormalizedColorAlpha'); +jest.dontMock('normalizeColor'); + +var setNormalizedColorAlpha = require('setNormalizedColorAlpha'); +var normalizeColor = require('normalizeColor'); + +describe('setNormalizedColorAlpha', function() { + it('should adjust the alpha of the color passed in', function() { + expect(setNormalizedColorAlpha(0xffffffff, 0.4)).toBe(0xffffff66); + expect(setNormalizedColorAlpha(0x204080ff, 0.6)).toBe(0x20408099); + }); + + it('should clamp invalid input', function() { + expect(setNormalizedColorAlpha(0xffffffff, 1.5)).toBe(0xffffffff); + expect(setNormalizedColorAlpha(0xffffffff, -1)).toBe(0xffffff00); + }); + + it('should ignore the color\'s original alpha', function() { + expect(setNormalizedColorAlpha(0x204080aa, 0.8)).toBe(0x204080cc); + }); + + it('should return the original color when alpha is unchanged', function() { + var originalColor = normalizeColor('blue'); + expect(setNormalizedColorAlpha(originalColor, 1)).toBe(originalColor); + }); +}); diff --git a/Libraries/StyleSheet/setNormalizedColorAlpha.js b/Libraries/StyleSheet/setNormalizedColorAlpha.js new file mode 100644 index 00000000000000..cd943309922c0e --- /dev/null +++ b/Libraries/StyleSheet/setNormalizedColorAlpha.js @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule setNormalizedColorAlpha + * @flow + */ +/* eslint no-bitwise: 0 */ +'use strict'; + +/** + * number should be a color processed by `normalizeColor` + * alpha should be number between 0 and 1 + */ +function setNormalizedColorAlpha(input: number, alpha: number): number { + if (alpha < 0) { + alpha = 0; + } else if (alpha > 1) { + alpha = 1; + } + + alpha = Math.round(alpha * 255); + // magic bitshift guarantees we return an unsigned int + return ((input & 0xffffff00) | alpha) >>> 0; +} + +module.exports = setNormalizedColorAlpha;