Add color (un)premultiplication utils
This commit is contained in:
parent
b9a5861e85
commit
b6fd6630dc
1 changed files with 59 additions and 0 deletions
|
|
@ -104,6 +104,65 @@ namespace psemek::gfx
|
||||||
return to_srgb(c, 1.f / g);
|
return to_srgb(c, 1.f / g);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline geom::vector<float, 4> premult(geom::vector<float, 4> const & c)
|
||||||
|
{
|
||||||
|
return {c[0] * c[3], c[1] * c[3], c[2] * c[3], c[3]};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline geom::vector<std::uint8_t, 4> premult(geom::vector<std::uint8_t, 4> const & c)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
(static_cast<std::uint16_t>(c[0]) * c[3]) / 255,
|
||||||
|
(static_cast<std::uint16_t>(c[1]) * c[3]) / 255,
|
||||||
|
(static_cast<std::uint16_t>(c[2]) * c[3]) / 255,
|
||||||
|
c[3]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline geom::vector<std::uint16_t, 4> premult(geom::vector<std::uint16_t, 4> const & c)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
(static_cast<std::uint32_t>(c[0]) * c[3]) / 65535,
|
||||||
|
(static_cast<std::uint32_t>(c[1]) * c[3]) / 65535,
|
||||||
|
(static_cast<std::uint32_t>(c[2]) * c[3]) / 65535,
|
||||||
|
c[3]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline geom::vector<float, 4> unpremult(geom::vector<float, 4> const & c)
|
||||||
|
{
|
||||||
|
if (c[3] == 0.f)
|
||||||
|
return geom::vector<float, 4>::zero();
|
||||||
|
|
||||||
|
return {c[0] / c[3], c[1] / c[3], c[2] / c[3], c[3]};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline geom::vector<std::uint8_t, 4> unpremult(geom::vector<std::uint8_t, 4> const & c)
|
||||||
|
{
|
||||||
|
if (c[3] == 0)
|
||||||
|
return geom::vector<std::uint8_t, 4>::zero();
|
||||||
|
|
||||||
|
return {
|
||||||
|
(static_cast<std::uint16_t>(c[0]) * 255) / c[3],
|
||||||
|
(static_cast<std::uint16_t>(c[1]) * 255) / c[3],
|
||||||
|
(static_cast<std::uint16_t>(c[2]) * 255) / c[3],
|
||||||
|
c[3]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline geom::vector<std::uint16_t, 4> unpremult(geom::vector<std::uint16_t, 4> const & c)
|
||||||
|
{
|
||||||
|
if (c[3] == 0)
|
||||||
|
return geom::vector<std::uint16_t, 4>::zero();
|
||||||
|
|
||||||
|
return {
|
||||||
|
(static_cast<std::uint32_t>(c[0]) * 65535) / c[3],
|
||||||
|
(static_cast<std::uint32_t>(c[1]) * 65535) / c[3],
|
||||||
|
(static_cast<std::uint32_t>(c[2]) * 65535) / c[3],
|
||||||
|
c[3]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
struct generic_color
|
struct generic_color
|
||||||
{
|
{
|
||||||
color_4f c;
|
color_4f c;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue