Storing Colors in a Pixel Grid

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Million Ether Homepage course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Million Ether Homepage, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Million Ether Homepage

To create our page, we're going to be storing pixels in an Ethereum smart contract. To store colors, we have to pick a format to encode them in. Every byte we store on the Ethereum blockchain costs either, so it's important that we're careful about what format we use to store our pixels. Thankfully, we don't need to invent our own format. We're going to use 24-bit RGB color. What that means is that we're going to store one byte each for the red, green, and blue component of each pixel. For example, when we use the color picker in Chrome, we can specify colors in RGBA. RGBA stands for Red/Green/Blue/Alpha. Each one of these is called a channel, as in the red channel, or the alpha channel. The alpha specifies the transparency of the color, which we're not going to use , but you could if you wanted to. Notice that as we pick a different color, the values for red, green, and blue vary as a number between 0 and 255. That's because each channel is represented by one byte. I can understand if you haven't been working with bits and bytes recently, so recall that one byte is 8 bits. A bit can hold two values, either 1 or 0. And with 8 bits, we can hold 2 to the eighth values, which is 256 values. In JavaScript, we can convert numbers between bases like this. This converts 255 in base 10 into binary base 2. There's another common way to view the bytes of a color code, and that is hex encoding. If you've done any web development at all, you've probably seen this. This is just another way of encoding the same thing. Each RGB channel is encoded in two base 16 letters, which are then concatenated together in one string. So to convert our RGB value to hex, we could do this. We'll convert each base 10 string to hex. And then we'll concatenate them together. And notice that this is the same hex code we see in our browser for that same color. But there's one caveat here that's specific to our implementation. Conventional hex color codes require that the hex code have exactly six digits. But if our input channel is less than 16, we are only given a single digit. Here we're given a single digit C, and that's incorrect. It should be 0C. We can fix this by a JavaScript trick, where we prepend zeros and then slice them off. We can also convert colors the other direction, that is from hex back to base 10. In this case, we're taking a base 16 number and converting it to base 10. We'll use this sort of conversion throughout the course. We're going to store our pixels in our smart contract storage as three raw bytes. When we draw this pixel in our browser's canvas, we're going to convert from these raw bytes into base 10. When we upload pixels to our page from an image, we're going to convert the decimal colors back into hexadecimal. We're not going to do a lot of bit manipulation in this series, but because we 're passing pixels around, understanding this color encoding helps a lot.