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 16export function ns2s(ns: number): string { 17 let second1 = 1_000_000_000; // 1 second 18 let millisecond1 = 1_000_000; // 1 millisecond 19 let microsecond1 = 1_000; // 1 microsecond 20 let nanosecond1 = 1000.0; 21 let res; 22 if (ns >= second1) { 23 res = (ns / 1000 / 1000 / 1000).toFixed(1) + " s"; 24 } else if (ns >= millisecond1) { 25 res = (ns / 1000 / 1000).toFixed(1) + " ms"; 26 } else if (ns >= microsecond1) { 27 res = (ns / 1000).toFixed(1) + " μs"; 28 } else if (ns > 0) { 29 res = ns.toFixed(1) + " ns"; 30 } else { 31 res = ns.toFixed(1) + " s"; 32 } 33 return res; 34} 35 36export function ns2x(ns: number, startNS: number, endNS: number, duration: number, rect: any) { 37 if (endNS == 0) { 38 endNS = duration; 39 } 40 let xSize: number = (ns - startNS) * rect.width / (endNS - startNS); 41 if (xSize < 0) { 42 xSize = 0; 43 } else if (xSize > rect.width) { 44 xSize = rect.width; 45 } 46 return xSize; 47} 48 49export class Rect { 50 x: number = 0 51 y: number = 0 52 width: number = 0 53 height: number = 0 54 55 constructor(x: number, y: number, width: number, height: number) { 56 this.x = x; 57 this.y = y; 58 this.width = width; 59 this.height = height; 60 } 61 62 contains(x: number, y: number): boolean { 63 return this.x <= x && x <= this.x + this.width && this.y <= y && y <= this.y + this.height; 64 } 65 static contains(rect:Rect,x: number, y: number): boolean { 66 return rect.x <= x && x <= rect.x + rect.width && rect.y <= y && y <= rect.y + rect.height; 67 } 68 69 containsWithPadding(x: number, y: number, paddingLeftRight: number, paddingTopBottom: number): boolean { 70 return this.x + paddingLeftRight <= x 71 && x <= this.x + this.width - paddingLeftRight 72 && this.y + paddingTopBottom <= y 73 && y <= this.y + this.height - paddingTopBottom; 74 } 75 static containsWithPadding(rect:Rect,x: number, y: number, paddingLeftRight: number, paddingTopBottom: number): boolean { 76 return rect.x + paddingLeftRight <= x 77 && x <= rect.x + rect.width - paddingLeftRight 78 && rect.y + paddingTopBottom <= y 79 && y <= rect.y + rect.height - paddingTopBottom; 80 } 81 82 containsWithMargin(x: number, y: number, t: number, r: number, b: number, l: number): boolean { 83 return this.x - l <= x 84 && x <= this.x + this.width + r 85 && this.y - t <= y 86 && y <= this.y + this.height + b; 87 } 88 static containsWithMargin(rect:Rect,x: number, y: number, t: number, r: number, b: number, l: number): boolean { 89 return rect.x - l <= x 90 && x <= rect.x + rect.width + r 91 && rect.y - t <= y 92 && y <= rect.y + rect.height + b; 93 } 94 95 /** 96 * 判断是否相交 97 * @param rect 98 */ 99 intersect(rect: Rect): boolean { 100 let maxX = this.x + this.width >= rect.x + rect.width ? this.x + this.width : rect.x + rect.width; 101 let maxY = this.y + this.height >= rect.y + rect.height ? this.y + this.height : rect.y + rect.height; 102 let minX = this.x <= rect.x ? this.x : rect.x; 103 let minY = this.y <= rect.y ? this.y : rect.y; 104 if (maxX - minX <= rect.width + this.width && maxY - minY <= this.height + rect.height){ 105 return true; 106 }else{ 107 return false; 108 } 109 } 110 static intersect(r1:Rect,rect: Rect): boolean { 111 let maxX = r1.x + r1.width >= rect.x + rect.width ? r1.x + r1.width : rect.x + rect.width; 112 let maxY = r1.y + r1.height >= rect.y + rect.height ? r1.y + r1.height : rect.y + rect.height; 113 let minX = r1.x <= rect.x ? r1.x : rect.x; 114 let minY = r1.y <= rect.y ? r1.y : rect.y; 115 if (maxX - minX <= rect.width + r1.width && maxY - minY <= r1.height + rect.height){ 116 return true; 117 }else{ 118 return false; 119 } 120 } 121} 122 123export class Point { 124 x: number = 0 125 y: number = 0 126 127 constructor(x: number, y: number) { 128 this.x = x; 129 this.y = y; 130 } 131} 132 133export class BaseStruct { 134 frame: Rect | undefined 135 isHover: boolean = false; 136} 137 138 139export class ColorUtils { 140 // public static GREY_COLOR:string = Color.getHSBColor(0, 0, 62); // grey 141 public static GREY_COLOR:string = "#f0f0f0" 142 /** 143 * Color array of all current columns 144 */ 145 public static MD_PALETTE: Array<string> = [ 146 "#3391ff",// red 147 "#0076ff",// pink 148 "#66adff",// purple 149 "#2db3aa",// deep purple 150 "#008078",// indigo 151 "#73e6de",// blue 152 "#535da6",// light blue 153 "#38428c", // cyan 154 "#7a84cc",// teal 155 "#ff9201",// green 156 "#ff7500",// light green 157 "#ffab40",// lime 158 "#2db4e2",// amber 0xffc105 159 "#0094c6", // orange 160 "#7cdeff",// deep orange 161 "#ffd44a", // brown 162 "#fbbf00",// blue gray 163 "#ffe593",// yellow 0xffec3d 164 ]; 165 public static FUNC_COLOR:Array<string> = [ 166 "#3391ff", // purple 167 "#2db4e2", 168 "#2db3aa", // deep purple 169 "#ffd44a", 170 "#535da6", // indigo 171 "#008078", // blue 172 "#ff9201", 173 "#38428c"]; 174 175 /** 176 * Get the color value according to the length of the string 177 * 178 * @param str str 179 * @param max max 180 * @return int 181 */ 182 public static hash(str: string, max: number): number { 183 let colorA: number = 0x811c9dc5; 184 let colorB: number = 0xfffffff; 185 let colorC: number = 16777619; 186 let colorD: number = 0xffffffff; 187 let hash: number = colorA & colorB; 188 189 for (let index: number = 0; index < str.length; index++) { 190 hash ^= str.charCodeAt(index); 191 hash = (hash * colorC) & colorD; 192 } 193 return Math.abs(hash) % max; 194 } 195 196 /** 197 * Get color according to tid 198 * 199 * @param tid tid 200 * @return Color 201 */ 202 public static colorForTid(tid:number):string { 203 let colorIdx:number = ColorUtils.hash(`${tid}`, ColorUtils.MD_PALETTE.length); 204 return ColorUtils.MD_PALETTE[colorIdx]; 205 } 206 207 public static formatNumberComma(str:number):string{ 208 let l = str.toString().split("").reverse(); 209 let t:string = ""; 210 for(let i = 0; i < l.length; i ++ ) 211 { 212 t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : ""); 213 } 214 return t.split("").reverse().join("") 215 } 216} 217 218export function drawLines(ctx: any,xs:Array<any>,height:number,lineColor:string) { 219 if (ctx) { 220 ctx.lineWidth = 1; 221 ctx.strokeStyle = lineColor||"#dadada"; 222 xs?.forEach(it => { 223 ctx.moveTo(Math.floor(it), 0) 224 ctx.lineTo(Math.floor(it), height) 225 }) 226 ctx.stroke(); 227 } 228} 229 230