20:30 <MateoC> Each pixel is given 3 color components, known as red (R), green (G), and blue (B)
20:30 <saxjax> [TheLastMillennial] right
20:30 <MateoC> On the CE, these are derived from a palette. An image has a palette, and the screen has a different palette.
20:30 <MateoC> What we want to do is make the colors in the image palette match as close as possible to the screen palette
20:31 <MateoC> This is so the colors look good when different images are using different palettes
20:31 <MateoC> In your case, the xlib palette is the screen palette
20:32 <MateoC> Now, for every pixel in the image, we grab the palette entry associated with it
20:32 <MateoC> Images are stored in bytes, so if a byte is 2, it means it is using entry 2 of the palette (starting from 0)
20:32 <MateoC> Palette entries are 16 bits, formatted as 1555
20:33 <MateoC> This means that 1 bit green, 5 bits red, 5 bits green, 5 bits blue
20:33 <Runer112> (that's not what it's supposed to mean, but that's what it does mean for the CE)
20:33 <MateoC> The 1 bit green actually is a part of the 5 bits green, resulting in a 565 r,g,b color
20:34 <MateoC> So what we do is we get each component of red, blue, and green into variables. We'll call them R1, G1, B1
20:34 <saxjax> [Cemetech] commandblockguy entered the room
20:35 <MateoC> Now, we have to search the screen (xlib) palette for a color that matches best these R1, G1, B1 values
20:35 <MateoC> Starting at the first entry in the palette, we get the color components for the xlib color. We'll call this R2, G2, B2
20:36 <MateoC> Now, we use a fancy thing called the Euclidean distance between these two colors. It's a straightforward formula that looks like this:
20:37 <MateoC> sqrt((R1 - R2)^2 + (G1 - G2)^2 + (B1 - B2)^2)
20:38 <MateoC> Then we loop through the screen palette for every R2, G2, and B2 color, and whichever one results in the smallest Euclidean distance is the palette entry we use, and draw to the screen
20:39 <MateoC> So if the original image's pixel is 2, we rewrite it to the screen as a different value which matches the palette entry
20:39 <MateoC> Okay I'm done
20:40 <MateoC> Or you could just use the lazy way I suggested and just store a precoverted image thumbnail
20:40 <MateoC> But I think the other way would be pretty neato