[Edited] aufgrund später: „Ich möchte die 32-Bit konvertieren RGBA auf 8bit R2G4B2"
[wieder Edited] wegen ‚ich verwende es auf Embedded-System, so statische Guss wird‘
[bearbeitet sogar mehr] wegen „nicht erlaubt ich bin mit einem c99 Compiler "- so nein C++
struct Color
{
float R, G, B, A;
}
int RGBAstructTo8bitRGB(const Color* color) {
if(NULL==color) { return 0; /* black */
unsigned r=(int)(3*(color->R < 0 ? 0 : this->R));
unsigned g=(int)(7*(color->G < 0 ? 0 : this->G));
unsigned b=(int)(3*(color->B < 0 ? 0 : this->B));
return (r<<6) | (g<<2) | b;
}
int RGBATo8bitRGB(float R, float G, float B) {
unsigned r=(int)(3*(R < 0 ? 0 : this->R));
unsigned g=(int)(7*(G < 0 ? 0 : this->G));
unsigned b=(int)(3*(B < 0 ? 0 : this->B));
return (r<<6) | (g<<2) | b;
}
void 8bitRGBtoRGBAstruct(int color, struct Color* dest) {
if(NULL!=dest) {
dest->R=(float)((color & 0x03)/3.0);
dest->G=(float)(((color >> 2) & 0x07)/7.0);
dest->B=(float)(((color >> 6) & 0x03)/3.0);
}
}
Für einen 24-Bit-RGB:
Ignorieren A
, skalieren die Komponenten [0,255]
verschiebe oder sie in einen int:
class Color
{
public:
float R, G, B, A;
int toRGB() const {
unsigned r=(int)(255*(this->R < 0 ? 0 : this->R));
unsigned g=(int)(255*(this->G < 0 ? 0 : this->G));
unsigned b=(int)(255*(this->B < 0 ? 0 : this->B));
return (r<<16) | (g<<8) | b;
}
}
die A entfernen? Kappa – Treycos
aber ich möchte das Äquivalent der RGB (1,0,0); – AndreAhmed
Cast sie zu "int"? – Treycos