Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2085,8 +2085,9 @@ static Picture BlendModeTest(BlendMode blend_mode,
const std::shared_ptr<Image>& src_image,
const std::shared_ptr<Image>& dst_image) {
Color destination_color = Color::CornflowerBlue().WithAlpha(0.75);
auto source_colors =
std::vector<Color>({Color::White(), Color::LimeGreen(), Color::Black()});
auto source_colors = std::vector<Color>({Color::White().WithAlpha(0.75),
Color::LimeGreen().WithAlpha(0.75),
Color::Black().WithAlpha(0.75)});

Canvas canvas;

Expand All @@ -2111,7 +2112,7 @@ static Picture BlendModeTest(BlendMode blend_mode,
// pass.
canvas.SaveLayer({.blend_mode = blend_mode});
{ //
canvas.DrawRect({50, 50, 100, 100}, {.color = color.WithAlpha(0.75)});
canvas.DrawRect({50, 50, 100, 100}, {.color = color});
}
canvas.Restore();
}
Expand All @@ -2135,9 +2136,9 @@ static Picture BlendModeTest(BlendMode blend_mode,
// canvas.DrawPaint({.color = destination_color});
for (auto& color : source_colors) {
// Simply write the CPU blended color to the pass.
canvas.DrawRect({50, 50, 100, 100}, {.color = destination_color.Blend(
color.WithAlpha(0.75), blend_mode),
.blend_mode = BlendMode::kSource});
canvas.DrawRect({50, 50, 100, 100},
{.color = destination_color.Blend(color, blend_mode),
.blend_mode = BlendMode::kSourceOver});
canvas.Translate(Vector2(100, 0));
}
canvas.RestoreToCount(0);
Expand Down
64 changes: 31 additions & 33 deletions impeller/geometry/color.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,6 @@ Color ColorHSB::ToRGBA() const {
return Color(0, 0, 0, alpha);
}

Color Color::operator+(const Color& c) const {
return Color(Vector4(*this) + Vector4(c));
}

Color Color::operator-(const Color& c) const {
return Color(Vector4(*this) - Vector4(c));
}

Color Color::operator*(Scalar value) const {
return Color(red * value, green * value, blue * value, alpha * value);
}

Color::Color(const ColorHSB& hsbColor) : Color(hsbColor.ToRGBA()) {}

Color::Color(const Vector4& value)
Expand Down Expand Up @@ -218,47 +206,57 @@ Color Color::Blend(const Color& src, BlendMode blend_mode) const {
return dst;
case BlendMode::kSourceOver:
// r = s + (1-sa)*d
return src + dst * (1 - src.alpha);
return (src.Premultiply() + dst.Premultiply() * (1 - src.alpha))
.Unpremultiply();
case BlendMode::kDestinationOver:
// r = d + (1-da)*s
return dst + src * (1 - dst.alpha);
return (dst.Premultiply() + src.Premultiply() * (1 - dst.alpha))
.Unpremultiply();
case BlendMode::kSourceIn:
// r = s * da
return src * dst.alpha;
return (src.Premultiply() * dst.alpha).Unpremultiply();
case BlendMode::kDestinationIn:
// r = d * sa
return dst * src.alpha;
return (dst.Premultiply() * src.alpha).Unpremultiply();
case BlendMode::kSourceOut:
// r = s * ( 1- da)
return src * (1 - dst.alpha);
return (src.Premultiply() * (1 - dst.alpha)).Unpremultiply();
case BlendMode::kDestinationOut:
// r = d * (1-sa)
return dst * (1 - src.alpha);
return (dst.Premultiply() * (1 - src.alpha)).Unpremultiply();
case BlendMode::kSourceATop:
// r = s*da + d*(1-sa)
return src * dst.alpha + dst * (1 - src.alpha);
return (src.Premultiply() * dst.alpha +
dst.Premultiply() * (1 - src.alpha))
.Unpremultiply();
case BlendMode::kDestinationATop:
// r = d*sa + s*(1-da)
return dst * src.alpha + src * (1 - dst.alpha);
return (dst.Premultiply() * src.alpha +
src.Premultiply() * (1 - dst.alpha))
.Unpremultiply();
case BlendMode::kXor:
// r = s*(1-da) + d*(1-sa)
return src * (1 - dst.alpha) + dst * (1 - src.alpha);
return (src.Premultiply() * (1 - dst.alpha) +
dst.Premultiply() * (1 - src.alpha))
.Unpremultiply();
case BlendMode::kPlus:
// r = min(s + d, 1)
return Min(src + dst, 1);
return (Min(src.Premultiply() + dst.Premultiply(), 1)).Unpremultiply();
case BlendMode::kModulate:
// r = s*d
return src * dst;
return (src.Premultiply() * dst.Premultiply()).Unpremultiply();
case BlendMode::kScreen: {
// r = s + d - s*d
return src + dst - src * dst;
auto s = src.Premultiply();
auto d = dst.Premultiply();
return (s + d - s * d).Unpremultiply();
}
case BlendMode::kOverlay:
return apply_rgb_srcover_alpha([&](auto s, auto d) {
if (d * 2 < dst.alpha) {
if (d * 2 <= dst.alpha) {
return 2 * s * d;
}
return src.alpha * dst.alpha - 2 * (dst.alpha - s) * (src.alpha - d);
return src.alpha * dst.alpha - 2 * (dst.alpha - d) * (src.alpha - s);
});
case BlendMode::kDarken: {
return apply_rgb_srcover_alpha([&](auto s, auto d) {
Expand Down Expand Up @@ -313,13 +311,13 @@ Color Color::Blend(const Color& src, BlendMode blend_mode) const {
std::sqrt(dst_rgb.z)), //
dst_rgb, //
0.25);
Color blended =
FromRGB(ComponentChoose(
dst_rgb - (1.0 - 2.0 * src) * dst * (1.0 - dst_rgb), //
dst_rgb + (2.0 * src_rgb - 1.0) * (d - dst_rgb), //
src_rgb, //
0.5),
dst.alpha);
Color blended = FromRGB(
ComponentChoose(
dst_rgb - (1.0 - 2.0 * src_rgb) * dst_rgb * (1.0 - dst_rgb), //
dst_rgb + (2.0 * src_rgb - 1.0) * (d - dst_rgb), //
src_rgb, //
0.5),
dst.alpha);
return blended + dst * (1 - blended.alpha);
}
case BlendMode::kDifference:
Expand Down
74 changes: 64 additions & 10 deletions impeller/geometry/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <cstdlib>
#include <ostream>
#include <type_traits>

#include "impeller/geometry/scalar.h"
#include "impeller/geometry/type_traits.h"

#define IMPELLER_FOR_EACH_BLEND_MODE(V) \
V(Clear) \
Expand Down Expand Up @@ -162,11 +164,51 @@ struct Color {
0xFFFFFFFF;
}

constexpr bool operator==(const Color& c) const {
constexpr inline bool operator==(const Color& c) const {
return ScalarNearlyEqual(red, c.red) && ScalarNearlyEqual(green, c.green) &&
ScalarNearlyEqual(blue, c.blue) && ScalarNearlyEqual(alpha, c.alpha);
}

constexpr inline Color operator+(const Color& c) const {
return {red + c.red, green + c.green, blue + c.blue, alpha + c.alpha};
}

template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr inline Color operator+(T value) const {
auto v = static_cast<Scalar>(value);
return {red + v, green + v, blue + v, alpha + v};
}

constexpr inline Color operator-(const Color& c) const {
return {red - c.red, green - c.green, blue - c.blue, alpha - c.alpha};
}

template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr inline Color operator-(T value) const {
auto v = static_cast<Scalar>(value);
return {red - v, green - v, blue - v, alpha - v};
}

constexpr inline Color operator*(const Color& c) const {
return {red * c.red, green * c.green, blue * c.blue, alpha * c.alpha};
}

template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr inline Color operator*(T value) const {
auto v = static_cast<Scalar>(value);
return {red * v, green * v, blue * v, alpha * v};
}

constexpr inline Color operator/(const Color& c) const {
return {red * c.red, green * c.green, blue * c.blue, alpha * c.alpha};
}

template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr inline Color operator/(T value) const {
auto v = static_cast<Scalar>(value);
return {red / v, green / v, blue / v, alpha / v};
}

constexpr Color Premultiply() const {
return {red * alpha, green * alpha, blue * alpha, alpha};
}
Expand Down Expand Up @@ -835,20 +877,32 @@ struct Color {
/// premultipled, the conversion output will be incorrect.
Color SRGBToLinear() const;

Color operator*(const Color& c) const {
return Color(red * c.red, green * c.green, blue * c.blue, alpha * c.alpha);
}
constexpr bool IsTransparent() const { return alpha == 0.0f; }

Color operator+(const Color& c) const;
constexpr bool IsOpaque() const { return alpha == 1.0f; }
};

Color operator-(const Color& c) const;
template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr inline Color operator+(T value, const Color& c) {
return c + static_cast<Scalar>(value);
}

Color operator*(Scalar value) const;
template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr inline Color operator-(T value, const Color& c) {
auto v = static_cast<Scalar>(value);
return {v - c.red, v - c.green, v - c.blue, v - c.alpha};
}

constexpr bool IsTransparent() const { return alpha == 0.0f; }
template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr inline Color operator*(T value, const Color& c) {
return c * static_cast<Scalar>(value);
}

constexpr bool IsOpaque() const { return alpha == 1.0f; }
};
template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr inline Color operator/(T value, const Color& c) {
auto v = static_cast<Scalar>(value);
return {v / c.red, v / c.green, v / c.blue, v / c.alpha};
}

std::string ColorToString(const Color& color);

Expand Down
Loading