Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7802791

Browse files
author
Jonah Williams
authored
[Impeller] Implement invert colors flag. (#39729)
* [Impeller] implement invert colors flag * [Impeller] add invert colors support
1 parent cf85927 commit 7802791

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ TEST_P(AiksTest, CanRenderImage) {
8282
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
8383
}
8484

85+
TEST_P(AiksTest, CanRenderInvertedImage) {
86+
Canvas canvas;
87+
Paint paint;
88+
auto image = std::make_shared<Image>(CreateTextureForFixture("kalimba.jpg"));
89+
paint.color = Color::Red();
90+
paint.invert_colors = true;
91+
canvas.DrawImage(image, Point::MakeXY(100.0, 100.0), paint);
92+
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
93+
}
94+
8595
bool GenerateMipmap(const std::shared_ptr<Context>& context,
8696
std::shared_ptr<Texture> texture,
8797
std::string label) {

impeller/aiks/paint.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ std::shared_ptr<Contents> Paint::WithFilters(
6161
const Matrix& effect_transform) const {
6262
bool is_solid_color_val = is_solid_color.value_or(!color_source);
6363
input = WithColorFilter(input);
64+
input = WithInvertFilter(input);
6465
input = WithMaskBlur(input, is_solid_color_val, effect_transform);
6566
input = WithImageFilter(input, effect_transform);
6667
return input;
@@ -114,6 +115,28 @@ std::shared_ptr<Contents> Paint::WithColorFilter(
114115
return input;
115116
}
116117

118+
/// A color matrix which inverts colors.
119+
// clang-format off
120+
constexpr ColorFilterContents::ColorMatrix kColorInversion = {
121+
.array = {
122+
-1.0, 0, 0, 1.0, 0, //
123+
0, -1.0, 0, 1.0, 0, //
124+
0, 0, -1.0, 1.0, 0, //
125+
1.0, 1.0, 1.0, 1.0, 0 //
126+
}
127+
};
128+
// clang-format on
129+
130+
std::shared_ptr<Contents> Paint::WithInvertFilter(
131+
std::shared_ptr<Contents> input) const {
132+
if (!invert_colors) {
133+
return input;
134+
}
135+
136+
return ColorFilterContents::MakeColorMatrix(
137+
{FilterInput::Make(std::move(input))}, kColorInversion);
138+
}
139+
117140
std::shared_ptr<FilterContents> Paint::MaskBlurDescriptor::CreateMaskBlur(
118141
const FilterInput::Ref& input,
119142
bool is_solid_color,

impeller/aiks/paint.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct Paint {
6767
Scalar stroke_miter = 4.0;
6868
Style style = Style::kFill;
6969
BlendMode blend_mode = BlendMode::kSourceOver;
70+
bool invert_colors = false;
7071

7172
std::optional<ImageFilterProc> image_filter;
7273
std::optional<ColorFilterProc> color_filter;
@@ -123,6 +124,9 @@ struct Paint {
123124

124125
std::shared_ptr<Contents> WithColorFilter(std::shared_ptr<Contents> input,
125126
bool absorb_opacity = false) const;
127+
128+
std::shared_ptr<Contents> WithInvertFilter(
129+
std::shared_ptr<Contents> input) const;
126130
};
127131

128132
} // namespace impeller

impeller/display_list/display_list_dispatcher.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ void DisplayListDispatcher::setColorFilter(
580580

581581
// |flutter::Dispatcher|
582582
void DisplayListDispatcher::setInvertColors(bool invert) {
583-
UNIMPLEMENTED;
583+
paint_.invert_colors = invert;
584584
}
585585

586586
// |flutter::Dispatcher|

0 commit comments

Comments
 (0)