diff --git a/libs/gfx/include/psemek/gfx/color.hpp b/libs/gfx/include/psemek/gfx/color.hpp index 497267bb..8f66532a 100644 --- a/libs/gfx/include/psemek/gfx/color.hpp +++ b/libs/gfx/include/psemek/gfx/color.hpp @@ -104,6 +104,65 @@ namespace psemek::gfx return to_srgb(c, 1.f / g); } + inline geom::vector premult(geom::vector const & c) + { + return {c[0] * c[3], c[1] * c[3], c[2] * c[3], c[3]}; + } + + inline geom::vector premult(geom::vector const & c) + { + return { + (static_cast(c[0]) * c[3]) / 255, + (static_cast(c[1]) * c[3]) / 255, + (static_cast(c[2]) * c[3]) / 255, + c[3] + }; + } + + inline geom::vector premult(geom::vector const & c) + { + return { + (static_cast(c[0]) * c[3]) / 65535, + (static_cast(c[1]) * c[3]) / 65535, + (static_cast(c[2]) * c[3]) / 65535, + c[3] + }; + } + + inline geom::vector unpremult(geom::vector const & c) + { + if (c[3] == 0.f) + return geom::vector::zero(); + + return {c[0] / c[3], c[1] / c[3], c[2] / c[3], c[3]}; + } + + inline geom::vector unpremult(geom::vector const & c) + { + if (c[3] == 0) + return geom::vector::zero(); + + return { + (static_cast(c[0]) * 255) / c[3], + (static_cast(c[1]) * 255) / c[3], + (static_cast(c[2]) * 255) / c[3], + c[3] + }; + } + + inline geom::vector unpremult(geom::vector const & c) + { + if (c[3] == 0) + return geom::vector::zero(); + + return { + (static_cast(c[0]) * 65535) / c[3], + (static_cast(c[1]) * 65535) / c[3], + (static_cast(c[2]) * 65535) / c[3], + c[3] + }; + } + struct generic_color { color_4f c;