From ca65d725864d414549dab9a32a4004a48f7de481 Mon Sep 17 00:00:00 2001 From: Manasvini B Suryanarayana Date: Wed, 28 Jun 2023 15:36:20 -0700 Subject: [PATCH] [Vis colors] Update legacy mapped colors in charts plugin to use ouiPaletteColorBlind() (#4398) Signed-off-by: Manasvini B Suryanarayana Co-authored-by: Ashwin P Chandran (cherry picked from commit 5e8a4fddb57ca57e0fea48690662031a22bbdab0) Signed-off-by: Manasvini B Suryanarayana --- .../public/services/colors/color_palette.ts | 44 ------- .../public/services/colors/colors.test.ts | 1 + .../services/colors/colors_palette.test.ts | 117 ------------------ 3 files changed, 1 insertion(+), 161 deletions(-) delete mode 100644 src/plugins/charts/public/services/colors/color_palette.ts delete mode 100644 src/plugins/charts/public/services/colors/colors_palette.test.ts diff --git a/src/plugins/charts/public/services/colors/color_palette.ts b/src/plugins/charts/public/services/colors/color_palette.ts deleted file mode 100644 index d7849ef37d88..000000000000 --- a/src/plugins/charts/public/services/colors/color_palette.ts +++ /dev/null @@ -1,44 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - * - * Any modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you 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 _ from 'lodash'; - -import { euiPaletteColorBlind } from '@elastic/eui'; - -/** - * Generates an array of hex colors the length of the input number - */ -export function createColorPalette(num: number): string[] { - if (!_.isNumber(num)) { - throw new TypeError('ColorPaletteUtilService expects a number'); - } - - return euiPaletteColorBlind({ rotations: Math.ceil(num / 10), direction: 'both' }).slice(0, num); -} diff --git a/src/plugins/charts/public/services/colors/colors.test.ts b/src/plugins/charts/public/services/colors/colors.test.ts index c189b9dc7bae..d41e6e6be6e7 100644 --- a/src/plugins/charts/public/services/colors/colors.test.ts +++ b/src/plugins/charts/public/services/colors/colors.test.ts @@ -31,6 +31,7 @@ import { coreMock } from '../../../../../core/public/mocks'; import { COLOR_MAPPING_SETTING } from '../../../common'; import { euiPaletteColorBlind } from '@elastic/eui'; + import { ColorsService } from './colors'; // Local state for config diff --git a/src/plugins/charts/public/services/colors/colors_palette.test.ts b/src/plugins/charts/public/services/colors/colors_palette.test.ts deleted file mode 100644 index ecb0a24be856..000000000000 --- a/src/plugins/charts/public/services/colors/colors_palette.test.ts +++ /dev/null @@ -1,117 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - * - * Any modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you 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 { createColorPalette } from './color_palette'; -import { euiPaletteColorBlind } from '@elastic/eui'; - -describe('Color Palette', () => { - const num1 = 45; - const num2 = 72; - const num3 = 90; - const string = 'Welcome'; - const bool = true; - const nullValue = null; - const emptyArr: [] = []; - const emptyObject = {}; - let colorPalette: string[]; - - beforeEach(() => { - colorPalette = createColorPalette(num1); - }); - - function isHexValue(value: string): boolean { - // Check if the hex value is valid. - const regex = /^#[0-9a-fA-F]{3}|[0-9a-fA-F]{6}$/; - return regex.test(value) ? true : false; - } - - it('should throw an error if input is not a number', () => { - expect(() => { - // @ts-expect-error - createColorPalette(string); - }).toThrowError(); - - expect(() => { - // @ts-expect-error - createColorPalette(bool); - }).toThrowError(); - - expect(() => { - // @ts-expect-error - createColorPalette(nullValue); - }).toThrowError(); - - expect(() => { - // @ts-expect-error - createColorPalette(emptyArr); - }).toThrowError(); - - expect(() => { - // @ts-expect-error - createColorPalette(emptyObject); - }).toThrowError(); - - expect(() => { - // @ts-expect-error - createColorPalette(); - }).toThrowError(); - }); - - it('should be a function', () => { - expect(typeof createColorPalette).toBe('function'); - }); - - it('should return an array', () => { - expect(colorPalette).toBeInstanceOf(Array); - }); - - it('should return an array of the same length as the input', () => { - expect(colorPalette.length).toBe(num1); - }); - - it('should return the seed color array when input length is 72', () => { - expect(createColorPalette(num2).length).toBe(num2); - expect(isHexValue(createColorPalette(num2)[71])).toBe(true); - }); - - it('should return an array of the same length as the input when input is greater than 72', () => { - expect(createColorPalette(num3).length).toBe(num3); - }); - - it('should create new darker colors when input is greater than 72', () => { - expect(createColorPalette(num3)[72]).not.toEqual(euiPaletteColorBlind()[0]); - }); - - it('should create new colors and convert them correctly', () => { - expect(createColorPalette(num3).length).toBe(num3); - expect(createColorPalette(num3)[72]).not.toEqual(euiPaletteColorBlind()[9]); - expect(isHexValue(createColorPalette(num3)[89])).toBe(true); - }); -});