Skip to content

Commit 1475c1b

Browse files
Thomas--Sparamat
authored andcommittedSep 15, 2016
Add an [invert:<mode> texture modifier
Inverts the given channels of the base image. Mode may contain the characters "r", "g", "b", "a". Only the channels that are mentioned in the mode string will be inverted.
1 parent 6e30dd0 commit 1475c1b

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed
 

Diff for: ‎doc/lua_api.txt

+9
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,15 @@ Example:
318318

319319
default_sandstone.png^[opacity:127
320320

321+
#### `[invert:<mode>`
322+
Inverts the given channels of the base image.
323+
Mode may contain the characters "r", "g", "b", "a".
324+
Only the channels that are mentioned in the mode string will be inverted.
325+
326+
Example:
327+
328+
default_apple.png^[invert:rgb
329+
321330
#### `[brighten`
322331
Brightens the texture.
323332

Diff for: ‎src/client/tile.cpp

+41-2
Original file line numberDiff line numberDiff line change
@@ -1783,9 +1783,48 @@ bool TextureSource::generateImagePart(std::string part_of_name,
17831783
for (u32 y = 0; y < dim.Height; y++)
17841784
for (u32 x = 0; x < dim.Width; x++)
17851785
{
1786-
video::SColor c = baseimg->getPixel(x,y);
1786+
video::SColor c = baseimg->getPixel(x, y);
17871787
c.setAlpha(floor((c.getAlpha() * ratio) / 255 + 0.5));
1788-
baseimg->setPixel(x,y,c);
1788+
baseimg->setPixel(x, y, c);
1789+
}
1790+
}
1791+
/*
1792+
[invert:mode
1793+
Inverts the given channels of the base image.
1794+
Mode may contain the characters "r", "g", "b", "a".
1795+
Only the channels that are mentioned in the mode string
1796+
will be inverted.
1797+
*/
1798+
else if (str_starts_with(part_of_name, "[invert:")) {
1799+
if (baseimg == NULL) {
1800+
errorstream << "generateImagePart(): baseimg == NULL "
1801+
<< "for part_of_name=\"" << part_of_name
1802+
<< "\", cancelling." << std::endl;
1803+
return false;
1804+
}
1805+
1806+
Strfnd sf(part_of_name);
1807+
sf.next(":");
1808+
1809+
std::string mode = sf.next("");
1810+
u32 mask = 0;
1811+
if (mode.find("a") != std::string::npos)
1812+
mask |= 0xff000000UL;
1813+
if (mode.find("r") != std::string::npos)
1814+
mask |= 0x00ff0000UL;
1815+
if (mode.find("g") != std::string::npos)
1816+
mask |= 0x0000ff00UL;
1817+
if (mode.find("b") != std::string::npos)
1818+
mask |= 0x000000ffUL;
1819+
1820+
core::dimension2d<u32> dim = baseimg->getDimension();
1821+
1822+
for (u32 y = 0; y < dim.Height; y++)
1823+
for (u32 x = 0; x < dim.Width; x++)
1824+
{
1825+
video::SColor c = baseimg->getPixel(x, y);
1826+
c.color ^= mask;
1827+
baseimg->setPixel(x, y, c);
17891828
}
17901829
}
17911830
else

0 commit comments

Comments
 (0)