17 #include "third_party/imgui/imgui.h"
18 #include "txt/platform.h"
35 std::vector<Color> colors = {
Color{0.9568, 0.2627, 0.2118, 1.0},
36 Color{0.1294, 0.5882, 0.9529, 0.0}};
37 std::vector<Scalar> stops = {0.0, 1.0};
40 {0, 0}, {200, 200}, std::move(colors), std::move(stops), tile_mode, {});
63 canvas.
Scale(GetContentScale());
67 std::vector<Color> colors = {
Color{0.9568, 0.2627, 0.2118, 1.0},
68 Color{0.1294, 0.5882, 0.9529, 0.0}};
69 std::vector<Scalar> stops = {0.0, 1.0};
72 {0, 0}, {200, 200}, std::move(colors), std::move(stops),
93 std::vector<Color> colors = {
Color{0.8, 0.8, 0.8, 1.0},
94 Color{0.2, 0.2, 0.2, 1.0}};
95 std::vector<Scalar> stops = {0.0, 1.0};
98 {0, 0}, {800, 500}, std::move(colors), std::move(stops),
100 paint.
dither = use_dithering;
114 bool use_dithering) {
120 std::vector<Color> colors = {
Color{1.0, 1.0, 1.0, 1.0},
121 Color{0.0, 0.0, 0.0, 1.0}};
122 std::vector<Scalar> stops = {0.0, 1.0};
125 {600, 600}, 600, std::move(colors), std::move(stops),
127 paint.
dither = use_dithering;
141 bool use_dithering) {
148 std::vector<Color> colors = {
Color{1.0, 1.0, 1.0, 1.0},
149 Color{0.0, 0.0, 0.0, 1.0}};
150 std::vector<Scalar> stops = {0.0, 1.0};
155 paint.
dither = use_dithering;
170 bool use_dithering) {
177 std::vector<Color> colors = {
Color{1.0, 1.0, 1.0, 1.0},
178 Color{0.0, 0.0, 0.0, 1.0}};
179 std::vector<Scalar> stops = {0.0, 1.0};
182 {100, 100}, 100, std::move(colors), std::move(stops), {0, 1}, 0,
184 paint.
dither = use_dithering;
199 void CanRenderLinearGradientWithOverlappingStops(
AiksTest* aiks_test,
205 std::vector<Color> colors = {
206 Color{0.9568, 0.2627, 0.2118, 1.0}, Color{0.9568, 0.2627, 0.2118, 1.0},
207 Color{0.1294, 0.5882, 0.9529, 1.0}, Color{0.1294, 0.5882, 0.9529, 1.0}};
208 std::vector<Scalar> stops = {0.0, 0.5, 0.5, 1.0};
211 {0, 0}, {500, 500}, std::move(colors), std::move(stops), tile_mode, {});
213 paint.
color = Color(1.0, 1.0, 1.0, 1.0);
225 void CanRenderLinearGradientManyColors(
AiksTest* aiks_test,
228 canvas.
Scale(aiks_test->GetContentScale());
232 std::vector<Color> colors = {
233 Color{0x1f / 255.0, 0.0, 0x5c / 255.0, 1.0},
234 Color{0x5b / 255.0, 0.0, 0x60 / 255.0, 1.0},
235 Color{0x87 / 255.0, 0x01 / 255.0, 0x60 / 255.0, 1.0},
236 Color{0xac / 255.0, 0x25 / 255.0, 0x53 / 255.0, 1.0},
237 Color{0xe1 / 255.0, 0x6b / 255.0, 0x5c / 255.0, 1.0},
238 Color{0xf3 / 255.0, 0x90 / 255.0, 0x60 / 255.0, 1.0},
239 Color{0xff / 255.0, 0xb5 / 255.0, 0x6b / 250.0, 1.0}};
240 std::vector<Scalar> stops = {
251 {0, 0}, {200, 200}, std::move(colors), std::move(stops), tile_mode, {});
253 paint.
color = Color(1.0, 1.0, 1.0, 1.0);
274 void CanRenderLinearGradientWayManyColors(
AiksTest* aiks_test,
279 auto color = Color{0x1f / 255.0, 0.0, 0x5c / 255.0, 1.0};
280 std::vector<Color> colors;
281 std::vector<Scalar> stops;
282 auto current_stop = 0.0;
283 for (
int i = 0; i < 2000; i++) {
284 colors.push_back(color);
285 stops.push_back(current_stop);
286 current_stop += 1 / 2000.0;
288 stops[2000 - 1] = 1.0;
291 {0, 0}, {200, 200}, std::move(colors), std::move(stops), tile_mode, {});
304 auto callback = [&](
AiksContext& renderer) -> std::optional<Picture> {
305 const char* tile_mode_names[] = {
"Clamp",
"Repeat",
"Mirror",
"Decal"};
310 static int selected_tile_mode = 0;
311 ImGui::Begin(
"Controls",
nullptr, ImGuiWindowFlags_AlwaysAutoResize);
312 ImGui::Combo(
"Tile mode", &selected_tile_mode, tile_mode_names,
313 sizeof(tile_mode_names) /
sizeof(
char*));
320 std::string label =
"##1";
321 for (
int i = 0; i < 4; i++) {
322 ImGui::InputScalarN(label.c_str(), ImGuiDataType_Float, &(matrix.
vec[i]),
323 4, NULL, NULL,
"%.2f", 0);
331 auto tile_mode = tile_modes[selected_tile_mode];
333 std::vector<Color> colors = {
334 Color{0x1f / 255.0, 0.0, 0x5c / 255.0, 1.0},
335 Color{0x5b / 255.0, 0.0, 0x60 / 255.0, 1.0},
336 Color{0x87 / 255.0, 0x01 / 255.0, 0x60 / 255.0, 1.0},
337 Color{0xac / 255.0, 0x25 / 255.0, 0x53 / 255.0, 1.0},
338 Color{0xe1 / 255.0, 0x6b / 255.0, 0x5c / 255.0, 1.0},
339 Color{0xf3 / 255.0, 0x90 / 255.0, 0x60 / 255.0, 1.0},
340 Color{0xff / 255.0, 0xb5 / 255.0, 0x6b / 250.0, 1.0}};
341 std::vector<Scalar> stops = {
342 0.0, 2.0 / 62.0, 4.0 / 62.0, 8.0 / 62.0, 16.0 / 62.0, 32.0 / 62.0, 1.0,
346 {0, 0}, {200, 200}, std::move(colors), std::move(stops), tile_mode, {});
351 ASSERT_TRUE(OpenPlaygroundHere(callback));
360 {200, 200}, {400, 400},
364 {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0},
366 .mask_blur_descriptor =
380 auto callback = [&](
AiksContext& renderer) -> std::optional<Picture> {
381 const char* tile_mode_names[] = {
"Clamp",
"Repeat",
"Mirror",
"Decal"};
386 static int selected_tile_mode = 0;
387 ImGui::Begin(
"Controls",
nullptr, ImGuiWindowFlags_AlwaysAutoResize);
388 ImGui::Combo(
"Tile mode", &selected_tile_mode, tile_mode_names,
389 sizeof(tile_mode_names) /
sizeof(
char*));
396 std::string label =
"##1";
397 for (
int i = 0; i < 4; i++) {
398 ImGui::InputScalarN(label.c_str(), ImGuiDataType_Float, &(matrix.
vec[i]),
399 4, NULL, NULL,
"%.2f", 0);
407 auto tile_mode = tile_modes[selected_tile_mode];
409 std::vector<Color> colors = {
Color{0.9568, 0.2627, 0.2118, 1.0},
410 Color{0.1294, 0.5882, 0.9529, 1.0}};
411 std::vector<Scalar> stops = {0.0, 1.0};
414 {100, 100}, 100, std::move(colors), std::move(stops), tile_mode, {});
419 ASSERT_TRUE(OpenPlaygroundHere(callback));
423 auto callback = [&](
AiksContext& renderer) -> std::optional<Picture> {
424 const char* tile_mode_names[] = {
"Clamp",
"Repeat",
"Mirror",
"Decal"};
429 static int selected_tile_mode = 0;
430 ImGui::Begin(
"Controls",
nullptr, ImGuiWindowFlags_AlwaysAutoResize);
431 ImGui::Combo(
"Tile mode", &selected_tile_mode, tile_mode_names,
432 sizeof(tile_mode_names) /
sizeof(
char*));
439 std::string label =
"##1";
440 for (
int i = 0; i < 4; i++) {
441 ImGui::InputScalarN(label.c_str(), ImGuiDataType_Float, &(matrix.
vec[i]),
442 4, NULL, NULL,
"%.2f", 0);
450 auto tile_mode = tile_modes[selected_tile_mode];
452 std::vector<Color> colors = {
453 Color{0x1f / 255.0, 0.0, 0x5c / 255.0, 1.0},
454 Color{0x5b / 255.0, 0.0, 0x60 / 255.0, 1.0},
455 Color{0x87 / 255.0, 0x01 / 255.0, 0x60 / 255.0, 1.0},
456 Color{0xac / 255.0, 0x25 / 255.0, 0x53 / 255.0, 1.0},
457 Color{0xe1 / 255.0, 0x6b / 255.0, 0x5c / 255.0, 1.0},
458 Color{0xf3 / 255.0, 0x90 / 255.0, 0x60 / 255.0, 1.0},
459 Color{0xff / 255.0, 0xb5 / 255.0, 0x6b / 250.0, 1.0}};
460 std::vector<Scalar> stops = {
471 {100, 100}, 100, std::move(colors), std::move(stops), tile_mode, {});
476 ASSERT_TRUE(OpenPlaygroundHere(callback));
482 canvas.
Scale(aiks_test->GetContentScale());
486 std::vector<Color> colors = {Color{0.9568, 0.2627, 0.2118, 1.0},
487 Color{0.1294, 0.5882, 0.9529, 1.0}};
488 std::vector<Scalar> stops = {0.0, 1.0};
491 {100, 100}, Degrees(45), Degrees(135), std::move(colors),
492 std::move(stops), tile_mode, {});
513 void CanRenderSweepGradientManyColors(
AiksTest* aiks_test,
519 std::vector<Color> colors = {
520 Color{0x1f / 255.0, 0.0, 0x5c / 255.0, 1.0},
521 Color{0x5b / 255.0, 0.0, 0x60 / 255.0, 1.0},
522 Color{0x87 / 255.0, 0x01 / 255.0, 0x60 / 255.0, 1.0},
523 Color{0xac / 255.0, 0x25 / 255.0, 0x53 / 255.0, 1.0},
524 Color{0xe1 / 255.0, 0x6b / 255.0, 0x5c / 255.0, 1.0},
525 Color{0xf3 / 255.0, 0x90 / 255.0, 0x60 / 255.0, 1.0},
526 Color{0xff / 255.0, 0xb5 / 255.0, 0x6b / 250.0, 1.0}};
527 std::vector<Scalar> stops = {
538 {100, 100}, Degrees(45), Degrees(135), std::move(colors),
539 std::move(stops), tile_mode, {});
569 std::vector<Scalar> stops = {0.0, 1.f / 3.f, 2.f / 3.f, 1.0};
570 std::array<std::tuple<Point, float, Point, float>, 8> array{
571 std::make_tuple(
Point{size / 2.f, size / 2.f}, 0.f,
572 Point{size / 2.f, size / 2.f}, size / 2.f),
573 std::make_tuple(
Point{size / 2.f, size / 2.f}, size / 4.f,
574 Point{size / 2.f, size / 2.f}, size / 2.f),
575 std::make_tuple(
Point{size / 4.f, size / 4.f}, 0.f,
576 Point{size / 2.f, size / 2.f}, size / 2.f),
577 std::make_tuple(
Point{size / 4.f, size / 4.f}, size / 2.f,
578 Point{size / 2.f, size / 2.f}, 0),
579 std::make_tuple(
Point{size / 4.f, size / 4.f}, size / 4.f,
580 Point{size / 2.f, size / 2.f}, size / 2.f),
581 std::make_tuple(
Point{size / 4.f, size / 4.f}, size / 16.f,
582 Point{size / 2.f, size / 2.f}, size / 8.f),
583 std::make_tuple(
Point{size / 4.f, size / 4.f}, size / 8.f,
584 Point{size / 2.f, size / 2.f}, size / 16.f),
585 std::make_tuple(
Point{size / 8.f, size / 8.f}, size / 8.f,
586 Point{size / 2.f, size / 2.f}, size / 8.f),
588 for (
int i = 0; i < 8; i++) {
590 canvas.
Translate({(i % 3) * size, i / 3 * size, 0});
592 std::get<0>(array[i]), std::get<1>(array[i]), colors, stops,
606 std::vector<Scalar> stops = {0.0, 1.f / 3.f, 2.f / 3.f, 1.0};
608 std::array<ColorSource, 3> color_sources = {
622 for (
int i = 0; i < 3; i++) {
632 #define APPLY_COLOR_FILTER_GRADIENT_TEST(name) \
633 TEST_P(AiksTest, name##GradientApplyColorFilter) { \
634 auto contents = name##GradientContents(); \
635 contents.SetColors({Color::CornflowerBlue().WithAlpha(0.75)}); \
636 auto result = contents.ApplyColorFilter([](const Color& color) { \
637 return color.Blend(Color::LimeGreen().WithAlpha(0.75), \
638 BlendMode::kScreen); \
640 ASSERT_TRUE(result); \
642 std::vector<Color> expected = {Color(0.433247, 0.879523, 0.825324, 0.75)}; \
643 ASSERT_COLORS_NEAR(contents.GetColors(), expected); \
653 auto callback = [&](
AiksContext& renderer) -> std::optional<Picture> {
654 static float scale = 3;
655 static bool add_circle_clip =
true;
656 const char* tile_mode_names[] = {
"Clamp",
"Repeat",
"Mirror",
"Decal"};
660 static int selected_tile_mode = 0;
661 static float alpha = 1;
663 ImGui::Begin(
"Controls",
nullptr, ImGuiWindowFlags_AlwaysAutoResize);
664 ImGui::SliderFloat(
"Scale", &scale, 0, 6);
665 ImGui::Checkbox(
"Circle clip", &add_circle_clip);
666 ImGui::SliderFloat(
"Alpha", &alpha, 0, 1);
667 ImGui::Combo(
"Tile mode", &selected_tile_mode, tile_mode_names,
668 sizeof(tile_mode_names) /
sizeof(
char*));
672 canvas.
Scale(GetContentScale());
680 auto tile_mode = tile_modes[selected_tile_mode];
682 std::vector<Color> colors = {
Color{0.9568, 0.2627, 0.2118, 1.0},
683 Color{0.1294, 0.5882, 0.9529, 1.0}};
684 std::vector<Scalar> stops = {0.0, 1.0};
687 {0, 0}, {50, 50}, std::move(colors), std::move(stops), tile_mode, {});
691 .QuadraticCurveTo({60, 20}, {60, 60})
694 .QuadraticCurveTo({60, 60}, {20, 60})
699 if (add_circle_clip) {
704 Point point_a = screen_to_canvas * handle_a * GetContentScale();
705 Point point_b = screen_to_canvas * handle_b * GetContentScale();
707 Point middle = (point_a + point_b) / 2;
716 canvas.
DrawPath(path.Clone(), paint);
725 ASSERT_TRUE(OpenPlaygroundHere(callback));