You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/_posts/2022-05-31-16-shades-of-gray/index.md
+33-29Lines changed: 33 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,6 @@ title: 16 Shades of Gray
3
3
description: The one where I create my first generative artwork and still refuse to use any color on my site.
4
4
keywords: [generative art, hash art, git hash]
5
5
categories: [essay, node, css, math, art]
6
-
lastUpdated: 2022-06-01
7
6
thumbnail: ./images/hash.png
8
7
---
9
8
@@ -15,41 +14,46 @@ This is just a fancy way of saying that I'm too lazy to pick a good color palett
15
14
16
15
In a recent redesign of my site, I also decided to make my layout wider and bigger, but this left a noticeably big gap to the right of the hero banner on my home page. And thus began my quest to fill that space with something meaningless to stare at. It wouldn't really make sense to show a photo of myself next to my name and intro, so I had to search for more reasonable options.
17
16
18
-
I'm a fan of generative art, especially those that are seeded with random hashes—like Jordan Scales's [hashart](https://hash.jordanscales.com/) project, where he draws various math-based artworks using SHA-256 hashes. I also recently had an idea to [show my site's git hash in my footer](/blog/eleventy-build-info/#3-getting-the-latest-commit-hash) as part of my 11ty build. And so I wondered: Could I somehow turn this random string of symbols into a mediocre work of art? Indeed, I could!
17
+
I enjoy generative art, especially works that are seeded with random hashes—like Jordan Scales's [hashart](https://hash.jordanscales.com/) project, where he draws various math-based artworks using SHA-256 hashes. I also recently had an idea to [show my site's git hash in my footer](/blog/eleventy-build-info/#3-getting-the-latest-commit-hash) as part of my 11ty build. And so I wondered: Could I somehow turn this random string of symbols into a mediocre work of art? Indeed, I could!
19
18
20
-
The code to do this is short—it just reads my latest Git commit hash, interprets it as an array of bytes, and maps every group of three consecutive bytes (minus the last one) to an RGB color:
19
+
I decided to take my git hash and convert it to a four-by-four grid of colored tiles. Naturally, having been confronted about my phobia of color, I had no choice but to redouble my efforts and make this a *grayscale* artwork, in keeping with tradition. To introduce color when I've renounced it for so long would be regressive, a sign of weakness, and cause for rebellion.
21
20
22
-
```js
21
+
The code to do this is short—it just reads my latest Git commit hash at build time and converts it to an array of bytes. Since one byte represents <code>2<sup>8</sup> = 256</code> values, and there are `256` values per channel in the RGB true color model, it makes sense to just interpret each byte as a color value. Grays are achieved by setting red, green, and blue to all be the same value. Note that I'm excluding the last four bytes because there are 20 bytes in a Git hash, and I'm only interested in the first 16 for the sake of symmetry.
result +=`<div style="background-color: rgb(${r}, ${g}, ${b})"></div>`;
28
+
constgray= bytes[i];
29
+
constcolor=`rgb(${gray}, ${gray}, ${gray})`;
30
+
result +=`<div style="background-color: ${color}"></div>`;
29
31
}
30
32
result +=`</div>`;
31
33
```
32
34
33
-
Naturally, having been confronted about my phobia of color, I had no choice but to redouble my efforts and make this a *grayscale* artwork, in keeping with tradition. To introduce color when I've renounced it for so long would be regressive, a sign of weakness, and cause for rebellion.
There is just one problem: Mathematically speaking, the hash should eventually generate an embarrassing permutation of tiles. When you consider that I [make frequent and atomic commits](/blog/atomic-git-commits/), this may not end well. On the other hand, there are 256 grayscale values for each of the 16 tiles, which when you do the math comes out to—*counts fingers*—lots and lots of permutations. So it's unlikely that I'll ever run into edge cases where this algorithm produces something silly.
35
+
Below are some examples of the output images this generates:
<caption>Git hashes interpreted as four-by-four grayscale grids</caption>
42
+
<thead>
43
+
<tr>
44
+
<th scope="col">Hash</th>
45
+
<th scope="col">Output</th>
46
+
</tr>
47
+
</thead>
48
+
<tbody>
49
+
{% for hash in hashes %}
50
+
<tr>
51
+
<td><code>{{ hash }}</code></td>
52
+
<td>{% hashArt hash %}</td>
53
+
</tr>
54
+
{% endfor %}
55
+
</tbody>
56
+
</table>
57
+
</div>
58
+
59
+
Nothing super exciting, but there is one problem: Mathematically speaking, the hash should eventually generate an embarrassing permutation of tiles. When you consider that I [make frequent and atomic commits](/blog/atomic-git-commits/), this may not end well. On the other hand, there are 256 grayscale values for each of the 16 tiles, which when you do the math comes out to—*counts fingers*—lots and lots of permutations. So it's unlikely that I'll ever run into edge cases where this algorithm produces something silly. Plus, there are only so many ways to draw things in the confines of a four-by-four grid.
0 commit comments