Clamp color arguments to [0, 1] in cases where we don’t support values outside of that range internally.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2017-03-09 21:15:56 -04:00
parent 1a9262dda9
commit a8e4db8e1e
5 changed files with 47 additions and 24 deletions
+11 -4
View File
@@ -403,11 +403,18 @@ std::vector<Font::DrawCommand> Font::generateVertices(const ColoredCodepoints &c
if (curcolori + 1 < ncolors && codepoints.colors[curcolori + 1].index == i)
{
Colorf color = gammaCorrectColor(codepoints.colors[++curcolori].color);
color *= linearconstantcolor;
unGammaCorrectColor(color);
Colorf c = codepoints.colors[++curcolori].color;
curcolor = toColor(color);
c.r = std::min(std::max(c.r, 0.0f), 1.0f);
c.g = std::min(std::max(c.g, 0.0f), 1.0f);
c.b = std::min(std::max(c.b, 0.0f), 1.0f);
c.a = std::min(std::max(c.a, 0.0f), 1.0f);
gammaCorrectColor(c);
c *= linearconstantcolor;
unGammaCorrectColor(c);
curcolor = toColor(c);
}
if (g == '\n')
+9
View File
@@ -719,6 +719,15 @@ love::Vector ParticleSystem::getOffset() const
void ParticleSystem::setColor(const std::vector<Colorf> &newColors)
{
colors = newColors;
// We don't support colors outside of [0,1] when drawing the ParticleSystem.
for (auto &c : colors)
{
c.r = std::min(std::max(c.r, 0.0f), 1.0f);
c.g = std::min(std::max(c.g, 0.0f), 1.0f);
c.b = std::min(std::max(c.b, 0.0f), 1.0f);
c.a = std::min(std::max(c.a, 0.0f), 1.0f);
}
}
std::vector<Colorf> ParticleSystem::getColor() const
+11 -4
View File
@@ -185,10 +185,17 @@ Texture *SpriteBatch::getTexture() const
return texture.get();
}
void SpriteBatch::setColor(const Color &color)
void SpriteBatch::setColor(const Colorf &c)
{
color_active = true;
this->color = color;
Colorf cclamped;
cclamped.r = std::min(std::max(c.r, 0.0f), 1.0f);
cclamped.g = std::min(std::max(c.g, 0.0f), 1.0f);
cclamped.b = std::min(std::max(c.b, 0.0f), 1.0f);
cclamped.a = std::min(std::max(c.a, 0.0f), 1.0f);
this->color = toColor(cclamped);
}
void SpriteBatch::setColor()
@@ -197,10 +204,10 @@ void SpriteBatch::setColor()
color = Color(255, 255, 255, 255);
}
const Color &SpriteBatch::getColor(bool &active) const
Colorf SpriteBatch::getColor(bool &active) const
{
active = color_active;
return color;
return toColorf(color);
}
int SpriteBatch::getCount() const
+2 -2
View File
@@ -72,7 +72,7 @@ public:
*
* @param color The color to use for the following sprites.
*/
void setColor(const Color &color);
void setColor(const Colorf &color);
/**
* Disable per-sprite colors for this SpriteBatch. The next call to
@@ -83,7 +83,7 @@ public:
/**
* Get the current color for this SpriteBatch.
**/
const Color &getColor(bool &active) const;
Colorf getColor(bool &active) const;
/**
* Get the number of sprites currently in this SpriteBatch.
+14 -14
View File
@@ -169,7 +169,7 @@ int w_SpriteBatch_getTexture(lua_State *L)
int w_SpriteBatch_setColor(lua_State *L)
{
SpriteBatch *t = luax_checkspritebatch(L, 1);
Color c;
Colorf c;
if (lua_gettop(L) <= 1)
{
@@ -181,19 +181,19 @@ int w_SpriteBatch_setColor(lua_State *L)
for (int i = 1; i <= 4; i++)
lua_rawgeti(L, 2, i);
c.r = (unsigned char) (luaL_checknumber(L, -4) * 255.0);
c.g = (unsigned char) (luaL_checknumber(L, -3) * 255.0);
c.b = (unsigned char) (luaL_checknumber(L, -2) * 255.0);
c.a = (unsigned char) (luaL_optnumber(L, -1, 1.0) * 255.0);
c.r = (float) luaL_checknumber(L, -4);
c.g = (float) luaL_checknumber(L, -3);
c.b = (float) luaL_checknumber(L, -2);
c.a = (float) luaL_optnumber(L, -1, 1.0);
lua_pop(L, 4);
}
else
{
c.r = (unsigned char) (luaL_checknumber(L, 2) * 255.0);
c.g = (unsigned char) (luaL_checknumber(L, 3) * 255.0);
c.b = (unsigned char) (luaL_checknumber(L, 4) * 255.0);
c.a = (unsigned char) (luaL_optnumber(L, 5, 1.0) * 255.0);
c.r = (float) luaL_checknumber(L, 2);
c.g = (float) luaL_checknumber(L, 3);
c.b = (float) luaL_checknumber(L, 4);
c.a = (float) luaL_optnumber(L, 5, 1.0);
}
t->setColor(c);
@@ -205,16 +205,16 @@ int w_SpriteBatch_getColor(lua_State *L)
{
SpriteBatch *t = luax_checkspritebatch(L, 1);
bool active = false;
const Color &color = t->getColor(active);
Colorf color = t->getColor(active);
// getColor returns null if no color is set.
if (!active)
return 0;
lua_pushnumber(L, (lua_Number) color.r / 255.0);
lua_pushnumber(L, (lua_Number) color.g / 255.0);
lua_pushnumber(L, (lua_Number) color.b / 255.0);
lua_pushnumber(L, (lua_Number) color.a / 255.0);
lua_pushnumber(L, color.r);
lua_pushnumber(L, color.g);
lua_pushnumber(L, color.b);
lua_pushnumber(L, color.a);
return 4;
}