From 8c1b380424d4fd70293db52f5ef1eff4a396caca Mon Sep 17 00:00:00 2001 From: ChiaNing Date: Tue, 18 Feb 2020 16:03:03 +0800 Subject: [PATCH] color hex bug fix (#6180) --- cocos/core/math/color.ts | 15 ++++++++------- cocos/core/math/deprecated.ts | 8 ++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cocos/core/math/color.ts b/cocos/core/math/color.ts index 6a01812c87..14b71cbdf9 100644 --- a/cocos/core/math/color.ts +++ b/cocos/core/math/color.ts @@ -93,14 +93,15 @@ export class Color extends ValueType { } /** - * @zh 根据指定整型数据设置颜色 + * @zh 从十六进制颜色字符串中读入颜色到 out 中 */ - public static fromHex (out: Out, hex: number) { - out.r = ((hex >> 24)) / 255.0; - out.g = ((hex >> 16) & 0xff) / 255.0; - out.b = ((hex >> 8) & 0xff) / 255.0; - out.a = ((hex) & 0xff) / 255.0; - + public static fromHEX (out: Out, hexString: string) { + hexString = (hexString.indexOf('#') === 0) ? hexString.substring(1) : hexString; + 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) || 255; + out._val = ((out.a << 24) >>> 0) + (out.b << 16) + (out.g << 8) + out.r; return out; } diff --git a/cocos/core/math/deprecated.ts b/cocos/core/math/deprecated.ts index 25cd6b63c2..b1db27e6d9 100644 --- a/cocos/core/math/deprecated.ts +++ b/cocos/core/math/deprecated.ts @@ -357,6 +357,14 @@ replaceProperty(Color, 'Color', [ target: Color, targetName: 'Color', }, + { + name: 'fromHex', + newName: 'fromHEX', + customFunction: function (...args: any) { + let arg1 = args[1].toString(16); + return cc.Color.fromHEX(args[0], arg1); + }, + }, ]); replaceProperty(Mat3, 'Mat3', [