1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { CpuStruct } from '../../../database/ui-worker/ProcedureWorkerCPU.js'; 17 18export class ColorUtils { 19 public static GREY_COLOR: string = '#f0f0f0'; 20 21 public static FUNC_COLOR_A: Array<string> = [ 22 '#40b3e7', 23 '#23b0e7', 24 '#8d9171', 25 '#FF0066', 26 '#7a9160', 27 '#9fafc4', 28 '#8a8a8b', 29 '#e05b52', 30 '#9bb87a', 31 '#ebc247', 32 '#c2cc66', 33 '#a16a40', 34 '#a94eb9', 35 '#aa4fba', 36 ]; 37 public static FUNC_COLOR_B: Array<string> = [ 38 '#9785D3', 39 '#A27F7E', 40 '#00bdd6', 41 '#94B5F4', 42 '#B282F6', 43 '#E97978', 44 '#7AD7E6', 45 '#A1C38A', 46 '#DB8E86', 47 '#42B7A4', 48 '#AACEA0', 49 '#E69553', 50 '#7EC6BB', 51 '#8d9171', 52 53 ]; 54 55 public static ANIMATION_COLOR: Array<string> = [ 56 '#ECECEC', 57 '#FE3000', 58 '#61CFBE', 59 '#000', 60 '#FFFFFF', 61 '#C6D9F2', 62 '#BFEBE5', 63 '#0A59F7', 64 '#25ACF5', 65 '#FFFFFF' 66 ]; 67 68 public static JANK_COLOR: Array<string> = ['#42A14D', '#C0CE85', '#FF651D', '#E8BE44', '#009DFA', '#E97978', '#A8D1F4']; 69 public static MD_PALETTE: Array<string> = ColorUtils.FUNC_COLOR_B; 70 public static FUNC_COLOR: Array<string> = ColorUtils.FUNC_COLOR_B; 71 72 public static hash(str: string, max: number): number { 73 let colorA: number = 0x811c9dc5; 74 let colorB: number = 0xfffffff; 75 let colorC: number = 16777619; 76 let colorD: number = 0xffffffff; 77 let hash: number = colorA & colorB; 78 79 for (let index: number = 0; index < str.length; index++) { 80 hash ^= str.charCodeAt(index); 81 hash = (hash * colorC) & colorD; 82 } 83 return Math.abs(hash) % max; 84 } 85 86 public static colorForThread(thread: CpuStruct): string { 87 if (thread == null) { 88 return ColorUtils.GREY_COLOR; 89 } 90 let tid: number | undefined | null = (thread.processId || -1) >= 0 ? thread.processId : thread.tid; 91 return ColorUtils.colorForTid(tid || 0); 92 } 93 94 public static colorForTid(tid: number): string { 95 let colorIdx: number = ColorUtils.hash(`${tid}`, ColorUtils.MD_PALETTE.length); 96 return ColorUtils.MD_PALETTE[colorIdx]; 97 } 98 99 public static colorForName(name: string): string { 100 let colorIdx: number = ColorUtils.hash(name, ColorUtils.MD_PALETTE.length); 101 return ColorUtils.MD_PALETTE[colorIdx]; 102 } 103 104 public static formatNumberComma(str: number): string { 105 if (str === undefined || str === null) return ''; 106 let unit = str >= 0 ? '' : '-'; 107 let l = Math.abs(str).toString().split('').reverse(); 108 let t: string = ''; 109 for (let i = 0; i < l.length; i++) { 110 t += l[i] + ((i + 1) % 3 == 0 && i + 1 != l.length ? ',' : ''); 111 } 112 return unit + t.split('').reverse().join(''); 113 } 114 115 public static hashFunc(str: string, depth: number, max: number): number { 116 let colorA: number = 0x811c9dc5; 117 let colorB: number = 0xfffffff; 118 let colorC: number = 16777619; 119 let colorD: number = 0xffffffff; 120 let hash: number = colorA & colorB; 121 let st = str.replace(/[0-9]+/g, ''); 122 for (let index: number = 0; index < st.length; index++) { 123 hash ^= st.charCodeAt(index); 124 hash = (hash * colorC) & colorD; 125 } 126 return (Math.abs(hash) + depth) % max; 127 } 128 129 public static funcTextColor(val: string) { 130 var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; 131 // 把颜色值变成小写 132 var color = val.toLowerCase(); 133 var result = ''; 134 if (reg.test(color)) { 135 if (color.length === 4) { 136 var colorNew = '#'; 137 for (var i = 1; i < 4; i += 1) { 138 colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1)); 139 } 140 color = colorNew; 141 } 142 var colorChange = []; 143 for (var i = 1; i < 7; i += 2) { 144 colorChange.push(parseInt('0x' + color.slice(i, i + 2))); 145 } 146 var grayLevel = colorChange[0] * 0.299 + colorChange[1] * 0.587 + colorChange[2] * 0.114; 147 if (grayLevel >= 150) { 148 //浅色模式 149 return '#000'; 150 } else { 151 return '#fff'; 152 } 153 } else { 154 result = '无效'; 155 return result; 156 } 157 } 158} 159