All files / js/visualization color.ts

90.32% Statements 28/31
71.43% Branches 10/14
100% Functions 6/6
90.32% Lines 28/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57      848x 848x 848x 848x 848x     848x 8x 840x 816x 24x   24x 10x 14x   14x 14x           2544x 2544x       848x     848x       848x 848x   848x 848x   848x       846x 846x       2x 2x  
 
// https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
function hsv_to_rgb(h: number, s: number, v: number) {
    const h_i = Math.floor(h*6);
    const f = h*6 - h_i;
    const p = v * (1 - s);
    const q = v * (1 - f*s);
    const t = v * (1 - (1 - f) * s);
    let r, g, b;
 
    if(h_i===0) {
        [r, g, b] = [v, t, p];
    } else if(h_i===1) {
        [r, g, b] = [q, v, p];
    } else Iif(h_i===2) {
        [r, g, b] = [p, v, t];
    } else if(h_i===3) {
        [r, g, b] = [p, q, v];
    } else Iif(h_i===4) {
        [r, g, b] = [t, p, v];
    } else if(h_i===5) {
        [r, g, b] = [v, p, q];
    } else E{
        throw "unreachable";
    }
 
    function componentToHex(c: number) {
        const hex = c.toString(16);
        return hex.length === 1 ? "0" + hex : hex;
    }
 
    function rgbToHex(r: number, g: number, b: number) {
        return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
    }
 
    return rgbToHex(Math.floor(r*256), Math.floor(g*256), Math.floor(b*256));
}
 
function hue_from_idx(n: number) {
    const GOLDEN_RATIO_CONJUGATE = 0.618033988749895
    const HUE = 0.3;
 
    let h = HUE + GOLDEN_RATIO_CONJUGATE * n;
    h -= Math.floor(h);
 
    return h;
}
 
export function idx2color(n: number ) {
    const h = hue_from_idx(n);
    return hsv_to_rgb(h, 0.5, 0.95);
}
 
export function desaturized_idx2color(n: number) {
    const h = hue_from_idx(n);
    return hsv_to_rgb(h, 0.25, 0.75);
}