microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
iadavis/pipeline-issue-debugging

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/npm/qsharp/ux/colormap.ts

51lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4export function ColorMap(colorsCount: number): string[] {
5 const colors: string[] = [];
6 const saturationIncrement = 1.4142135623730951; // square root of 2
7 const hueIncrement = 0.618033988749895; // golden ratio
8 const lightnessIncrement = 2.718281828459045; // euler's number
9
10 for (let i = 0; i < colorsCount; i++) {
11 const hue = (i * hueIncrement) % 1;
12 const saturation = (1 - ((i * saturationIncrement) % 1)) * 0.5 + 0.5;
13 const lightness = ((i * lightnessIncrement) % 1) * 0.3 + 0.35;
14 const rgbColor = hslToRgb(hue, saturation, lightness);
15 const hexColor = rgbToHex(rgbColor[0], rgbColor[1], rgbColor[2]);
16 colors.push(hexColor);
17 }
18 return colors;
19}
20
21function hslToRgb(h: number, s: number, l: number): [number, number, number] {
22 let r, g, b;
23
24 if (s === 0) {
25 r = g = b = l; // Grayscale
26 } else {
27 const hue2rgb = (p: number, q: number, t: number) => {
28 if (t < 0) t += 1;
29 if (t > 1) t -= 1;
30 if (t < 1 / 6) return p + (q - p) * 6 * t;
31 if (t < 1 / 2) return q;
32 if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
33 return p;
34 };
35
36 const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
37 const p = 2 * l - q;
38 r = hue2rgb(p, q, h + 1 / 3);
39 g = hue2rgb(p, q, h);
40 b = hue2rgb(p, q, h - 1 / 3);
41 }
42
43 return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
44}
45
46function rgbToHex(r: number, g: number, b: number): string {
47 return `#${((1 << 24) | (r << 16) | (g << 8) | b)
48 .toString(16)
49 .slice(1)
50 .toUpperCase()}`;
51}
52