-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace usages of hexToRgb with setNormalizedColorAlpha
Reviewed By: vjeux Differential Revision: D2906507 fb-gh-sync-id: 671ec5b9f5a701891c3601a8f78968b99476a2b5 shipit-source-id: 671ec5b9f5a701891c3601a8f78968b99476a2b5
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Libraries/StyleSheet/__tests__/setNormalizedColorAlpha-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |