From 06d2c48c314a95f69105b32a139fd67baf3ed78d Mon Sep 17 00:00:00 2001 From: MadCcc <1075746765@qq.com> Date: Fri, 23 Sep 2022 17:31:21 +0800 Subject: [PATCH] fix: getAlphaColor should ignore alpha color (#37720) --- components/theme/__tests__/util.test.tsx | 9 +++++++++ components/theme/util/getAlphaColor.ts | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 components/theme/__tests__/util.test.tsx diff --git a/components/theme/__tests__/util.test.tsx b/components/theme/__tests__/util.test.tsx new file mode 100644 index 0000000000..45741d15ba --- /dev/null +++ b/components/theme/__tests__/util.test.tsx @@ -0,0 +1,9 @@ +import getAlphaColor from '../util/getAlphaColor'; + +describe('util', () => { + describe('getAlphaColor', () => { + it('should not process color with alpha', () => { + expect(getAlphaColor('rgba(0, 0, 0, 0.5)', 'rgba(255, 255, 255)')).toBe('rgba(0, 0, 0, 0.5)'); + }); + }); +}); diff --git a/components/theme/util/getAlphaColor.ts b/components/theme/util/getAlphaColor.ts index 13c5bad82f..a0adc212f9 100644 --- a/components/theme/util/getAlphaColor.ts +++ b/components/theme/util/getAlphaColor.ts @@ -5,7 +5,11 @@ function isStableColor(color: number): boolean { } function getAlphaColor(frontColor: string, backgroundColor: string): string { - const { r: fR, g: fG, b: fB } = new TinyColor(frontColor).toRgb(); + const { r: fR, g: fG, b: fB, a: originAlpha } = new TinyColor(frontColor).toRgb(); + if (originAlpha < 1) { + return frontColor; + } + const { r: bR, g: bG, b: bB } = new TinyColor(backgroundColor).toRgb(); for (let fA = 0.01; fA <= 1; fA += 0.01) {