mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 10:56:30 +00:00
better code coverage colors
This commit is contained in:
parent
3032f0867c
commit
c5ec4a9970
@ -1,5 +1,28 @@
|
||||
import * as data from "./data.ts";
|
||||
|
||||
type Color = { r: number; g: number; b: number };
|
||||
|
||||
function lerp(ratio: number, start: number, middle: number, end: number) {
|
||||
if (ratio < 0.5) {
|
||||
return (1 - ratio) * start + ratio * middle;
|
||||
} else {
|
||||
return (1 - ratio) * middle + ratio * end;
|
||||
}
|
||||
}
|
||||
|
||||
function colorLerp(
|
||||
ratio: number,
|
||||
start: Color,
|
||||
middle: Color,
|
||||
end: Color,
|
||||
): Color {
|
||||
return {
|
||||
r: lerp(ratio, start.r, middle.r, end.r),
|
||||
g: lerp(ratio, start.g, middle.g, end.g),
|
||||
b: lerp(ratio, start.b, middle.b, end.b),
|
||||
};
|
||||
}
|
||||
|
||||
function loadCodeCoverage(
|
||||
text: string,
|
||||
input: data.CodeCovEntry[],
|
||||
@ -17,6 +40,9 @@ function loadCodeCoverage(
|
||||
const elements: HTMLElement[] = [];
|
||||
let line = 1;
|
||||
let col = 1;
|
||||
const maxCovers = entries.map((v) => v.covers).reduce((acc, v) =>
|
||||
acc > v ? acc : v
|
||||
);
|
||||
for (let index = 0; index < text.length; ++index) {
|
||||
if (text[index] === "\n") {
|
||||
col = 1;
|
||||
@ -32,11 +58,23 @@ function loadCodeCoverage(
|
||||
}
|
||||
charEntries[`${line}-${col}`] = entry;
|
||||
|
||||
const color = (ratio: number) =>
|
||||
`rgba(${255 - 255 * ratio}, ${255 * ratio}, 125, 0.5)`;
|
||||
const backgroundColor = (ratio: number) => {
|
||||
const clr = colorLerp(ratio, { r: 42, g: 121, b: 82 }, {
|
||||
r: 247,
|
||||
g: 203,
|
||||
b: 21,
|
||||
}, {
|
||||
r: 167,
|
||||
g: 29,
|
||||
b: 49,
|
||||
});
|
||||
return `rgb(${clr.r}, ${clr.g}, ${clr.b})`;
|
||||
};
|
||||
|
||||
const span = document.createElement("span");
|
||||
span.style.backgroundColor = color(Math.min(entry.covers / 25, 1));
|
||||
span.style.backgroundColor = backgroundColor(
|
||||
Math.log10((entry.covers / maxCovers) * 10),
|
||||
);
|
||||
span.innerText = text[index];
|
||||
span.dataset.covers = entry.covers.toString();
|
||||
elements.push(span);
|
||||
|
Loading…
Reference in New Issue
Block a user