GradientPainting.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Checked.h>
  7. #include <AK/Math.h>
  8. #include <LibGfx/Gamma.h>
  9. #include <LibGfx/Line.h>
  10. #include <LibWeb/CSS/StyleValue.h>
  11. #include <LibWeb/Painting/GradientPainting.h>
  12. namespace Web::Painting {
  13. static float normalized_gradient_angle_radians(float gradient_angle)
  14. {
  15. // Adjust angle so 0 degrees is bottom
  16. float real_angle = 90 - gradient_angle;
  17. return real_angle * (AK::Pi<float> / 180);
  18. }
  19. static float calulate_gradient_length(Gfx::IntSize const& gradient_size, float sin_angle, float cos_angle)
  20. {
  21. return AK::fabs(gradient_size.height() * sin_angle) + AK::fabs(gradient_size.width() * cos_angle);
  22. }
  23. static float calulate_gradient_length(Gfx::IntSize const& gradient_size, float gradient_angle)
  24. {
  25. float angle = normalized_gradient_angle_radians(gradient_angle);
  26. float sin_angle, cos_angle;
  27. AK::sincos(angle, sin_angle, cos_angle);
  28. return calulate_gradient_length(gradient_size, sin_angle, cos_angle);
  29. }
  30. LinearGradientData resolve_linear_gradient_data(Layout::Node const& node, Gfx::FloatSize const& gradient_size, CSS::LinearGradientStyleValue const& linear_gradient)
  31. {
  32. auto& color_stop_list = linear_gradient.color_stop_list();
  33. VERIFY(color_stop_list.size() >= 2);
  34. ColorStopList resolved_color_stops;
  35. resolved_color_stops.ensure_capacity(color_stop_list.size());
  36. for (auto& stop : color_stop_list)
  37. resolved_color_stops.append(ColorStop { .color = stop.color_stop.color });
  38. auto gradient_angle = linear_gradient.angle_degrees(gradient_size);
  39. auto gradient_length_px = calulate_gradient_length(gradient_size.to_rounded<int>(), gradient_angle);
  40. auto gradient_length = CSS::Length::make_px(gradient_length_px);
  41. // 1. If the first color stop does not have a position, set its position to 0%.
  42. auto& first_stop = color_stop_list.first().color_stop;
  43. resolved_color_stops.first().position = first_stop.length.has_value()
  44. ? first_stop.length->resolved(node, gradient_length).to_px(node)
  45. : 0;
  46. // If the last color stop does not have a position, set its position to 100%
  47. auto& last_stop = color_stop_list.last().color_stop;
  48. resolved_color_stops.last().position = last_stop.length.has_value()
  49. ? last_stop.length->resolved(node, gradient_length).to_px(node)
  50. : gradient_length_px;
  51. // FIXME: Handle transition hints
  52. // 2. If a color stop or transition hint has a position that is less than the
  53. // specified position of any color stop or transition hint before it in the list,
  54. // set its position to be equal to the largest specified position of any color stop
  55. // or transition hint before it.
  56. auto max_previous_color_stop = resolved_color_stops[0].position;
  57. for (size_t i = 1; i < color_stop_list.size(); i++) {
  58. auto& stop = color_stop_list[i];
  59. if (stop.color_stop.length.has_value()) {
  60. float value = stop.color_stop.length->resolved(node, gradient_length).to_px(node);
  61. value = max(value, max_previous_color_stop);
  62. resolved_color_stops[i].position = value;
  63. max_previous_color_stop = value;
  64. }
  65. }
  66. // 3. If any color stop still does not have a position, then, for each run of adjacent color stops
  67. // without positions, set their positions so that they are evenly spaced between the preceding
  68. // and following color stops with positions.
  69. size_t i = 1;
  70. auto find_run_end = [&] {
  71. while (i < color_stop_list.size() - 1 && !color_stop_list[i].color_stop.length.has_value()) {
  72. i++;
  73. }
  74. return i;
  75. };
  76. while (i < color_stop_list.size() - 1) {
  77. auto& stop = color_stop_list[i];
  78. if (!stop.color_stop.length.has_value()) {
  79. auto run_start = i - 1;
  80. auto run_end = find_run_end();
  81. auto start_position = resolved_color_stops[run_start].position;
  82. auto end_position = resolved_color_stops[run_end].position;
  83. auto spacing = (end_position - start_position) / (run_end - run_start);
  84. for (auto j = run_start + 1; j < run_end; j++) {
  85. resolved_color_stops[j].position = start_position + (j - run_start) * spacing;
  86. }
  87. }
  88. i++;
  89. }
  90. return { gradient_angle, resolved_color_stops };
  91. }
  92. static float mix(float x, float y, float a)
  93. {
  94. return x * (1 - a) + y * a;
  95. }
  96. // Note: Gfx::gamma_accurate_blend() is NOT correct for linear gradients!
  97. static Gfx::Color color_mix(Gfx::Color x, Gfx::Color y, float a)
  98. {
  99. if (x.alpha() == y.alpha() || x.with_alpha(0) == y.with_alpha(0)) {
  100. return Gfx::Color {
  101. round_to<u8>(mix(x.red(), y.red(), a)),
  102. round_to<u8>(mix(x.green(), y.green(), a)),
  103. round_to<u8>(mix(x.blue(), y.blue(), a)),
  104. round_to<u8>(mix(x.alpha(), y.alpha(), a)),
  105. };
  106. }
  107. // Use slower but more visually pleasing premultiplied alpha mixing if both the color and alpha differ.
  108. // https://drafts.csswg.org/css-images/#coloring-gradient-line
  109. auto mixed_alpha = mix(x.alpha(), y.alpha(), a);
  110. auto premultiplied_mix_channel = [&](float channel_x, float channel_y, float a) {
  111. return round_to<u8>(mix(channel_x * (x.alpha() / 255.0f), channel_y * (y.alpha() / 255.0f), a) / (mixed_alpha / 255.0f));
  112. };
  113. return Gfx::Color {
  114. premultiplied_mix_channel(x.red(), y.red(), a),
  115. premultiplied_mix_channel(x.green(), y.green(), a),
  116. premultiplied_mix_channel(x.blue(), y.blue(), a),
  117. round_to<u8>(mixed_alpha),
  118. };
  119. }
  120. void paint_linear_gradient(PaintContext& context, Gfx::IntRect const& gradient_rect, LinearGradientData const& data)
  121. {
  122. float angle = normalized_gradient_angle_radians(data.gradient_angle);
  123. float sin_angle, cos_angle;
  124. AK::sincos(angle, sin_angle, cos_angle);
  125. auto length = calulate_gradient_length(gradient_rect.size(), sin_angle, cos_angle);
  126. Gfx::FloatPoint offset { cos_angle * (length / 2), sin_angle * (length / 2) };
  127. auto center = gradient_rect.translated(-gradient_rect.location()).center();
  128. auto start_point = center.to_type<float>() - offset;
  129. // Rotate gradient line to be horizontal
  130. auto rotated_start_point_x = start_point.x() * cos_angle - start_point.y() * -sin_angle;
  131. // FIXME: Handle transition hint interpolation
  132. auto linear_step = [](float min, float max, float value) -> float {
  133. if (value < min)
  134. return 0.;
  135. if (value > max)
  136. return 1.;
  137. return (value - min) / (max - min);
  138. };
  139. Vector<Gfx::Color, 1024> gradient_line_colors;
  140. auto int_length = round_to<int>(length);
  141. gradient_line_colors.resize(int_length);
  142. auto& color_stops = data.color_stops;
  143. for (int loc = 0; loc < int_length; loc++) {
  144. Gfx::Color gradient_color = color_mix(
  145. color_stops[0].color,
  146. color_stops[1].color,
  147. linear_step(
  148. color_stops[0].position,
  149. color_stops[1].position,
  150. loc));
  151. for (size_t i = 1; i < color_stops.size() - 1; i++) {
  152. gradient_color = color_mix(
  153. gradient_color,
  154. color_stops[i + 1].color,
  155. linear_step(
  156. color_stops[i].position,
  157. color_stops[i + 1].position,
  158. loc));
  159. }
  160. gradient_line_colors[loc] = gradient_color;
  161. }
  162. auto lookup_color = [&](int loc) {
  163. return gradient_line_colors[clamp(loc, 0, int_length - 1)];
  164. };
  165. for (int y = 0; y < gradient_rect.height(); y++) {
  166. for (int x = 0; x < gradient_rect.width(); x++) {
  167. auto loc = (x * cos_angle - (gradient_rect.height() - y) * -sin_angle) - rotated_start_point_x;
  168. // Blend between the two neighbouring colors (this fixes some nasty aliasing issues at small angles)
  169. auto blend = loc - static_cast<int>(loc);
  170. auto gradient_color = color_mix(lookup_color(loc - 1), lookup_color(loc), blend);
  171. context.painter().set_pixel(gradient_rect.x() + x, gradient_rect.y() + y, gradient_color, gradient_color.alpha() < 255);
  172. }
  173. }
  174. }
  175. }