2016-10-28 4 views
-5

Ich habe einen Schwimmer RGBA, die von 0 bis 1 ist, ich möchte es konvertieren rgb in intKonvertieren von float RGBA in int RGB

class Color 
{ 
    public: 
     float R, G, B, A; 

     Color(float r = 1.0f, float g = 1.0f, float b = 1.0f, float a = 1.0f); 
} 
+4

die A entfernen? Kappa – Treycos

+0

aber ich möchte das Äquivalent der RGB (1,0,0); – AndreAhmed

+2

Cast sie zu "int"? – Treycos

Antwort

2

[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; 
     } 
} 
+0

Ich benutze es auf Embedded-System, so dass statische Besetzung nicht erlaubt ist – AndreAhmed

+5

@AndreAhmed I sehe keine Beziehung zwischen diesen beiden Aussagen. Wie verhindert ein eingebettetes System statische Würfe? – user2079303

+2

@AndreAhmed _ "Eingebettete System, so statische Besetzung ist nicht erlaubt" _ Huh was ?? –