diff --git a/src/modules/graphics/Font.cpp b/src/modules/graphics/Font.cpp index 2ca76fc1c..0268d21d6 100644 --- a/src/modules/graphics/Font.cpp +++ b/src/modules/graphics/Font.cpp @@ -403,11 +403,18 @@ std::vector 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') diff --git a/src/modules/graphics/ParticleSystem.cpp b/src/modules/graphics/ParticleSystem.cpp index 0da05f0d0..899bb1ec1 100644 --- a/src/modules/graphics/ParticleSystem.cpp +++ b/src/modules/graphics/ParticleSystem.cpp @@ -719,6 +719,15 @@ love::Vector ParticleSystem::getOffset() const void ParticleSystem::setColor(const std::vector &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 ParticleSystem::getColor() const diff --git a/src/modules/graphics/SpriteBatch.cpp b/src/modules/graphics/SpriteBatch.cpp index 30cd34a4b..5eeab5f18 100644 --- a/src/modules/graphics/SpriteBatch.cpp +++ b/src/modules/graphics/SpriteBatch.cpp @@ -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 diff --git a/src/modules/graphics/SpriteBatch.h b/src/modules/graphics/SpriteBatch.h index d3a4b62f5..d2e1709e8 100644 --- a/src/modules/graphics/SpriteBatch.h +++ b/src/modules/graphics/SpriteBatch.h @@ -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. diff --git a/src/modules/graphics/wrap_SpriteBatch.cpp b/src/modules/graphics/wrap_SpriteBatch.cpp index dd546a82b..544d2c936 100644 --- a/src/modules/graphics/wrap_SpriteBatch.cpp +++ b/src/modules/graphics/wrap_SpriteBatch.cpp @@ -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; }