Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(color): fix short hex color error & add unit test (#5197)
VicKun169f10607f23
diff --git a/packages/engine-render/src/services/__tests__/canvas-color.spec.ts b/packages/engine-render/src/services/__tests__/canvas-color.spec.tsnew file mode 100644index 0000000000..6e91d23364--- /dev/null+++ b/packages/engine-render/src/services/__tests__/canvas-color.spec.ts@@ -0,0 +1,94 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { Injector } from '@univerjs/core';+import { ThemeService } from '@univerjs/core';+import { ICanvasColorService } from '@univerjs/engine-render';+import { beforeEach, describe, expect, it } from 'vitest';+import { createCanvasColorTestBed } from './create-canvas-color-test-bed';++describe('Test Canvas Color', () => {+ let get: Injector['get'];+ let canvasColorService: ICanvasColorService;+ let themeService: ThemeService;++ beforeEach(() => {+ const testBed = createCanvasColorTestBed();+ get = testBed.get;+ canvasColorService = get(ICanvasColorService);+ themeService = get(ThemeService);+ });++ it('test hex color', async () => {+ const color = '#FF0000';+ themeService.setDarkMode(false);+ const invertedColor = canvasColorService.getRenderColor(color);+ expect(invertedColor).toEqual(color);+ themeService.setDarkMode(true);+ const invertedColor2 = canvasColorService.getRenderColor(color);+ expect(invertedColor2).toEqual('#ff5555');+ });++ it('test rgb color', async () => {+ const color = 'rgb(255, 0, 0)';+ themeService.setDarkMode(false);+ const invertedColor = canvasColorService.getRenderColor(color);+ expect(invertedColor).toEqual(color);+ themeService.setDarkMode(true);+ const invertedColor2 = canvasColorService.getRenderColor(color);+ expect(invertedColor2).toEqual('rgb(255,85,85)');+ });++ it('test rgba color', async () => {+ const color = 'rgba(255, 0, 0, 1)';+ themeService.setDarkMode(false);+ const invertedColor = canvasColorService.getRenderColor(color);+ expect(invertedColor).toEqual(color);+ themeService.setDarkMode(true);+ const invertedColor2 = canvasColorService.getRenderColor(color);+ expect(invertedColor2).toEqual('rgba(255,85,85, 1)');+ });++ it('test short hex color', async () => {+ const color = '#ccc';+ themeService.setDarkMode(false);+ const invertedColor = canvasColorService.getRenderColor(color);+ expect(invertedColor).toEqual(color);+ themeService.setDarkMode(true);+ const invertedColor2 = canvasColorService.getRenderColor(color);+ expect(invertedColor2).toEqual('#333333');+ });++ it('test hex color with alpha', async () => {+ const color = '#ccccccff';+ themeService.setDarkMode(false);+ const invertedColor = canvasColorService.getRenderColor(color);+ expect(invertedColor).toEqual(color);+ themeService.setDarkMode(true);+ const invertedColor2 = canvasColorService.getRenderColor(color);+ expect(invertedColor2).toEqual('#333333ff');+ });++ it('test short hex color with alpha', async () => {+ const color = '#cccf';+ themeService.setDarkMode(false);+ const invertedColor = canvasColorService.getRenderColor(color);+ expect(invertedColor).toEqual(color);+ themeService.setDarkMode(true);+ const invertedColor2 = canvasColorService.getRenderColor(color);+ expect(invertedColor2).toEqual('#333333ff');+ });+});diff --git a/packages/engine-render/src/services/__tests__/create-canvas-color-test-bed.ts b/packages/engine-render/src/services/__tests__/create-canvas-color-test-bed.tsnew file mode 100644index 0000000000..aa6fc8f4fa--- /dev/null+++ b/packages/engine-render/src/services/__tests__/create-canvas-color-test-bed.ts@@ -0,0 +1,157 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { Dependency, IWorkbookData, UnitModel } from '@univerjs/core';+import {+ ILogService,+ Inject,+ Injector,+ IUniverInstanceService,+ LocaleType,+ LogLevel,+ Plugin,+ Univer,+ UniverInstanceType,+} from '@univerjs/core';+import { FUniver } from '@univerjs/core/facade';+import { CanvasColorService, ICanvasColorService } from '@univerjs/engine-render';++function getTestWorkbookDataDemo(): IWorkbookData {+ return {+ id: 'test',+ appVersion: '3.0.0-alpha',+ sheets: {+ sheet1: {+ id: 'sheet1',+ name: 'sheet1',+ cellData: {+ 0: {+ 3: {+ f: '=SUM(A1)',+ si: '3e4r5t',+ },+ 4: {+ v: 123,+ t: 2,+ },+ },+ 1: {+ 3: {+ f: '=SUM(A2)',+ si: 'OSPtzm',+ },+ },+ 2: {+ 3: {+ si: 'OSPtzm',+ },+ },+ 3: {+ 3: {+ si: 'OSPtzm',+ },+ },+ },+ rowCount: 100,+ columnCount: 100,+ },+ },+ locale: LocaleType.ZH_CN,+ name: '',+ sheetOrder: [],+ styles: {},+ resources: [+ {+ name: 'SHEET_CONDITIONAL_FORMATTING_PLUGIN',+ data: '{"sheet1":[{"cfId":"AEGZdW8C","ranges":[{"startRow":2,"startColumn":1,"endRow":5,"endColumn":5,"startAbsoluteRefType":0,"endAbsoluteRefType":0,"rangeType":0}],"rule":{"type":"highlightCell","subType":"text","operator":"containsText","style":{"cl":{"rgb":"#2f56ef"},"bg":{"rgb":"#e8ecfc"}},"value":""},"stopIfTrue":false},{"cfId":"4ICEXdJj","ranges":[{"startRow":4,"startColumn":1,"endRow":7,"endColumn":7,"startAbsoluteRefType":0,"endAbsoluteRefType":0,"rangeType":0}],"rule":{"type":"highlightCell","subType":"text","operator":"containsText","style":{"cl":{"rgb":"#2f56ef"},"bg":{"rgb":"#e8ecfc"}},"value":""},"stopIfTrue":false},{"cfId":"geCv018z","ranges":[{"startRow":11,"startColumn":1,"endRow":12,"endColumn":5,"startAbsoluteRefType":0,"endAbsoluteRefType":0,"rangeType":0}],"rule":{"type":"highlightCell","subType":"text","operator":"containsText","style":{"cl":{"rgb":"#2f56ef"},"bg":{"rgb":"#e8ecfc"}},"value":""},"stopIfTrue":false}]}',+ },+ ],+ };+}++export interface ITestBed {+ univer: Univer;+ get: Injector['get'];+ sheet: UnitModel<IWorkbookData>;+ univerAPI: FUniver;+ injector: Injector;+}++export function createCanvasColorTestBed(workbookData?: IWorkbookData, dependencies?: Dependency[]): ITestBed {+ const univer = new Univer();+ const injector = univer.__getInjector();++ class TestPlugin extends Plugin {+ static override pluginName = 'test-plugin';+ static override type = UniverInstanceType.UNIVER_SHEET;++ constructor(+ _config: undefined,+ @Inject(Injector) override readonly _injector: Injector+ ) {+ super();+ }++ override onStarting(): void {+ const injector = this._injector;+ // injector.add([ThemeService]);+ injector.add([ICanvasColorService, { useClass: CanvasColorService }]);++ dependencies?.forEach((d) => injector.add(d));+ }++ override onReady(): void {++ }+ }++ univer.registerPlugin(TestPlugin);++ const sheet = univer.createUnit<IWorkbookData, UnitModel<IWorkbookData>>(UniverInstanceType.UNIVER_SHEET, workbookData || getTestWorkbookDataDemo());+ const univerInstanceService = injector.get(IUniverInstanceService);+ univerInstanceService.focusUnit('test');++ // set log level+ const logService = injector.get(ILogService);+ logService.setLogLevel(LogLevel.SILENT); // NOTE: change this to `LogLevel.VERBOSE` to debug tests via logs++ // init data validation+ const univerAPI = FUniver.newAPI(injector);++ return {+ univer,+ get: injector.get.bind(injector),+ sheet,+ univerAPI,+ injector,+ };+}diff --git a/packages/engine-render/src/services/canvas-color.service.ts b/packages/engine-render/src/services/canvas-color.service.tsindex 57fdf3c225..b609afe9ff 100644--- a/packages/engine-render/src/services/canvas-color.service.ts+++ b/packages/engine-render/src/services/canvas-color.service.ts@@ -62,9 +62,9 @@ export class CanvasColorService extends Disposable implements ICanvasColorServicconst invertedColor = this._invertAlgo(hexToRgb(color));cachedColor = rgbToHex(invertedColor);- // append alpha channel if the original has alpha channel- if (color.length === 4) {- const alpha = color.charAt(3);+ // append alpha channel if the original has alpha channel , such as #cccf+ if (color.length === 5) {+ const alpha = color.charAt(4);cachedColor += alpha + alpha;}