mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
LibGfx: De-duplicate color interpolation code
This commit is contained in:
parent
e5a39d134b
commit
3cc074c1b0
1 changed files with 8 additions and 13 deletions
|
@ -239,14 +239,8 @@ public:
|
||||||
|
|
||||||
Color mixed_with(Color other, float weight) const
|
Color mixed_with(Color other, float weight) const
|
||||||
{
|
{
|
||||||
if (alpha() == other.alpha() || with_alpha(0) == other.with_alpha(0)) {
|
if (alpha() == other.alpha() || with_alpha(0) == other.with_alpha(0))
|
||||||
return Gfx::Color {
|
return interpolate(other, weight);
|
||||||
round_to<u8>(mix<float>(red(), other.red(), weight)),
|
|
||||||
round_to<u8>(mix<float>(green(), other.green(), weight)),
|
|
||||||
round_to<u8>(mix<float>(blue(), other.blue(), weight)),
|
|
||||||
round_to<u8>(mix<float>(alpha(), other.alpha(), weight)),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
// Fallback to slower, but more visually pleasing premultiplied alpha mix.
|
// Fallback to slower, but more visually pleasing premultiplied alpha mix.
|
||||||
// This is needed for linear-gradient()s in LibWeb.
|
// This is needed for linear-gradient()s in LibWeb.
|
||||||
auto mixed_alpha = mix<float>(alpha(), other.alpha(), weight);
|
auto mixed_alpha = mix<float>(alpha(), other.alpha(), weight);
|
||||||
|
@ -263,11 +257,12 @@ public:
|
||||||
|
|
||||||
Color interpolate(Color other, float weight) const noexcept
|
Color interpolate(Color other, float weight) const noexcept
|
||||||
{
|
{
|
||||||
u8 r = red() + round_to<u8>(static_cast<float>(other.red() - red()) * weight);
|
return Gfx::Color {
|
||||||
u8 g = green() + round_to<u8>(static_cast<float>(other.green() - green()) * weight);
|
round_to<u8>(mix<float>(red(), other.red(), weight)),
|
||||||
u8 b = blue() + round_to<u8>(static_cast<float>(other.blue() - blue()) * weight);
|
round_to<u8>(mix<float>(green(), other.green(), weight)),
|
||||||
u8 a = alpha() + round_to<u8>(static_cast<float>(other.alpha() - alpha()) * weight);
|
round_to<u8>(mix<float>(blue(), other.blue(), weight)),
|
||||||
return Color(r, g, b, a);
|
round_to<u8>(mix<float>(alpha(), other.alpha(), weight)),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr Color multiply(Color other) const
|
constexpr Color multiply(Color other) const
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue