Skip to content

Commit

Permalink
Replace usages of hexToRgb with setNormalizedColorAlpha
Browse files Browse the repository at this point in the history
Reviewed By: vjeux

Differential Revision: D2906507

fb-gh-sync-id: 671ec5b9f5a701891c3601a8f78968b99476a2b5
shipit-source-id: 671ec5b9f5a701891c3601a8f78968b99476a2b5
  • Loading branch information
javache authored and facebook-github-bot-7 committed Feb 9, 2016
1 parent d80ee0a commit d97223b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Libraries/StyleSheet/__tests__/normalizeColor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
36 changes: 36 additions & 0 deletions Libraries/StyleSheet/__tests__/setNormalizedColorAlpha-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
31 changes: 31 additions & 0 deletions Libraries/StyleSheet/setNormalizedColorAlpha.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit d97223b

Please sign in to comment.