logarithmic colors

This commit is contained in:
Theis Pieter Hollebeek 2024-12-13 10:40:05 +01:00
parent f2163901d8
commit 275be8f29f

View File

@ -2,12 +2,13 @@ import * as data from "./data.ts";
type Color = { r: number; g: number; b: number }; type Color = { r: number; g: number; b: number };
function lerp(ratio: number, start: number, middle: number, end: number) { function lerp2(ratio: number, start: number, end: number) {
if (ratio < 0.5) { return (1 - ratio) * start + ratio * end;
return (1 - ratio) * start + ratio * middle;
} else {
return (1 - ratio) * middle + ratio * end;
} }
function lerp3(ratio: number, start: number, middle: number, end: number) {
return (1 - ratio) * lerp2(ratio, start, middle) +
ratio * lerp2(ratio, middle, end);
} }
function colorLerp( function colorLerp(
@ -17,9 +18,9 @@ function colorLerp(
end: Color, end: Color,
): Color { ): Color {
return { return {
r: lerp(ratio, start.r, middle.r, end.r), r: lerp3(ratio, start.r, middle.r, end.r),
g: lerp(ratio, start.g, middle.g, end.g), g: lerp3(ratio, start.g, middle.g, end.g),
b: lerp(ratio, start.b, middle.b, end.b), b: lerp3(ratio, start.b, middle.b, end.b),
}; };
} }
@ -41,7 +42,7 @@ function loadCodeCoverage(
let line = 1; let line = 1;
let col = 1; let col = 1;
const maxCovers = entries.map((v) => v.covers).reduce((acc, v) => const maxCovers = entries.map((v) => v.covers).reduce((acc, v) =>
acc > v ? acc : v acc > Math.log10(v) ? acc : Math.log10(v)
); );
for (let index = 0; index < text.length; ++index) { for (let index = 0; index < text.length; ++index) {
if (text[index] === "\n") { if (text[index] === "\n") {
@ -73,7 +74,7 @@ function loadCodeCoverage(
const span = document.createElement("span"); const span = document.createElement("span");
span.style.backgroundColor = backgroundColor( span.style.backgroundColor = backgroundColor(
Math.log10((entry.covers / maxCovers) * 10), Math.log10(entry.covers) / maxCovers,
); );
span.innerText = text[index]; span.innerText = text[index];
span.dataset.covers = entry.covers.toString(); span.dataset.covers = entry.covers.toString();