Browse Source

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

--HG--
branch : minor
Alex Szpakowski 8 years ago
parent
commit
a8e4db8e1e

+ 11 - 4
src/modules/graphics/Font.cpp

@@ -403,11 +403,18 @@ std::vector<Font::DrawCommand> Font::generateVertices(const ColoredCodepoints &c
 
 
 		if (curcolori + 1 < ncolors && codepoints.colors[curcolori + 1].index == i)
 		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')
 		if (g == '\n')

+ 9 - 0
src/modules/graphics/ParticleSystem.cpp

@@ -719,6 +719,15 @@ love::Vector ParticleSystem::getOffset() const
 void ParticleSystem::setColor(const std::vector<Colorf> &newColors)
 void ParticleSystem::setColor(const std::vector<Colorf> &newColors)
 {
 {
 	colors = 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
 std::vector<Colorf> ParticleSystem::getColor() const

+ 11 - 4
src/modules/graphics/SpriteBatch.cpp

@@ -185,10 +185,17 @@ Texture *SpriteBatch::getTexture() const
 	return texture.get();
 	return texture.get();
 }
 }
 
 
-void SpriteBatch::setColor(const Color &color)
+void SpriteBatch::setColor(const Colorf &c)
 {
 {
 	color_active = true;
 	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()
 void SpriteBatch::setColor()
@@ -197,10 +204,10 @@ void SpriteBatch::setColor()
 	color = Color(255, 255, 255, 255);
 	color = Color(255, 255, 255, 255);
 }
 }
 
 
-const Color &SpriteBatch::getColor(bool &active) const
+Colorf SpriteBatch::getColor(bool &active) const
 {
 {
 	active = color_active;
 	active = color_active;
-	return color;
+	return toColorf(color);
 }
 }
 
 
 int SpriteBatch::getCount() const
 int SpriteBatch::getCount() const

+ 2 - 2
src/modules/graphics/SpriteBatch.h

@@ -72,7 +72,7 @@ public:
 	 *
 	 *
 	 * @param color The color to use for the following sprites.
 	 * @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
 	 * Disable per-sprite colors for this SpriteBatch. The next call to
@@ -83,7 +83,7 @@ public:
 	/**
 	/**
 	 * Get the current color for this SpriteBatch.
 	 * 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.
 	 * Get the number of sprites currently in this SpriteBatch.

+ 14 - 14
src/modules/graphics/wrap_SpriteBatch.cpp

@@ -169,7 +169,7 @@ int w_SpriteBatch_getTexture(lua_State *L)
 int w_SpriteBatch_setColor(lua_State *L)
 int w_SpriteBatch_setColor(lua_State *L)
 {
 {
 	SpriteBatch *t = luax_checkspritebatch(L, 1);
 	SpriteBatch *t = luax_checkspritebatch(L, 1);
-	Color c;
+	Colorf c;
 
 
 	if (lua_gettop(L) <= 1)
 	if (lua_gettop(L) <= 1)
 	{
 	{
@@ -181,19 +181,19 @@ int w_SpriteBatch_setColor(lua_State *L)
 		for (int i = 1; i <= 4; i++)
 		for (int i = 1; i <= 4; i++)
 			lua_rawgeti(L, 2, 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);
 		lua_pop(L, 4);
 	}
 	}
 	else
 	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);
 	t->setColor(c);
@@ -205,16 +205,16 @@ int w_SpriteBatch_getColor(lua_State *L)
 {
 {
 	SpriteBatch *t = luax_checkspritebatch(L, 1);
 	SpriteBatch *t = luax_checkspritebatch(L, 1);
 	bool active = false;
 	bool active = false;
-	const Color &color = t->getColor(active);
+	Colorf color = t->getColor(active);
 
 
 	// getColor returns null if no color is set.
 	// getColor returns null if no color is set.
 	if (!active)
 	if (!active)
 		return 0;
 		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;
 	return 4;
 }
 }