浏览代码

Merge pull request #15377 from grafana/fix/named-colors-palette-default-colors

Fix error caused by named colors that are not part of named colors palette
Torkel Ödegaard 6 年之前
父节点
当前提交
1407691de2

+ 6 - 2
packages/grafana-ui/src/utils/namedColorsPalette.test.ts

@@ -44,8 +44,8 @@ describe('colors', () => {
   });
 
   describe('getColorFromHexRgbOrName', () => {
-    it('returns undefined for unknown color', () => {
-      expect(() => getColorFromHexRgbOrName('aruba-sunshine')).toThrow();
+    it('returns black for unknown color', () => {
+      expect(getColorFromHexRgbOrName('aruba-sunshine')).toBe("#000000");
     });
 
     it('returns dark hex variant for known color if theme not specified', () => {
@@ -64,5 +64,9 @@ describe('colors', () => {
       expect(getColorFromHexRgbOrName('rgb(0,0,0)')).toBe('rgb(0,0,0)');
       expect(getColorFromHexRgbOrName('rgba(0,0,0,1)')).toBe('rgba(0,0,0,1)');
     });
+
+    it('returns hex for named color that is not a part of named colors palette', () => {
+      expect(getColorFromHexRgbOrName('lime')).toBe('#00ff00');
+    });
   });
 });

+ 2 - 1
packages/grafana-ui/src/utils/namedColorsPalette.ts

@@ -1,5 +1,6 @@
 import { flatten } from 'lodash';
 import { GrafanaThemeType } from '../types';
+import tinycolor from 'tinycolor2';
 
 type Hue = 'green' | 'yellow' | 'red' | 'blue' | 'orange' | 'purple';
 
@@ -106,7 +107,7 @@ export const getColorFromHexRgbOrName = (color: string, theme?: GrafanaThemeType
   const colorDefinition = getColorByName(color);
 
   if (!colorDefinition) {
-    throw new Error('Unknown color');
+    return new tinycolor(color).toHexString();
   }
 
   return theme ? colorDefinition.variants[theme] : colorDefinition.variants.dark;