This commit is contained in:
Miloslav Ciz 2025-05-26 19:29:00 +02:00
parent 23f4bd88fc
commit 8b619fe2cc
17 changed files with 2185 additions and 2130 deletions

View file

@ -68,6 +68,23 @@ void rgbFrom332(unsigned char colorIndex, unsigned char *red, unsigned char *gre
}
```
In practice though it may be better to do something as follows, even if the conversions may be slightly inaccurate:
```
uint32_t rgbFrom332(uint8_t c332)
{
uint32_t c = ((c332 & 0x03) << 6);
c = ((c332 & 0xe0) << 16) | ((c332 & 0x1c) << 11) | c | (c >> 2);
return c | (c >> 3) | (c >> 5);
}
uint8_t rgbTo332(uint32_t c)
{
return ((c >> 16) & 0xe0) | ((c >> 11) & 0x1c) | ((c >> 6) & 0x03);
}
```
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 }