Color values in love's APIs are now in the range of [0, 1] rather than [0, 255].

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2016-01-31 10:49:29 -04:00
parent 50087bd60e
commit ed933847f8
19 changed files with 126 additions and 144 deletions
+1 -21
View File
@@ -687,31 +687,11 @@ love::Vector ParticleSystem::getOffset() const
void ParticleSystem::setColor(const std::vector<Colorf> &newColors)
{
colors = newColors;
for (Colorf &c : colors)
{
// We want to store the colors as [0, 1], rather than [0, 255].
c.r /= 255.0f;
c.g /= 255.0f;
c.b /= 255.0f;
c.a /= 255.0f;
}
}
std::vector<Colorf> ParticleSystem::getColor() const
{
// The particle system stores colors in the range of [0, 1]...
std::vector<Colorf> ncolors(colors);
for (Colorf &c : ncolors)
{
c.r *= 255.0f;
c.g *= 255.0f;
c.b *= 255.0f;
c.a *= 255.0f;
}
return ncolors;
return colors;
}
void ParticleSystem::setQuads(const std::vector<Quad *> &newQuads)
+9 -3
View File
@@ -388,7 +388,7 @@ void Font::getCodepointsFromString(const std::vector<ColoredString> &strs, Color
{
IndexedColor c = codepoints.colors[0];
if (c.index == 0 && c.color == Color(255, 255, 255, 255))
if (c.index == 0 && c.color == Colorf(1.0f, 1.0f, 1.0f, 1.0f))
codepoints.colors.pop_back();
}
}
@@ -425,7 +425,13 @@ std::vector<Font::DrawCommand> Font::generateVertices(const ColoredCodepoints &c
uint32 g = codepoints.cps[i];
if (curcolori + 1 < ncolors && codepoints.colors[curcolori + 1].index == i)
curcolor = codepoints.colors[++curcolori].color;
{
const Colorf &c = codepoints.colors[++curcolori].color;
curcolor.r = (unsigned char) (c.r * 255.0f);
curcolor.g = (unsigned char) (c.g * 255.0f);
curcolor.b = (unsigned char) (c.b * 255.0f);
curcolor.a = (unsigned char) (c.a * 255.0f);
}
if (g == '\n')
{
@@ -763,7 +769,7 @@ void Font::getWrap(const ColoredCodepoints &codepoints, float wraplimit, std::ve
// Keeping the indexed colors "in sync" is a bit tricky, since we split
// things up and we might skip some glyphs but we don't want to skip any
// color which starts at those indices.
Color curcolor(255, 255, 255, 255);
Colorf curcolor(1.0f, 1.0f, 1.0f, 1.0f);
bool addcurcolor = false;
int curcolori = -1;
int endcolori = (int) codepoints.colors.size() - 1;
+2 -2
View File
@@ -64,12 +64,12 @@ public:
struct ColoredString
{
std::string str;
Color color;
Colorf color;
};
struct IndexedColor
{
Color color;
Colorf color;
int index;
};
+5 -8
View File
@@ -440,11 +440,9 @@ void Graphics::reset()
void Graphics::clear(Colorf c)
{
Colorf nc = Colorf(c.r/255.0f, c.g/255.0f, c.b/255.0f, c.a/255.0f);
gammaCorrectColor(c);
gammaCorrectColor(nc);
glClearColor(nc.r, nc.g, nc.b, nc.a);
glClearColor(c.r, c.g, c.b, c.a);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (gl.bugs.clearRequiresDriverTextureStateUpdate && Shader::current)
@@ -479,7 +477,7 @@ void Graphics::clear(const std::vector<OptionalColorf> &colors)
if (!colors[i].enabled)
continue;
GLfloat c[] = {colors[i].r/255.f, colors[i].g/255.f, colors[i].b/255.f, colors[i].a/255.f};
GLfloat c[] = {colors[i].r, colors[i].g, colors[i].b, colors[i].a};
// TODO: Investigate a potential bug on AMD drivers in Windows/Linux
// which apparently causes the clear color to be incorrect when mixed
@@ -943,11 +941,10 @@ bool Graphics::isGammaCorrect() const
void Graphics::setColor(Colorf c)
{
Colorf nc = Colorf(c.r/255.0f, c.g/255.0f, c.b/255.0f, c.a/255.0f);
Colorf nc = c;
gammaCorrectColor(nc);
glVertexAttrib4f(ATTRIB_CONSTANTCOLOR, nc.r, nc.g, nc.b, nc.a);
states.back().color = c;
}
+3 -3
View File
@@ -442,7 +442,7 @@ public:
/**
* Creates a screenshot of the view and saves it to the default folder.
* @param image The love.image module.
* @param copyAlpha If the alpha channel should be copied or set to full opacity (255).
* @param copyAlpha If the alpha channel should be copied or set to full opacity (1.0).
**/
love::image::ImageData *newScreenshot(love::image::Image *image, bool copyAlpha = true);
@@ -481,8 +481,8 @@ private:
struct DisplayState
{
Colorf color = Colorf(255.0, 255.0, 255.0, 255.0);
Colorf backgroundColor = Colorf(0.0, 0.0, 0.0, 255.0);
Colorf color = Colorf(1.0, 1.0, 1.0, 1.0);
Colorf backgroundColor = Colorf(0.0, 0.0, 0.0, 1.0);
BlendMode blendMode = BLEND_ALPHA;
BlendAlpha blendAlphaMode = BLENDALPHA_MULTIPLY;
+15 -15
View File
@@ -58,7 +58,7 @@ int w_clear(lua_State *L)
Colorf color;
if (lua_isnoneornil(L, 1))
color.set(0, 0, 0, 0);
color.set(0.0, 0.0, 0.0, 0.0);
else if (lua_istable(L, 1))
{
std::vector<Graphics::OptionalColorf> colors((size_t) lua_gettop(L));
@@ -78,7 +78,7 @@ int w_clear(lua_State *L)
colors[i].r = (float) luaL_checknumber(L, -4);
colors[i].g = (float) luaL_checknumber(L, -3);
colors[i].b = (float) luaL_checknumber(L, -2);
colors[i].a = (float) luaL_optnumber(L, -1, 255);
colors[i].a = (float) luaL_optnumber(L, -1, 1.0);
lua_pop(L, 4);
}
@@ -91,7 +91,7 @@ int w_clear(lua_State *L)
color.r = (float) luaL_checknumber(L, 1);
color.g = (float) luaL_checknumber(L, 2);
color.b = (float) luaL_checknumber(L, 3);
color.a = (float) luaL_optnumber(L, 4, 255);
color.a = (float) luaL_optnumber(L, 4, 1.0);
}
luax_catchexcept(L, [&]() { instance()->clear(color); });
@@ -706,10 +706,10 @@ static Mesh *newStandardMesh(lua_State *L)
v.s = (float) luaL_optnumber(L, -6, 0.0);
v.t = (float) luaL_optnumber(L, -5, 0.0);
v.r = (unsigned char) luaL_optnumber(L, -4, 255);
v.g = (unsigned char) luaL_optnumber(L, -3, 255);
v.b = (unsigned char) luaL_optnumber(L, -2, 255);
v.a = (unsigned char) luaL_optnumber(L, -1, 255);
v.r = (unsigned char) (luaL_optnumber(L, -4, 1.0) * 255.0);
v.g = (unsigned char) (luaL_optnumber(L, -3, 1.0) * 255.0);
v.b = (unsigned char) (luaL_optnumber(L, -2, 1.0) * 255.0);
v.a = (unsigned char) (luaL_optnumber(L, -1, 1.0) * 255.0);
lua_pop(L, 9);
vertices.push_back(v);
@@ -911,7 +911,7 @@ int w_setColor(lua_State *L)
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, 255);
c.a = (float) luaL_optnumber(L, -1, 1.0);
lua_pop(L, 4);
}
@@ -920,7 +920,7 @@ int w_setColor(lua_State *L)
c.r = (float) luaL_checknumber(L, 1);
c.g = (float) luaL_checknumber(L, 2);
c.b = (float) luaL_checknumber(L, 3);
c.a = (float) luaL_optnumber(L, 4, 255);
c.a = (float) luaL_optnumber(L, 4, 1.0);
}
instance()->setColor(c);
return 0;
@@ -947,7 +947,7 @@ int w_setBackgroundColor(lua_State *L)
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, 255);
c.a = (float) luaL_optnumber(L, -1, 1.0);
lua_pop(L, 4);
}
@@ -956,7 +956,7 @@ int w_setBackgroundColor(lua_State *L)
c.r = (float) luaL_checknumber(L, 1);
c.g = (float) luaL_checknumber(L, 2);
c.b = (float) luaL_checknumber(L, 3);
c.a = (float) luaL_optnumber(L, 4, 255);
c.a = (float) luaL_optnumber(L, 4, 1.0);
}
instance()->setBackgroundColor(c);
return 0;
@@ -1635,10 +1635,10 @@ int w_points(lua_State *L)
coords[i * 2 + 0] = luax_tofloat(L, -6);
coords[i * 2 + 1] = luax_tofloat(L, -5);
colors[i * 4 + 0] = (uint8) luaL_optnumber(L, -4, 255);
colors[i * 4 + 1] = (uint8) luaL_optnumber(L, -3, 255);
colors[i * 4 + 2] = (uint8) luaL_optnumber(L, -2, 255);
colors[i * 4 + 3] = (uint8) luaL_optnumber(L, -1, 255);
colors[i * 4 + 0] = (uint8) (luaL_optnumber(L, -4, 1.0) * 255.0);
colors[i * 4 + 1] = (uint8) (luaL_optnumber(L, -3, 1.0) * 255.0);
colors[i * 4 + 2] = (uint8) (luaL_optnumber(L, -2, 1.0) * 255.0);
colors[i * 4 + 3] = (uint8) (luaL_optnumber(L, -1, 1.0) * 255.0);
lua_pop(L, 7);
}
+2 -2
View File
@@ -45,7 +45,7 @@ static inline size_t writeByteData(lua_State *L, int startidx, int components, c
uint8 *componentdata = (uint8 *) data;
for (int i = 0; i < components; i++)
componentdata[i] = (uint8) luaL_optnumber(L, startidx + i, 255);
componentdata[i] = (uint8) (luaL_optnumber(L, startidx + i, 1.0) * 255.0);
return sizeof(uint8) * components;
}
@@ -78,7 +78,7 @@ static inline size_t readByteData(lua_State *L, int components, const char *data
const uint8 *componentdata = (const uint8 *) data;
for (int i = 0; i < components; i++)
lua_pushnumber(L, (lua_Number) componentdata[i]);
lua_pushnumber(L, (lua_Number) componentdata[i] / 255.0);
return sizeof(uint8) * components;
}
@@ -515,7 +515,7 @@ int w_ParticleSystem_setColors(lua_State *L)
colors[i].r = (float) luaL_checknumber(L, -4);
colors[i].g = (float) luaL_checknumber(L, -3);
colors[i].b = (float) luaL_checknumber(L, -2);
colors[i].a = (float) luaL_optnumber(L, -1, 255);
colors[i].a = (float) luaL_optnumber(L, -1, 1.0);
// pop the color components from the stack
lua_pop(L, 4);
+5 -10
View File
@@ -174,21 +174,16 @@ static int w__Shader_sendFloat(lua_State *L, bool colors)
if (!values)
return luaL_error(L, "Error in arguments.");
if (colors)
if (colors && love::graphics::isGammaCorrect())
{
bool gammacorrect = love::graphics::isGammaCorrect();
// the fourth component (alpha) is always already linear, if it exists.
int ncomponents = std::min((int) dimension, 3);
const auto &m = love::math::Math::instance;
for (int i = 0; i < count; i++)
{
for (int j = 0; j < (int) dimension; j++)
{
// the fourth component (alpha) is always already linear, if it exists.
if (gammacorrect && j < 3)
values[i * dimension + j] = m.gammaToLinear(values[i * dimension + j] / 255.0f);
else
values[i * dimension + j] /= 255.0f;
}
for (int j = 0; j < ncomponents; j++)
values[i * dimension + j] = m.gammaToLinear(values[i * dimension + j]);
}
}
@@ -144,19 +144,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);
c.g = (unsigned char) luaL_checknumber(L, -3);
c.b = (unsigned char) luaL_checknumber(L, -2);
c.a = (unsigned char) luaL_optnumber(L, -1, 255);
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);
lua_pop(L, 4);
}
else
{
c.r = (unsigned char) luaL_checknumber(L, 2);
c.g = (unsigned char) luaL_checknumber(L, 3);
c.b = (unsigned char) luaL_checknumber(L, 4);
c.a = (unsigned char) luaL_optnumber(L, 5, 255);
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);
}
t->setColor(c);
@@ -173,10 +173,10 @@ int w_SpriteBatch_getColor(lua_State *L)
if (!color)
return 0;
lua_pushnumber(L, color->r);
lua_pushnumber(L, color->g);
lua_pushnumber(L, color->b);
lua_pushnumber(L, color->a);
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);
return 4;
}
+5 -5
View File
@@ -35,7 +35,7 @@ Text *luax_checktext(lua_State *L, int idx)
void luax_checkcoloredstring(lua_State *L, int idx, std::vector<Font::ColoredString> &strings)
{
Font::ColoredString coloredstr;
coloredstr.color = Color(255, 255, 255, 255);
coloredstr.color = Colorf(1.0f, 1.0f, 1.0f, 1.0f);
if (lua_istable(L, idx))
{
@@ -50,10 +50,10 @@ void luax_checkcoloredstring(lua_State *L, int idx, std::vector<Font::ColoredStr
for (int j = 1; j <= 4; j++)
lua_rawgeti(L, -j, j);
coloredstr.color.r = (unsigned char) luaL_checknumber(L, -4);
coloredstr.color.g = (unsigned char) luaL_checknumber(L, -3);
coloredstr.color.b = (unsigned char) luaL_checknumber(L, -2);
coloredstr.color.a = (unsigned char) luaL_optnumber(L, -1, 255);
coloredstr.color.r = (float) luaL_checknumber(L, -4);
coloredstr.color.g = (float) luaL_checknumber(L, -3);
coloredstr.color.b = (float) luaL_checknumber(L, -2);
coloredstr.color.a = (float) luaL_optnumber(L, -1, 1.0);
lua_pop(L, 4);
}