Sha256: ad8d96512ad4b620c8fa3af52ca96256351244c25613e096e541d63a0f71d22b

Contents?: true

Size: 1.88 KB

Versions: 43

Compression:

Stored size: 1.88 KB

Contents

#include <Gosu/Bitmap.hpp>
#include <vector>

void Gosu::applyColorKey(Bitmap& bitmap, Color key)
{
    std::vector<Color> surroundingColors;
    surroundingColors.reserve(4);

    for (unsigned y = 0; y < bitmap.height(); ++y)
        for (unsigned x = 0; x < bitmap.width(); ++x)
            if (bitmap.getPixel(x, y) == key)
            {
                surroundingColors.clear();
                if (x > 0 && bitmap.getPixel(x - 1, y) != key)
                    surroundingColors.push_back(bitmap.getPixel(x - 1, y));
                if (x < bitmap.width() - 1 && bitmap.getPixel(x + 1, y) != key)
                    surroundingColors.push_back(bitmap.getPixel(x + 1, y));
                if (y > 0 && bitmap.getPixel(x, y - 1) != key)
                    surroundingColors.push_back(bitmap.getPixel(x, y - 1));
                if (y < bitmap.height() - 1 && bitmap.getPixel(x, y + 1) != key)
                    surroundingColors.push_back(bitmap.getPixel(x, y + 1));

                if (surroundingColors.empty())
                {
                    bitmap.setPixel(x, y, Color::NONE);
                    continue;
                }

                unsigned red = 0, green = 0, blue = 0;
                for (unsigned i = 0; i < surroundingColors.size(); ++i)
                {
                    red += surroundingColors[i].red();
                    green += surroundingColors[i].green();
                    blue += surroundingColors[i].blue();
                }
                bitmap.setPixel(x, y, Color(0, red / surroundingColors.size(),
                    green / surroundingColors.size(), blue / surroundingColors.size()));
            }
}

void Gosu::unapplyColorKey(Bitmap& bitmap, Color color)
{
    Color* p = bitmap.data();
    for (int i = bitmap.width() * bitmap.height(); i > 0; --i, ++p)
        if (p->alpha() == 0)
            *p = color;
        else
            p->setAlpha(255);
}

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
gosu-0.9.2 src/Bitmap/BitmapColorKey.cpp
gosu-0.9.2.pre1 src/Bitmap/BitmapColorKey.cpp
gosu-0.9.1 src/Bitmap/BitmapColorKey.cpp
gosu-0.9.0 src/Bitmap/BitmapColorKey.cpp
gosu-0.9.0.pre1 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.7.2 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.7.1 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.7 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.6 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.6.pre1 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.5 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.5.pre1 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.4 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.3 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.2 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.1 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.0 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.0.pre7 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.0.pre6 src/Bitmap/BitmapColorKey.cpp
gosu-0.8.0.pre5 src/Bitmap/BitmapColorKey.cpp