7 #include "flutter/testing/testing.h"
72 canvas.
DrawPaint({.color = Color::Color::OrangeRed().WithAlpha(0.5),
80 .mask_blur_descriptor =
95 std::vector<Color> colors = {
Color{0.9568, 0.2627, 0.2118, 1.0},
96 Color{0.1294, 0.5882, 0.9529, 1.0}};
97 std::vector<Scalar> stops = {0.0, 1.0};
101 {0, 0}, {100, 100}, std::move(colors), std::move(stops),
135 EXPECT_EQ(GetContext()->GetCapabilities()->GetDefaultColorFormat(),
137 auto texture = CreateTextureForFixture(
"airplane.jpg",
141 canvas.
Scale(GetContentScale());
150 std::make_shared<Image>(texture),
Rect::MakeSize(texture->GetSize()),
158 EXPECT_EQ(GetContext()->GetCapabilities()->GetDefaultColorFormat(),
160 auto texture = CreateTextureForFixture(
"airplane.jpg",
164 canvas.
Scale(GetContentScale());
175 std::make_shared<Image>(texture),
Rect::MakeSize(texture->GetSize()),
181 #define BLEND_MODE_TUPLE(blend_mode) {#blend_mode, BlendMode::k##blend_mode},
189 std::vector<const char*> blend_mode_names;
190 std::vector<BlendMode> blend_mode_values;
192 const std::vector<std::tuple<const char*, BlendMode>> blends = {
194 assert(blends.size() ==
196 for (
const auto& [name, mode] : blends) {
197 blend_mode_names.push_back(name);
198 blend_mode_values.push_back(mode);
202 return {blend_mode_names, blend_mode_values};
210 auto draw_color_wheel = [](
Canvas& canvas) {
213 auto color_wheel_sampler = [](
Radians r) {
217 auto color_cycle = [](
Scalar x) {
218 Scalar cycle = std::fmod(x, 6.0f);
219 return std::max(0.0f, std::min(1.0f, 2 - std::abs(2 - cycle)));
221 return Color(color_cycle(6 * x + 1),
222 color_cycle(6 * x - 1),
223 color_cycle(6 * x - 3),
232 const int max_dist = 900;
233 for (
int i = 0; i <= 900; i++) {
236 Scalar normalized_distance =
static_cast<Scalar>(i) / max_dist;
239 color_wheel_sampler(r).WithAlpha(1.0f - normalized_distance);
243 canvas.DrawCircle(position, 9 + normalized_distance * 3,
paint);
247 std::shared_ptr<Image> color_wheel_image;
248 Matrix color_wheel_transform;
250 auto callback = [&](
AiksContext& renderer) -> std::optional<Picture> {
252 static bool cache_the_wheel =
true;
253 static int current_blend_index = 3;
254 static float dst_alpha = 1;
255 static float src_alpha = 1;
261 ImGuiWindowFlags_AlwaysAutoResize)) {
262 ImGui::Checkbox(
"Cache the wheel", &cache_the_wheel);
263 ImGui::ListBox(
"Blending mode", ¤t_blend_index,
266 ImGui::SliderFloat(
"Source alpha", &src_alpha, 0, 1);
267 ImGui::ColorEdit4(
"Color A",
reinterpret_cast<float*
>(&color0));
268 ImGui::ColorEdit4(
"Color B",
reinterpret_cast<float*
>(&color1));
269 ImGui::ColorEdit4(
"Color C",
reinterpret_cast<float*
>(&color2));
270 ImGui::SliderFloat(
"Destination alpha", &dst_alpha, 0, 1);
274 static Point content_scale;
275 Point new_content_scale = GetContentScale();
277 if (!cache_the_wheel || new_content_scale != content_scale) {
278 content_scale = new_content_scale;
283 canvas.
Scale(content_scale);
288 draw_color_wheel(canvas);
290 auto snapshot = color_wheel_picture.
Snapshot(renderer);
291 if (!snapshot.has_value() || !snapshot->texture) {
294 color_wheel_image = std::make_shared<Image>(snapshot->texture);
295 color_wheel_transform = snapshot->transform;
313 canvas.
Scale(content_scale);
326 paint.color = color0;
328 paint.color = color1;
330 paint.color = color2;
338 ASSERT_TRUE(OpenPlaygroundHere(callback));
370 const std::shared_ptr<Image>& src_image,
371 const std::shared_ptr<Image>& dst_image,
374 ImGuiWindowFlags_AlwaysAutoResize)) {
375 ImGui::SliderFloat(
"Source alpha", &src_alpha, 0, 1);
395 for (
const auto&
color : source_colors) {
402 canvas.SaveLayer({});
404 canvas.DrawPaint({.color = destination_color});
407 canvas.SaveLayer({.blend_mode = blend_mode});
416 canvas.Translate(
Vector2(100, 0));
418 canvas.RestoreToCount(0);
425 canvas.Translate({0, 100});
428 canvas.SaveLayer({});
429 for (
const auto&
color : source_colors) {
432 {.color = destination_color.
Blend(
color, blend_mode),
434 canvas.Translate(
Vector2(100, 0));
446 canvas.Translate({0, 250});
452 for (
int y = 0; y < 400 / 8; y++) {
453 for (
int x = 0; x < 800 / 16; x++) {
454 canvas.DrawRect(
Rect::MakeXYWH(x * 16 + (y % 2) * 8, y * 8, 8, 8),
463 canvas.DrawImage(dst_image, {0, 0},
467 canvas.DrawImage(src_image, {0, 0},
470 .blend_mode = blend_mode,
480 canvas.DrawImage(dst_image, {400, 0},
483 .blend_mode = blend_mode});
485 canvas.DrawImage(src_image, {400, 0},
493 return canvas.EndRecordingAsPicture();
496 #define BLEND_MODE_TEST(blend_mode) \
497 TEST_P(AiksTest, BlendMode##blend_mode) { \
498 auto src_image = std::make_shared<Image>( \
499 CreateTextureForFixture("blend_mode_src.png")); \
500 auto dst_image = std::make_shared<Image>( \
501 CreateTextureForFixture("blend_mode_dst.png")); \
502 auto callback = [&](AiksContext& renderer) -> std::optional<Picture> { \
503 return BlendModeTest(GetContentScale(), BlendMode::k##blend_mode, \
504 src_image, dst_image, 1.0); \
506 OpenPlaygroundHere(callback); \
510 #define BLEND_MODE_SRC_ALPHA_TEST(blend_mode) \
511 TEST_P(AiksTest, BlendModeSrcAlpha##blend_mode) { \
512 auto src_image = std::make_shared<Image>( \
513 CreateTextureForFixture("blend_mode_src.png")); \
514 auto dst_image = std::make_shared<Image>( \
515 CreateTextureForFixture("blend_mode_dst.png")); \
516 auto callback = [&](AiksContext& renderer) -> std::optional<Picture> { \
517 return BlendModeTest(GetContentScale(), BlendMode::k##blend_mode, \
518 src_image, dst_image, 0.5); \
520 OpenPlaygroundHere(callback); \
527 auto callback = [&](
AiksContext& renderer) -> std::optional<Picture> {
529 static Color foreground = Color::Color::OrangeRed().
WithAlpha(0.5);
530 static int current_blend_index = 3;
533 ImGuiWindowFlags_AlwaysAutoResize)) {
534 ImGui::ColorEdit4(
"Background",
reinterpret_cast<float*
>(&background));
535 ImGui::ColorEdit4(
"Foreground",
reinterpret_cast<float*
>(&foreground));
536 ImGui::ListBox(
"Blend mode", ¤t_blend_index,
537 modes.blend_mode_names.data(),
538 modes.blend_mode_names.size());
546 {.color = foreground,
547 .blend_mode =
static_cast<BlendMode>(current_blend_index)});
550 ASSERT_TRUE(OpenPlaygroundHere(callback));
554 auto texture = CreateTextureForFixture(
"airplane.jpg",
559 canvas.
DrawImage(std::make_shared<Image>(texture), {200, 200},
567 auto texture = CreateTextureForFixture(
"airplane.jpg",
572 canvas.
DrawImage(std::make_shared<Image>(texture), {200, 200},
580 auto texture = CreateTextureForFixture(
"airplane.jpg",
588 canvas.
DrawImage(std::make_shared<Image>(texture), {20, 20},