From d97223bc3b207545ac1bb9d97868685481a25f07 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 9 Feb 2016 14:57:58 -0800 Subject: [PATCH] Replace usages of hexToRgb with setNormalizedColorAlpha Reviewed By: vjeux Differential Revision: D2906507 fb-gh-sync-id: 671ec5b9f5a701891c3601a8f78968b99476a2b5 shipit-source-id: 671ec5b9f5a701891c3601a8f78968b99476a2b5 --- .../__tests__/normalizeColor-test.js | 5 +++ .../__tests__/setNormalizedColorAlpha-test.js | 36 +++++++++++++++++++ .../StyleSheet/setNormalizedColorAlpha.js | 31 ++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 Libraries/StyleSheet/__tests__/setNormalizedColorAlpha-test.js create mode 100644 Libraries/StyleSheet/setNormalizedColorAlpha.js diff --git a/Libraries/StyleSheet/__tests__/normalizeColor-test.js b/Libraries/StyleSheet/__tests__/normalizeColor-test.js index dc12de12f..fd8d7d147 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 000000000..598652dbb --- /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 000000000..cd9433099 --- /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;