Replace usages of hexToRgb with setNormalizedColorAlpha
Reviewed By: vjeux Differential Revision: D2906507 fb-gh-sync-id: 671ec5b9f5a701891c3601a8f78968b99476a2b5 shipit-source-id: 671ec5b9f5a701891c3601a8f78968b99476a2b5
This commit is contained in:
parent
d80ee0a8ac
commit
d97223bc3b
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
|
@ -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;
|
Loading…
Reference in New Issue