Sha256: 4101cba09a80545785c0f6e091c881580930ad21ab92df5bbf1818661000bd48

Contents?: true

Size: 1.63 KB

Versions: 16

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, 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()));
            }
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
gosu-0.7.29 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.28 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.27.1 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.27 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.26.1 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.26 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.25 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.24 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.23 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.22 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.21 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.20 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.19 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.18 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.17 GosuImpl/Graphics/BitmapColorKey.cpp
gosu-0.7.16 GosuImpl/Graphics/BitmapColorKey.cpp