Skip to content

Commit

Permalink
implement color mixing
Browse files Browse the repository at this point in the history
  • Loading branch information
Laguna1989 committed Oct 10, 2023
1 parent 82682d0 commit ef11022
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion impl/aselib/image_builder.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
#include "image_builder.hpp"

namespace {
aselib::PixelDataRGBA addPixelColor(
aselib::PixelDataRGBA const& pixel_src, aselib::PixelDataRGBA const& pixel_orig)
{

if (pixel_src.a == 255) {
// just paint over
return pixel_src;
} else if (pixel_src.a == 0) {
return pixel_orig;
} else {
// TODO test

float const a0 = static_cast<float>(pixel_src.a) / 255.0f;
float const r0 = static_cast<float>(pixel_src.r) / 255.0f;
float const g0 = static_cast<float>(pixel_src.g) / 255.0f;
float const b0 = static_cast<float>(pixel_src.b) / 255.0f;

float const a1 = static_cast<float>(pixel_orig.a) / 255.0f;
float const r1 = static_cast<float>(pixel_orig.r) / 255.0f;
float const g1 = static_cast<float>(pixel_orig.g) / 255.0f;
float const b1 = static_cast<float>(pixel_orig.b) / 255.0f;

float const a01 = (1.0f - a0) * a1 + a0;
float const r01 = ((1.0f - a0) * a1 * r1 + a0 * r0) / a01;
float const g01 = ((1.0f - a0) * a1 * g1 + a0 * g0) / a01;
float const b01 = ((1.0f - a0) * a1 * b1 + a0 * b0) / a01;

return aselib::PixelDataRGBA {
// clang-format off
static_cast<std::uint8_t>(r01 * 255.0f),
static_cast<std::uint8_t>(g01 * 255.0f),
static_cast<std::uint8_t>(b01 * 255.0f),
static_cast<std::uint8_t>(a01 * 255.0f)
// clang-format on
};
}
}
} // namespace

aselib::Image aselib::makeImageFromAse(aselib::AsepriteData const& ase)
{
Image img {};
Expand Down Expand Up @@ -29,7 +69,10 @@ aselib::Image aselib::makeImageFromAse(aselib::AsepriteData const& ase)

auto const& pixel_src
= cel.m_pixels_rgba[x_in_cel + y_in_cel * cel.m_cell_width];
img.getPixelAt(x_in_frame + frame_offset_x, y_in_frame) = pixel_src;
auto const& pixel_orig
= img.getPixelAt(x_in_frame + frame_offset_x, y_in_frame);
img.getPixelAt(x_in_frame + frame_offset_x, y_in_frame)
= addPixelColor(pixel_src, pixel_orig);
}
}
}
Expand Down

0 comments on commit ef11022

Please sign in to comment.