color-utils.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { hsl } from 'd3-color';
  2. import { scaleLinear, scaleOrdinal } from 'd3-scale';
  3. import { schemeCategory10 } from 'd3-scale-chromatic';
  4. const PSEUDO_COLOR = '#b1b1cb';
  5. const hueRange = [20, 330]; // exclude red
  6. const hueScale = scaleLinear().range(hueRange);
  7. const networkColorScale = scaleOrdinal(schemeCategory10);
  8. // map hues to lightness
  9. const lightnessScale = scaleLinear().domain(hueRange).range([0.5, 0.7]);
  10. const startLetterRange = 'A'.charCodeAt();
  11. const endLetterRange = 'Z'.charCodeAt();
  12. const letterRange = endLetterRange - startLetterRange;
  13. /**
  14. * Converts a text to a 360 degree value
  15. */
  16. export function text2degree(text) {
  17. const input = text.substr(0, 2).toUpperCase();
  18. let num = 0;
  19. for (let i = 0; i < input.length; i += 1) {
  20. const charCode = Math.max(Math.min(input[i].charCodeAt(), endLetterRange), startLetterRange);
  21. num += Math.pow(letterRange, input.length - i - 1) * (charCode - startLetterRange);
  22. }
  23. hueScale.domain([0, Math.pow(letterRange, input.length)]);
  24. return hueScale(num);
  25. }
  26. export function colors(text, secondText) {
  27. let hue = text2degree(text);
  28. // skip green and shift to the end of the color wheel
  29. if (hue > 70 && hue < 150) {
  30. hue += 80;
  31. }
  32. const saturation = 0.6;
  33. let lightness = 0.5;
  34. if (secondText) {
  35. // reuse text2degree and feed degree to lightness scale
  36. lightness = lightnessScale(text2degree(secondText));
  37. }
  38. return hsl(hue, saturation, lightness);
  39. }
  40. export function getNeutralColor() {
  41. return PSEUDO_COLOR;
  42. }
  43. export function getNodeColor(text = '', secondText = '', isPseudo = false) {
  44. if (isPseudo) {
  45. return PSEUDO_COLOR;
  46. }
  47. return colors(text, secondText).toString();
  48. }
  49. export function getNodeColorDark(text = '', secondText = '', isPseudo = false) {
  50. if (isPseudo) {
  51. return PSEUDO_COLOR;
  52. }
  53. let color = hsl(colors(text, secondText));
  54. // ensure darkness
  55. if (color.h > 20 && color.h < 120) {
  56. color = color.darker(2);
  57. } else if (hsl.l > 0.7) {
  58. color = color.darker(1.5);
  59. } else {
  60. color = color.darker(1);
  61. }
  62. return color.toString();
  63. }
  64. export function getNetworkColor(text) {
  65. return networkColorScale(text);
  66. }
  67. export function brightenColor(c) {
  68. let color = hsl(c);
  69. if (hsl.l > 0.5) {
  70. color = color.brighter(0.5);
  71. } else {
  72. color = color.brighter(0.8);
  73. }
  74. return color.toString();
  75. }
  76. export function darkenColor(c) {
  77. let color = hsl(c);
  78. if (hsl.l < 0.5) {
  79. color = color.darker(0.5);
  80. } else {
  81. color = color.darker(0.8);
  82. }
  83. return color.toString();
  84. }
  85. const statusColorMap = {
  86. 'running' : 'rgb(0,215,119)',//运行中 绿色
  87. 'closed' : 'rgb(0,0,33)',//已关闭 黑色
  88. 'helm' : 'rgb(0,215,119)',//helm 绿色
  89. 'third_party': "rgb(91,178,250)",
  90. 'undeploy' : 'rgb(112,128,144)',//未部署 石板灰
  91. 'creating' : 'rgb(119,136,153)',//部署中 浅石板灰
  92. 'waitting': 'rgb(246,157,74)',
  93. 'starting' : 'rgb(246,157,74)',//开启中 道奇蓝
  94. 'startting' : 'rgb(246,157,74)',//开启中 道奇蓝
  95. 'checking' : 'rgb(246,157,74)',//检测中 橙色
  96. 'stoping' : 'rgb(32,18,74)',//关闭中 紫色
  97. 'stopping': 'rgb(32,18,74)',//关闭中 紫色
  98. 'upgrade' : 'rgb(0,255,74)',//升级中 洋红
  99. 'unusual' : 'rgb(205,2,0)',//异常 纯红
  100. 'expired' : 'rgb(205,2,0)',//过期 猩红
  101. 'Expired' : 'rgb(205,2,0)', //猩红
  102. 'internet' : 'rgb(91,178,250)',//蓝色
  103. 'The Internet' : 'rgb(91,178,250)',//蓝色
  104. 'Unknow' : 'rgb(205,2,0)', //深粉色
  105. 'unknow' : 'rgb(205,2,0)',//深粉色
  106. 'abnormal':'rgb(205,2,0)',//不正常,纯红
  107. 'some_abnormal':'rgb(205,2,0)',//一些不正常 纯红
  108. 'building':'rgb(0,119,16)',//构建 纯蓝
  109. 'build_failure':'rgb(205,2,0)',//构建失败 纯红
  110. 'RUNNING': 'rgb(0,215,119)',//运行中 绿色 往下应用状态颜色
  111. 'STARTING': 'rgb(246,157,74)',//开启中 道奇蓝
  112. 'CLOSED': 'rgb(0,0,33)',//已关闭 黑色
  113. 'STOPPING': 'rgb(32,18,74)',//关闭中 紫色
  114. 'ABNORMAL': 'rgb(205,2,0)',//异常 纯红
  115. 'PARTIAL_ABNORMAL': 'rgb(205,2,0)',//部分异常 纯红
  116. 'not-configured': 'rgb(112,128,144)',//未配置 石板灰
  117. 'unknown': 'rgb(205,2,0)',//未知 纯红
  118. 'deployed': 'rgb(0,215,119)',//已部署 绿色
  119. "succeeded": 'rgb(0,215,119)', //job、cronjob 已完成 绿色
  120. 'operator': 'rgb(181 182 160)',// operator 类型组件 紫色
  121. }
  122. export function getStatusColor(status) {
  123. return statusColorMap[status] || statusColorMap['internet'];
  124. }
  125. const nodeColors = {
  126. B: '#5B8FF9',
  127. R: '#ff4949',
  128. Y: '#EEBC20',
  129. // G: '#5BD8A6',
  130. G:'#38BC93',
  131. DI: '#A7A7A7',
  132. D:'#5BB2FA'
  133. };
  134. export function setNodeColor(colorValue){
  135. return nodeColors[colorValue] || nodeColors['D'];
  136. }