fix Color.fromHex Bug (#9867)

This commit is contained in:
alpha
2021-12-28 11:52:01 +08:00
committed by GitHub
parent 6601d1d2de
commit f153b5cc1b
2 changed files with 33 additions and 2 deletions
+2 -2
View File
@@ -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;
}
+31
View File
@@ -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);
});
});
});