Skip to content

Commit d97223b

Browse files
javachefacebook-github-bot-7
authored andcommitted
Replace usages of hexToRgb with setNormalizedColorAlpha
Reviewed By: vjeux Differential Revision: D2906507 fb-gh-sync-id: 671ec5b9f5a701891c3601a8f78968b99476a2b5 shipit-source-id: 671ec5b9f5a701891c3601a8f78968b99476a2b5
1 parent d80ee0a commit d97223b

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

Libraries/StyleSheet/__tests__/normalizeColor-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,9 @@ describe('normalizeColor', function() {
123123
expect(normalizeColor(0xffffffff)).toBe(0xffffffff);
124124
expect(normalizeColor(0x01234567)).toBe(0x01234567);
125125
});
126+
127+
it('should return the same color when it\'s already normalized', function() {
128+
const normalizedColor = normalizeColor('red') || 0;
129+
expect(normalizeColor(normalizedColor)).toBe(normalizedColor);
130+
});
126131
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
jest.dontMock('setNormalizedColorAlpha');
12+
jest.dontMock('normalizeColor');
13+
14+
var setNormalizedColorAlpha = require('setNormalizedColorAlpha');
15+
var normalizeColor = require('normalizeColor');
16+
17+
describe('setNormalizedColorAlpha', function() {
18+
it('should adjust the alpha of the color passed in', function() {
19+
expect(setNormalizedColorAlpha(0xffffffff, 0.4)).toBe(0xffffff66);
20+
expect(setNormalizedColorAlpha(0x204080ff, 0.6)).toBe(0x20408099);
21+
});
22+
23+
it('should clamp invalid input', function() {
24+
expect(setNormalizedColorAlpha(0xffffffff, 1.5)).toBe(0xffffffff);
25+
expect(setNormalizedColorAlpha(0xffffffff, -1)).toBe(0xffffff00);
26+
});
27+
28+
it('should ignore the color\'s original alpha', function() {
29+
expect(setNormalizedColorAlpha(0x204080aa, 0.8)).toBe(0x204080cc);
30+
});
31+
32+
it('should return the original color when alpha is unchanged', function() {
33+
var originalColor = normalizeColor('blue');
34+
expect(setNormalizedColorAlpha(originalColor, 1)).toBe(originalColor);
35+
});
36+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule setNormalizedColorAlpha
10+
* @flow
11+
*/
12+
/* eslint no-bitwise: 0 */
13+
'use strict';
14+
15+
/**
16+
* number should be a color processed by `normalizeColor`
17+
* alpha should be number between 0 and 1
18+
*/
19+
function setNormalizedColorAlpha(input: number, alpha: number): number {
20+
if (alpha < 0) {
21+
alpha = 0;
22+
} else if (alpha > 1) {
23+
alpha = 1;
24+
}
25+
26+
alpha = Math.round(alpha * 255);
27+
// magic bitshift guarantees we return an unsigned int
28+
return ((input & 0xffffff00) | alpha) >>> 0;
29+
}
30+
31+
module.exports = setNormalizedColorAlpha;

0 commit comments

Comments
 (0)