1 /* 2 * Copyright (c) 2021 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 16 package ohos.devtools.views.trace.util; 17 18 import ohos.devtools.views.trace.bean.CpuData; 19 20 import java.awt.Color; 21 import java.util.Map; 22 import java.util.concurrent.ConcurrentHashMap; 23 24 /** 25 * Color tool 26 * 27 * @since 2021/04/22 12:25 28 */ 29 public final class ColorUtils { 30 /** 31 * Gray color object 32 */ 33 public static final Color GREY_COLOR = Color.getHSBColor(0, 0, 62); // grey 34 35 /** 36 * Color array of all current columns 37 */ 38 public static final Color[] MD_PALETTE = new Color[] { 39 new Color(0x3391ff), // red 40 new Color(0x0076ff), // pink 41 new Color(0x66adff), // purple 42 new Color(0x2db3aa), // deep purple 43 new Color(0x008078), // indigo 44 new Color(0x73e6de), // blue 45 new Color(0x535da6), // light blue 46 new Color(0x38428c), // cyan 47 new Color(0x7a84cc), // teal 48 new Color(0xff9201), // green 49 new Color(0xff7500), // light green 50 new Color(0xffab40), // lime 51 new Color(0x2db4e2), // amber 0xffc105 52 new Color(0x0094c6), // orange 53 new Color(0x7cdeff), // deep orange 54 new Color(0xffd44a), // brown 55 new Color(0xfbbf00), // blue gray 56 new Color(0xffe593), // yellow 0xffec3d 57 }; 58 59 /** 60 * Current method color array 61 */ 62 public static final Color[] FUNC_COLOR = new Color[] {new Color(0x3391ff), // purple 63 new Color(0x2db4e2), new Color(0x2db3aa), // deep purple 64 new Color(0xffd44a), new Color(0x535da6), // indigo 65 new Color(0x008078), // blue 66 new Color(0xff9201), new Color(0x38428c)}; 67 68 private static Map<Integer, Color> colorHashMap = new ConcurrentHashMap(); 69 ColorUtils()70 private ColorUtils() { 71 super(); 72 } 73 74 /** 75 * Get color according to id 76 * 77 * @param id id 78 * @return Color Color 79 */ getColor(final int id)80 public static Color getColor(final int id) { 81 if (colorHashMap.containsKey(id)) { 82 return colorHashMap.get(id); 83 } else { 84 final int red = ((id * 10000000) & 0xff0000) >> 16; 85 final int green = ((id * 10000000) & 0x00ff00) >> 8; 86 final int blue = id * 10000000 & 0x0000ff; 87 final Color color = new Color(red, green, blue, 255); 88 colorHashMap.put(id, color); 89 return color; 90 } 91 } 92 93 /** 94 * Get the color value according to the length of the string 95 * 96 * @param str str 97 * @param max max 98 * @return int 99 */ hash(final String str, final int max)100 public static int hash(final String str, final int max) { 101 final int colorA = 0x811c9dc5; 102 final int colorB = 0xfffffff; 103 final int colorC = 16777619; 104 final int colorD = 0xffffffff; 105 int hash = colorA & colorB; 106 for (int index = 0; index < str.length(); index++) { 107 hash ^= str.charAt(index); 108 hash = (hash * colorC) & colorD; 109 } 110 return Math.abs(hash) % max; 111 } 112 113 /** 114 * Get color based on cpu object data 115 * 116 * @param thread thread 117 * @return Color 118 */ colorForThread(final CpuData thread)119 public static Color colorForThread(final CpuData thread) { 120 if (thread == null) { 121 return GREY_COLOR; 122 } 123 int tid = thread.getProcessId() >= 0 ? thread.getProcessId() : thread.getTid(); 124 return colorForTid(tid); 125 } 126 127 /** 128 * Get color according to tid 129 * 130 * @param tid tid 131 * @return Color 132 */ colorForTid(final int tid)133 public static Color colorForTid(final int tid) { 134 int colorIdx = hash(String.valueOf(tid), MD_PALETTE.length); 135 return MD_PALETTE[colorIdx]; 136 } 137 } 138