From b6fd6630dcdabc2dee4d5754a4470cd4b2934c2e Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sat, 28 May 2022 18:54:17 +0300 Subject: [PATCH] Add color (un)premultiplication utils --- libs/gfx/include/psemek/gfx/color.hpp | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) 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;