mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
fix Color.fromHex Bug (#9867)
This commit is contained in:
@@ -102,8 +102,8 @@ export class Color extends ValueType {
|
||||
out.r = parseInt(hexString.substr(0, 2), 16) || 0;
|
||||
out.g = parseInt(hexString.substr(2, 2), 16) || 0;
|
||||
out.b = parseInt(hexString.substr(4, 2), 16) || 0;
|
||||
out.a = parseInt(hexString.substr(6, 2), 16);
|
||||
out.a = !Number.isNaN(out.a) ? out.a : 255;
|
||||
const a = parseInt(hexString.substr(6, 2), 16);
|
||||
out.a = !Number.isNaN(a) ? a : 255;
|
||||
out._val = ((out.a << 24) >>> 0) + (out.b << 16) + (out.g << 8) + out.r;
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Color } from '../../../cocos/core/math/color';
|
||||
|
||||
// test Color
|
||||
describe('Test Color', () => {
|
||||
test('fromHex', () => {
|
||||
// fromHex and prototype.fromHex
|
||||
const startColor = new Color(0, 0, 0, 0);
|
||||
const endColor = new Color(255, 255, 255, 255);
|
||||
[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0].forEach((ratio) => {
|
||||
const testColor = new Color();
|
||||
Color.lerp(testColor, startColor, endColor, ratio);
|
||||
const testHexRGBA = testColor.toHEX('#rrggbbaa');
|
||||
const color1 = new Color();
|
||||
color1.fromHEX(testHexRGBA);
|
||||
expect(color1._val).toBe(testColor._val);
|
||||
|
||||
const color2 = new Color();
|
||||
Color.fromHEX(color2, testHexRGBA);
|
||||
expect(color2._val).toBe(testColor._val);
|
||||
|
||||
const testHexRGB = testColor.toHEX('#rrggbb');
|
||||
const color3 = new Color();
|
||||
color3.fromHEX(testHexRGB);
|
||||
expect(color3.a).toBe(255);
|
||||
|
||||
const color4 = new Color();
|
||||
Color.fromHEX(color4, testHexRGB);
|
||||
expect(color4._val).toBe(color3._val);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user