1// Copyright (C) 2019 The Android Open Source Project 2// 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 15import {ThreadDesc} from './globals'; 16 17export interface Color { 18 c: string; 19 h: number; 20 s: number; 21 l: number; 22} 23 24const MD_PALETTE: Color[] = [ 25 {c: 'red', h: 4, s: 90, l: 58}, 26 {c: 'pink', h: 340, s: 82, l: 52}, 27 {c: 'purple', h: 291, s: 64, l: 42}, 28 {c: 'deep purple', h: 262, s: 52, l: 47}, 29 {c: 'indigo', h: 231, s: 48, l: 48}, 30 {c: 'blue', h: 207, s: 90, l: 54}, 31 {c: 'light blue', h: 199, s: 98, l: 48}, 32 {c: 'cyan', h: 187, s: 100, l: 42}, 33 {c: 'teal', h: 174, s: 100, l: 29}, 34 {c: 'green', h: 122, s: 39, l: 49}, 35 {c: 'light green', h: 88, s: 50, l: 53}, 36 {c: 'lime', h: 66, s: 70, l: 54}, 37 {c: 'amber', h: 45, s: 100, l: 51}, 38 {c: 'orange', h: 36, s: 100, l: 50}, 39 {c: 'deep orange', h: 14, s: 100, l: 57}, 40 {c: 'brown', h: 16, s: 25, l: 38}, 41 {c: 'blue gray', h: 200, s: 18, l: 46}, 42 {c: 'yellow', h: 54, s: 100, l: 62}, 43]; 44 45const GREY_COLOR: Color = { 46 c: 'grey', 47 h: 0, 48 s: 0, 49 l: 62 50}; 51 52function hash(s: string, max: number): number { 53 let hash = 0x811c9dc5 & 0xfffffff; 54 for (let i = 0; i < s.length; i++) { 55 hash ^= s.charCodeAt(i); 56 hash = (hash * 16777619) & 0xffffffff; 57 } 58 return Math.abs(hash) % max; 59} 60 61export function hueForCpu(cpu: number): number { 62 return (128 + (32 * cpu)) % 256; 63} 64 65export function colorForState(state: string): Color { 66 switch (state) { 67 case 'Running': 68 case 'Busy': 69 return {c: 'dark green', h: 120, s: 44, l: 34}; 70 case 'Runnable': 71 case 'R': 72 case 'R+': 73 return {c: 'lime green', h: 75, s: 55, l: 47}; 74 default: 75 return {c: 'light grey', h: 0, s: 0, l: 87}; 76 } 77} 78 79export function colorForTid(tid: number) { 80 const colorIdx = hash(tid.toString(), MD_PALETTE.length); 81 return Object.assign({}, MD_PALETTE[colorIdx]); 82} 83 84export function colorForThread(thread: ThreadDesc|undefined): Color { 85 if (thread === undefined) { 86 return Object.assign({}, GREY_COLOR); 87 } 88 const tid = thread.pid ? thread.pid : thread.tid; 89 return colorForTid(tid); 90} 91