Add an [invert:<mode> texture modifier
authorThomas--S <Thomas--S@users.noreply.github.com>
Fri, 12 Aug 2016 15:56:31 +0000 (17:56 +0200)
committerparamat <mat.gregory@virginmedia.com>
Thu, 15 Sep 2016 04:25:38 +0000 (05:25 +0100)
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.

doc/lua_api.txt
src/client/tile.cpp

index aa4f129f018038915cce8c62ae256f657791c6ed..41e7c00ebbf037cfefef1c7ab37f8c2c7e82b242 100644 (file)
@@ -318,6 +318,15 @@ Example:
 
     default_sandstone.png^[opacity:127
 
+#### `[invert:<mode>`
+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.
+
+Example:
+
+       default_apple.png^[invert:rgb
+
 #### `[brighten`
 Brightens the texture.
 
index 67d5d8d1aa97bdfaa017a8902322624290ecfcfe..8f0c39465e3894f416b2aa04b6fe280b0742919d 100644 (file)
@@ -1783,9 +1783,48 @@ bool TextureSource::generateImagePart(std::string part_of_name,
                        for (u32 y = 0; y < dim.Height; y++)
                        for (u32 x = 0; x < dim.Width; x++)
                        {
-                               video::SColor c = baseimg->getPixel(x,y);
+                               video::SColor c = baseimg->getPixel(x, y);
                                c.setAlpha(floor((c.getAlpha() * ratio) / 255 + 0.5));
-                               baseimg->setPixel(x,y,c);
+                               baseimg->setPixel(x, y, c);
+                       }
+               }
+               /*
+                       [invert:mode
+                       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.
+               */
+               else if (str_starts_with(part_of_name, "[invert:")) {
+                       if (baseimg == NULL) {
+                               errorstream << "generateImagePart(): baseimg == NULL "
+                                               << "for part_of_name=\"" << part_of_name
+                                               << "\", cancelling." << std::endl;
+                               return false;
+                       }
+
+                       Strfnd sf(part_of_name);
+                       sf.next(":");
+
+                       std::string mode = sf.next("");
+                       u32 mask = 0;
+                       if (mode.find("a") != std::string::npos)
+                               mask |= 0xff000000UL;
+                       if (mode.find("r") != std::string::npos)
+                               mask |= 0x00ff0000UL;
+                       if (mode.find("g") != std::string::npos)
+                               mask |= 0x0000ff00UL;
+                       if (mode.find("b") != std::string::npos)
+                               mask |= 0x000000ffUL;
+
+                       core::dimension2d<u32> dim = baseimg->getDimension();
+
+                       for (u32 y = 0; y < dim.Height; y++)
+                       for (u32 x = 0; x < dim.Width; x++)
+                       {
+                               video::SColor c = baseimg->getPixel(x, y);
+                               c.color ^= mask;        
+                               baseimg->setPixel(x, y, c);
                        }
                }
                else