This commit is contained in:
Miloslav Ciz 2024-07-30 22:52:22 +02:00
parent 9005259ff3
commit df80221a15
29 changed files with 1864 additions and 1808 deletions

View file

@ -64,6 +64,8 @@ void rgbFrom332(unsigned char colorIndex, unsigned char *red, unsigned char *gre
}
```
NOTE on `rgbFrom332`: a quick naive idea on getting the 8bit values for R, G and B components out of RGB332 color is to simply take the bits and pad them with zeros from bottom to the 8bit values -- though that will somewhat work, it won't be completely correct; consider e.g. an input value `11100000` where R is `111`, i.e. at maximum -- by padding this to `11100000` we however don't get the desired maximum value `11111111` (if we pad with 1s we'll have the same problem for the zero value). This is why the code isn't as simple as the `rgbTo332` function where we really do just chop off the unneeded bits.
Addition/subtraction of two RGB332 colors can be performed by simply adding/subtracting the two color values as long as no over/underflow occurs in either component -- by adding the values we basically perform a parallel addition/subtraction of all three components with only one operation. Unfortunately checking for when exactly such overflow occurs is not easy to do quickly { Or is it? ~drummyfish }, but to rule out e.g. an overflow with addition we may for example check whether the highest bit of each component in both colors to be added is 0 (i.e. `if (((color1 & 0x92) | (color2 & 0x92)) == 0) newColor = color1 + color2;`). { Code untested. ~drummyfish }
Addition/subtraction of colors can also be [approximated](approximation.md) in a very fast way using the [OR](or.md)/[AND](and.md) operation instead of arithmetic addition/subtraction -- however this only works sometimes (check visually). For example if you need to quickly brighten/darken all pixels in a 332 image, you can just OR/AND each pixel with these values:
@ -79,6 +81,8 @@ darken by 3: & 0x92 (100 100 10)
darken by more: doesn't really work anymore
```
Division by power of two is also fairly fast, you may simply shift the whole value right and zero appropriate bits, for example to darken a color twice you can just do `(color >> 1) & 0x6d` { Again code untested sorry. ~drummyfish }. Multiplying by power of two is not as simple though (consider e.g. a value `1011` shifted left once will result in a smaller value `0110`; you may still try some tricks like a bitwise or with the previous value but that will already be an approximation);
{ TODO: Would it be possible to accurately add two 332 colors by adding all components in parallel using bitwise operators somehow? I briefly tried but the result seemed too complex to be worth it though. ~drummyfish }
Inverting a 332 color is done simply by inverting all bits in the color value.