namedColorsPalette.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { flatten } from 'lodash';
  2. import { GrafanaThemeType } from '../types';
  3. type Hue = 'green' | 'yellow' | 'red' | 'blue' | 'orange' | 'purple';
  4. export type Color =
  5. | 'green'
  6. | 'dark-green'
  7. | 'semi-dark-green'
  8. | 'light-green'
  9. | 'super-light-green'
  10. | 'yellow'
  11. | 'dark-yellow'
  12. | 'semi-dark-yellow'
  13. | 'light-yellow'
  14. | 'super-light-yellow'
  15. | 'red'
  16. | 'dark-red'
  17. | 'semi-dark-red'
  18. | 'light-red'
  19. | 'super-light-red'
  20. | 'blue'
  21. | 'dark-blue'
  22. | 'semi-dark-blue'
  23. | 'light-blue'
  24. | 'super-light-blue'
  25. | 'orange'
  26. | 'dark-orange'
  27. | 'semi-dark-orange'
  28. | 'light-orange'
  29. | 'super-light-orange'
  30. | 'purple'
  31. | 'dark-purple'
  32. | 'semi-dark-purple'
  33. | 'light-purple'
  34. | 'super-light-purple';
  35. type ThemeVariants = {
  36. dark: string;
  37. light: string;
  38. };
  39. export type ColorDefinition = {
  40. hue: Hue;
  41. isPrimary?: boolean;
  42. name: Color;
  43. variants: ThemeVariants;
  44. };
  45. let colorsPaletteInstance: Map<Hue, ColorDefinition[]>;
  46. const buildColorDefinition = (
  47. hue: Hue,
  48. name: Color,
  49. [light, dark]: string[],
  50. isPrimary?: boolean
  51. ): ColorDefinition => ({
  52. hue,
  53. name,
  54. variants: {
  55. light,
  56. dark,
  57. },
  58. isPrimary: !!isPrimary,
  59. });
  60. export const getColorDefinitionByName = (name: Color): ColorDefinition => {
  61. return flatten(Array.from(getNamedColorPalette().values())).filter(definition => definition.name === name)[0];
  62. };
  63. export const getColorDefinition = (hex: string, theme: GrafanaThemeType): ColorDefinition | undefined => {
  64. return flatten(Array.from(getNamedColorPalette().values())).filter(definition => definition.variants[theme] === hex)[0];
  65. };
  66. const isHex = (color: string) => {
  67. const hexRegex = /^((0x){0,1}|#{0,1})([0-9A-F]{8}|[0-9A-F]{6}|[0-9A-F]{3})$/gi;
  68. return hexRegex.test(color);
  69. };
  70. export const getColorName = (color?: string, theme?: GrafanaThemeType): Color | undefined => {
  71. if (!color) {
  72. return undefined;
  73. }
  74. if (color.indexOf('rgb') > -1) {
  75. return undefined;
  76. }
  77. if (isHex(color)) {
  78. const definition = getColorDefinition(color, theme || GrafanaThemeType.Dark);
  79. return definition ? definition.name : undefined;
  80. }
  81. return color as Color;
  82. };
  83. export const getColorByName = (colorName: string) => {
  84. const definition = flatten(Array.from(getNamedColorPalette().values())).filter(definition => definition.name === colorName);
  85. return definition.length > 0 ? definition[0] : undefined;
  86. };
  87. export const getColorFromHexRgbOrName = (color: string, theme?: GrafanaThemeType): string => {
  88. if (color.indexOf('rgb') > -1 || isHex(color)) {
  89. return color;
  90. }
  91. const colorDefinition = getColorByName(color);
  92. if (!colorDefinition) {
  93. throw new Error('Unknown color');
  94. }
  95. return theme ? colorDefinition.variants[theme] : colorDefinition.variants.dark;
  96. };
  97. export const getColorForTheme = (color: ColorDefinition, theme?: GrafanaThemeType) => {
  98. return theme ? color.variants[theme] : color.variants.dark;
  99. };
  100. const buildNamedColorsPalette = () => {
  101. const palette = new Map<Hue, ColorDefinition[]>();
  102. const BasicGreen = buildColorDefinition('green', 'green', ['#56A64B', '#73BF69'], true);
  103. const DarkGreen = buildColorDefinition('green', 'dark-green', ['#19730E', '#37872D']);
  104. const SemiDarkGreen = buildColorDefinition('green', 'semi-dark-green', ['#37872D', '#56A64B']);
  105. const LightGreen = buildColorDefinition('green', 'light-green', ['#73BF69', '#96D98D']);
  106. const SuperLightGreen = buildColorDefinition('green', 'super-light-green', ['#96D98D', '#C8F2C2']);
  107. const BasicYellow = buildColorDefinition('yellow', 'yellow', ['#F2CC0C', '#FADE2A'], true);
  108. const DarkYellow = buildColorDefinition('yellow', 'dark-yellow', ['#CC9D00', '#E0B400']);
  109. const SemiDarkYellow = buildColorDefinition('yellow', 'semi-dark-yellow', ['#E0B400', '#F2CC0C']);
  110. const LightYellow = buildColorDefinition('yellow', 'light-yellow', ['#FADE2A', '#FFEE52']);
  111. const SuperLightYellow = buildColorDefinition('yellow', 'super-light-yellow', ['#FFEE52', '#FFF899']);
  112. const BasicRed = buildColorDefinition('red', 'red', ['#E02F44', '#F2495C'], true);
  113. const DarkRed = buildColorDefinition('red', 'dark-red', ['#AD0317', '#C4162A']);
  114. const SemiDarkRed = buildColorDefinition('red', 'semi-dark-red', ['#C4162A', '#E02F44']);
  115. const LightRed = buildColorDefinition('red', 'light-red', ['#F2495C', '#FF7383']);
  116. const SuperLightRed = buildColorDefinition('red', 'super-light-red', ['#FF7383', '#FFA6B0']);
  117. const BasicBlue = buildColorDefinition('blue', 'blue', ['#3274D9', '#5794F2'], true);
  118. const DarkBlue = buildColorDefinition('blue', 'dark-blue', ['#1250B0', '#1F60C4']);
  119. const SemiDarkBlue = buildColorDefinition('blue', 'semi-dark-blue', ['#1F60C4', '#3274D9']);
  120. const LightBlue = buildColorDefinition('blue', 'light-blue', ['#5794F2', '#8AB8FF']);
  121. const SuperLightBlue = buildColorDefinition('blue', 'super-light-blue', ['#8AB8FF', '#C0D8FF']);
  122. const BasicOrange = buildColorDefinition('orange', 'orange', ['#FF780A', '#FF9830'], true);
  123. const DarkOrange = buildColorDefinition('orange', 'dark-orange', ['#E55400', '#FA6400']);
  124. const SemiDarkOrange = buildColorDefinition('orange', 'semi-dark-orange', ['#FA6400', '#FF780A']);
  125. const LightOrange = buildColorDefinition('orange', 'light-orange', ['#FF9830', '#FFB357']);
  126. const SuperLightOrange = buildColorDefinition('orange', 'super-light-orange', ['#FFB357', '#FFCB7D']);
  127. const BasicPurple = buildColorDefinition('purple', 'purple', ['#A352CC', '#B877D9'], true);
  128. const DarkPurple = buildColorDefinition('purple', 'dark-purple', ['#7C2EA3', '#8F3BB8']);
  129. const SemiDarkPurple = buildColorDefinition('purple', 'semi-dark-purple', ['#8F3BB8', '#A352CC']);
  130. const LightPurple = buildColorDefinition('purple', 'light-purple', ['#B877D9', '#CA95E5']);
  131. const SuperLightPurple = buildColorDefinition('purple', 'super-light-purple', ['#CA95E5', '#DEB6F2']);
  132. const greens = [BasicGreen, DarkGreen, SemiDarkGreen, LightGreen, SuperLightGreen];
  133. const yellows = [BasicYellow, DarkYellow, SemiDarkYellow, LightYellow, SuperLightYellow];
  134. const reds = [BasicRed, DarkRed, SemiDarkRed, LightRed, SuperLightRed];
  135. const blues = [BasicBlue, DarkBlue, SemiDarkBlue, LightBlue, SuperLightBlue];
  136. const oranges = [BasicOrange, DarkOrange, SemiDarkOrange, LightOrange, SuperLightOrange];
  137. const purples = [BasicPurple, DarkPurple, SemiDarkPurple, LightPurple, SuperLightPurple];
  138. palette.set('green', greens);
  139. palette.set('yellow', yellows);
  140. palette.set('red', reds);
  141. palette.set('blue', blues);
  142. palette.set('orange', oranges);
  143. palette.set('purple', purples);
  144. return palette;
  145. };
  146. export const getNamedColorPalette = () => {
  147. if (colorsPaletteInstance) {
  148. return colorsPaletteInstance;
  149. }
  150. colorsPaletteInstance = buildNamedColorsPalette();
  151. return colorsPaletteInstance;
  152. };