summaryrefslogtreecommitdiff
path: root/ascii-town-heatmap.rs
blob: 83795eaeb15ccd93e12b576e6349a780039b40c5 (plain) (blame)
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
58
59
60
61
62
// cargo-deps: hsl, image, csv = "1.0.0-beta.4", serde, serde_derive
extern crate hsl;
extern crate image;
extern crate csv;
extern crate serde;
#[macro_use]
extern crate serde_derive;

use std::io;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Tile {
    tile_x: usize,
    tile_y: usize,
    create_time: u64,
    modify_count: u64,
    modify_time: u64,
    access_count: u64,
    access_time: u64
}

const W: u32 = 512;

fn main() {
    let mut rdr = csv::Reader::from_reader(io::stdin());
    let mut ma = 0.0f32;
    let mut mm = 0.0f32;
    let mut tiles = [[(0.0f32, 0.0f32); W as usize]; W as usize];
    for tile in rdr.deserialize().map(Result::<Tile, _>::unwrap) {
        let a = tile.access_count as f32;
        let m = tile.modify_count as f32;
        if (tile.tile_x, tile.tile_y) != (0, 0) {
            ma = ma.max(a);
            mm = mm.max(m);
        }
        tiles[tile.tile_y][tile.tile_x] = (a, m);
    }

    let mut heatmap = image::ImageBuffer::new(W * 4, W);
    let green = hsl::HSL::from_rgb(&[0, 255, 0]).h;
    let red = hsl::HSL::from_rgb(&[255, 0, 0]).h;
    for y in 0..W {
        for x in 0..W {
            let (a, m) = tiles[y as usize][x as usize];
            let a = (a / ma).min(1.0).powf(0.2) as f64;
            let m = (m / mm).min(1.0).powf(0.2) as f64;

            let l = a.max(m) * 0.5;
            let h = green * (1.0 - m) + red * m;
            let (r, g, b) = hsl::HSL { h, s: 1.0, l }.to_rgb();
            let rgb = image::Rgb([r, g, b]);

            let y = (y + W / 2) % W;
            for dx in 0..4 {
                let x = (x * 4 + dx + (W - 1) * 2) % (W * 4);
                heatmap.put_pixel(x, y, rgb);
            }
        }
    }
    heatmap.save("heatmap.png").unwrap();
}