Sha256: 645b6f58abde37bb5543112de565fb0174a83d11618e3e8da7b008f77235e2b1

Contents?: true

Size: 1.63 KB

Versions: 10

Compression:

Stored size: 1.63 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, Colors::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()));
            }
}

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
gosu-0.7.15 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.14 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.10.1 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.10.3 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.10.2 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.11 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.12 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.13.2 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.13.3 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.13 GosuImpl/Graphics/BitmapColorKey.cpp