summary refs log tree commit diff
path: root/ascii-town-heatmap.rs
diff options
context:
space:
mode:
authorEduard-Mihai Burtescu <edy.burt@gmail.com>2017-08-27 11:39:44 +0300
committerGitHub <noreply@github.com>2017-08-27 11:39:44 +0300
commit93cdd0ad3c8efaa1ccc4692544e68d5380d62caa (patch)
tree540a58434d3f2713d296919f82b66c49f87de0a5 /ascii-town-heatmap.rs
downloadheatmap-93cdd0ad3c8efaa1ccc4692544e68d5380d62caa.tar.gz
heatmap-93cdd0ad3c8efaa1ccc4692544e68d5380d62caa.zip
Diffstat (limited to 'ascii-town-heatmap.rs')
-rw-r--r--ascii-town-heatmap.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/ascii-town-heatmap.rs b/ascii-town-heatmap.rs
new file mode 100644
index 0000000..83795ea
--- /dev/null
+++ b/ascii-town-heatmap.rs
@@ -0,0 +1,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();
+}