Fix Color IO in binary + handle alpha

This commit is contained in:
Simon Giraudot 2019-01-24 15:21:09 +01:00 committed by Andreas Fabri
parent bd2a66ffcd
commit 9a2953d825
1 changed files with 16 additions and 10 deletions

View File

@ -394,38 +394,44 @@ std::ostream& operator<<( std::ostream& out, const Color& col)
case IO::ASCII : case IO::ASCII :
return out << static_cast<int>(col.red()) << ' ' return out << static_cast<int>(col.red()) << ' '
<< static_cast<int>(col.green()) << ' ' << static_cast<int>(col.green()) << ' '
<< static_cast<int>(col.blue()); << static_cast<int>(col.blue()) << ' '
<< static_cast<int>(col.alpha());
case IO::BINARY : case IO::BINARY :
write(out, static_cast<int>(col.red())); out.write(reinterpret_cast<const char*>(col.to_rgba().data()), 4);
write(out, static_cast<int>(col.green()));
write(out, static_cast<int>(col.blue()));
return out; return out;
default: default:
return out << "Color(" << static_cast<int>(col.red()) << ", " return out << "Color(" << static_cast<int>(col.red()) << ", "
<< static_cast<int>(col.green()) << ", " << static_cast<int>(col.green()) << ", "
<< static_cast<int>(col.blue()) << ')'; << static_cast<int>(col.blue()) << ", "
<< static_cast<int>(col.alpha()) << ")";
} }
} }
inline inline
std::istream &operator>>(std::istream &is, Color& col) std::istream &operator>>(std::istream &is, Color& col)
{ {
int r = 0, g = 0, b = 0; unsigned char r = 0, g = 0, b = 0, a = 0;
int ir = 0, ig = 0, ib = 0, ia = 0;
switch(get_mode(is)) { switch(get_mode(is)) {
case IO::ASCII : case IO::ASCII :
is >> r >> g >> b; is >> ir >> ig >> ib >> ia;
r = (unsigned char)ir;
g = (unsigned char)ig;
b = (unsigned char)ib;
a = (unsigned char)ia;
break; break;
case IO::BINARY : case IO::BINARY :
read(is, r); read(is, r);
read(is, g); read(is, g);
read(is, b); read(is, b);
read(is, a);
break; break;
default: default:
std::cerr << "" << std::endl; std::cerr << "" << std::endl;
std::cerr << "Stream must be in ascii or binary mode" << std::endl; std::cerr << "Stream must be in ascii or binary mode" << std::endl;
break; break;
} }
col = Color((unsigned char)r,(unsigned char)g,(unsigned char)b); col = Color(r,g,b,a);
return is; return is;
} }