Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / interpolation_5.lucb

#==============================================================================================
#
#   interpolation_5 - CSS interpolation: interpolate_value_impl's longer cases, repeatable
#                     lists, filter accumulation and interpolate_value
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/CSS/Interpolation.cpp (lines 1600-2028 in part, and
#       2030-2251), by region p4a: the cases of interpolate_value_impl that are functions here
#       (basic shapes, border-radius rectangles, font styles, radial sizes, superellipses and
#       value lists), the interpolation of repeatable lists (repeated to their least common
#       multiple length), the accumulation of filter value lists, and interpolate_value
#       (interpolate_value_impl, falling back to a discrete interpolation).
#
#       Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2025-2026, Tim Ledbetter <tim.ledbetter@ladybird.org>
#       Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: interpolate_value_impl's longer cases ===================================================

## A CalculationContext whose percentages resolve as lengths (with the given accepted ranges).
func css_interpolation_length_percentage_context() -> CssCalculationContext:
    return CssCalculationContext(percentages_resolve_as = CssValueType.length, resolve_numbers_as_integers = false)

## A BasicShape's alternative index (C++'s Variant::index()).
func css_basic_shape_index(shape: const CssBasicShape*) -> u8:
    match *shape:
        .css_inset(value):
            _ = value
            return 0
        .css_xywh(value):
            _ = value
            return 1
        .css_rect(value):
            _ = value
            return 2
        .css_circle(value):
            _ = value
            return 3
        .css_ellipse(value):
            _ = value
            return 4
        .css_polygon(value):
            _ = value
            return 5
        .css_path(value):
            _ = value
            return 6

## The result of interpolate_value_impl's `interpolate_optional_position` lambda: Optional<RefPtr<StyleValue const>>,
## where OptionalNone is a failure and a null RefPtr is the interpolation of two null positions.
enum CssInterpolatedOptionalPosition:
    failed
    position(value: CssStyleValue*?)

## interpolate_value_impl's `interpolate_optional_position` lambda (case BasicShape).
func css_interpolate_optional_position(element: DomElement*, basic_shape_calculation_context: const CssCalculationContext*, from_position: CssStyleValue*?, to_position: CssStyleValue*?, delta: f32, allow_discrete: CssAllowDiscrete) -> CssInterpolatedOptionalPosition:
    if from_position == none and to_position == none:
        return CssInterpolatedOptionalPosition.position(none)

    let from_position_with_default = from_position else css_position_style_value_create_computed_center().css_style_value()
    let to_position_with_default = to_position else css_position_style_value_create_computed_center().css_style_value()

    let interpolated_position = css_interpolate_value(element, basic_shape_calculation_context, from_position_with_default, to_position_with_default, delta, allow_discrete)

    # NB: Use OptionalNone to indicate failure to interpolate since nullptr is a valid result for interpolating
    #     between two null positions.
    if interpolated_position == none:
        return CssInterpolatedOptionalPosition.failed

    return CssInterpolatedOptionalPosition.position(interpolated_position)

## interpolate_value_impl's `case StyleValue::Type::BasicShape`.
# https://drafts.csswg.org/css-shapes-1/#basic-shape-interpolation
func css_interpolate_basic_shape(element: DomElement*, from_: CssStyleValue*, to: CssStyleValue*, delta: f32, allow_discrete: CssAllowDiscrete) -> CssStyleValue*?:
    let from_shape = css_basic_shape_style_value_basic_shape(css_style_value_as_basic_shape(from_))
    let to_shape = css_basic_shape_style_value_basic_shape(css_style_value_as_basic_shape(to))
    if css_basic_shape_index(&from_shape) != css_basic_shape_index(&to_shape):
        return none

    let basic_shape_calculation_context = css_interpolation_length_percentage_context()
    let context = &basic_shape_calculation_context

    var interpolated_shape: CssBasicShape? = none
    match from_shape:
        .css_inset(from_inset):
            # If both shapes are of type inset(), interpolate between each value in the shape functions.
            let to_inset = match to_shape:
                .css_inset(value) => value
                _ => trap("VERIFY: Variant::get<Inset>()")
            let interpolated_top = css_interpolate_value(element, context, from_inset.top, to_inset.top, delta, allow_discrete)
            let interpolated_right = css_interpolate_value(element, context, from_inset.right, to_inset.right, delta, allow_discrete)
            let interpolated_bottom = css_interpolate_value(element, context, from_inset.bottom, to_inset.bottom, delta, allow_discrete)
            let interpolated_left = css_interpolate_value(element, context, from_inset.left, to_inset.left, delta, allow_discrete)

            let interpolated_border_radius = css_interpolate_value(element, context, from_inset.border_radius, to_inset.border_radius, delta, allow_discrete)

            let top = interpolated_top else return none
            let right = interpolated_right else return none
            let bottom = interpolated_bottom else return none
            let left = interpolated_left else return none
            let border_radius = interpolated_border_radius else return none

            interpolated_shape = CssBasicShape.css_inset(CssInset(top = top, right = right, bottom = bottom, left = left, border_radius = border_radius))
        .css_circle(from_circle):
            # If both shapes are the same type, that type is ellipse() or circle(), and the radiuses are specified
            # as <length-percentage> (rather than keywords), interpolate between each value in the shape functions.
            let to_circle = match to_shape:
                .css_circle(value) => value
                _ => trap("VERIFY: Variant::get<Circle>()")
            let interpolated_radius = css_interpolate_value_impl(element, context, from_circle.radius, to_circle.radius, delta, CssAllowDiscrete.no)
            let interpolated_position = css_interpolate_optional_position(element, context, from_circle.position, to_circle.position, delta, allow_discrete)
            let radius = interpolated_radius else return none
            match interpolated_position:
                .failed:
                    return none
                .position(position):
                    interpolated_shape = CssBasicShape.css_circle(CssCircle(radius = radius, position = position))
        .css_ellipse(from_ellipse):
            let to_ellipse = match to_shape:
                .css_ellipse(value) => value
                _ => trap("VERIFY: Variant::get<Ellipse>()")
            let interpolated_radius = css_interpolate_value_impl(element, context, from_ellipse.radius, to_ellipse.radius, delta, CssAllowDiscrete.no)
            let interpolated_position = css_interpolate_optional_position(element, context, from_ellipse.position, to_ellipse.position, delta, allow_discrete)
            let radius = interpolated_radius else return none
            match interpolated_position:
                .failed:
                    return none
                .position(position):
                    interpolated_shape = CssBasicShape.css_ellipse(CssEllipse(radius = radius, position = position))
        .css_polygon(from_polygon):
            # If both shapes are of type polygon(), both polygons have the same number of vertices, and use the
            # same <'fill-rule'>, interpolate between each value in the shape functions.
            let to_polygon = match to_shape:
                .css_polygon(value) => value
                _ => trap("VERIFY: Variant::get<Polygon>()")
            if from_polygon.fill_rule != to_polygon.fill_rule:
                return none
            if ak.vector_size(&from_polygon.points) != ak.vector_size(&to_polygon.points):
                return none
            var interpolated_points: ak.Vector[CssPolygonPoint]
            ak.vector_ensure_capacity(&interpolated_points, ak.vector_size(&from_polygon.points))
            for i in 0..<ak.vector_size(&from_polygon.points):
                let from_point = ak.vector_at_const(&from_polygon.points, i)
                let to_point = ak.vector_at_const(&to_polygon.points, i)
                let interpolated_point_x = css_interpolate_value(element, context, from_point.x, to_point.x, delta, allow_discrete)
                let interpolated_point_y = css_interpolate_value(element, context, from_point.y, to_point.y, delta, allow_discrete)
                let x = interpolated_point_x else return none
                let y = interpolated_point_y else return none
                ak.vector_append_t(&interpolated_points, CssPolygonPoint(x = x, y = y))

            interpolated_shape = CssBasicShape.css_polygon(CssPolygon(fill_rule = from_polygon.fill_rule, points = interpolated_points))
        _:
            return none

    let shape = interpolated_shape else return none

    return css_basic_shape_style_value_create(shape).css_style_value()

## interpolate_value_impl's `case StyleValue::Type::BorderRadiusRect`.
func css_interpolate_border_radius_rect(element: DomElement*, from_: CssStyleValue*, to: CssStyleValue*, delta: f32, allow_discrete: CssAllowDiscrete) -> CssStyleValue*?:
    var border_radius_rect_computation_context = css_interpolation_length_percentage_context()
    _ = ak.hash_map_set(&border_radius_rect_computation_context.accepted_ranges_by_type, CssValueType.length, CssNumericRange(min = 0.0, max = f64(ak.f32_max)))
    _ = ak.hash_map_set(&border_radius_rect_computation_context.accepted_ranges_by_type, CssValueType.percentage, CssNumericRange(min = 0.0, max = f64(ak.f32_max)))
    let context = &border_radius_rect_computation_context

    let from_rect = css_style_value_as_border_radius_rect(from_)
    let to_rect = css_style_value_as_border_radius_rect(to)

    let interpolated_top_left = css_interpolate_value_impl(element, context, css_border_radius_rect_style_value_top_left(from_rect), css_border_radius_rect_style_value_top_left(to_rect), delta, allow_discrete)
    let interpolated_top_right = css_interpolate_value_impl(element, context, css_border_radius_rect_style_value_top_right(from_rect), css_border_radius_rect_style_value_top_right(to_rect), delta, allow_discrete)
    let interpolated_bottom_right = css_interpolate_value_impl(element, context, css_border_radius_rect_style_value_bottom_right(from_rect), css_border_radius_rect_style_value_bottom_right(to_rect), delta, allow_discrete)
    let interpolated_bottom_left = css_interpolate_value_impl(element, context, css_border_radius_rect_style_value_bottom_left(from_rect), css_border_radius_rect_style_value_bottom_left(to_rect), delta, allow_discrete)

    let top_left = interpolated_top_left else return none
    let top_right = interpolated_top_right else return none
    let bottom_right = interpolated_bottom_right else return none
    let bottom_left = interpolated_bottom_left else return none

    return css_border_radius_rect_style_value_create(top_left, top_right, bottom_right, bottom_left).css_style_value()

## interpolate_value_impl's `case StyleValue::Type::FontStyle`.
func css_interpolate_font_style(element: DomElement*, calculation_context: const CssCalculationContext*, from_: CssStyleValue*, to: CssStyleValue*, delta: f32, allow_discrete: CssAllowDiscrete) -> CssStyleValue*?:
    let from_font_style = css_style_value_as_font_style(from_)
    let to_font_style = css_style_value_as_font_style(to)
    let from_keyword = css_keyword_style_value_create(css_data.to_keyword_font_style_keyword(css_font_style_style_value_font_style(from_font_style))).css_style_value()
    let to_keyword = css_keyword_style_value_create(css_data.to_keyword_font_style_keyword(css_font_style_style_value_font_style(to_font_style))).css_style_value()
    let interpolated_font_style = css_interpolate_value(element, calculation_context, from_keyword, to_keyword, delta, allow_discrete) else return none
    let font_style = css_data.keyword_to_font_style_keyword(css_style_value_to_keyword(interpolated_font_style)) else trap("VERIFY: Optional::value()")
    if let from_angle = css_font_style_style_value_angle(from_font_style):
        if let to_angle = css_font_style_style_value_angle(to_font_style):
            var angle_context = CssCalculationContext()
            _ = ak.hash_map_set(&angle_context.accepted_ranges_by_type, CssValueType.angle, CssNumericRange(min = -90.0, max = 90.0))
            let interpolated_angle = css_interpolate_value(element, &angle_context, from_angle, to_angle, delta, allow_discrete) else return none
            return css_font_style_style_value_create(font_style, interpolated_angle).css_style_value()

    return css_font_style_style_value_create(font_style).css_style_value()

## A RadialSizeStyleValue component's style value (`component.get<NonnullRefPtr<StyleValue const>>()`).
func css_radial_size_component_value(component: const CssRadialSizeStyleValueComponent*) -> CssStyleValue*:
    match *component:
        .css_style_value(value):
            return value
        .radial_extent(extent):
            _ = extent
            trap("VERIFY: Variant::get<NonnullRefPtr<StyleValue const>>()")

## interpolate_value_impl's and composite_value's `is_radial_extent` over a component list (any_of).
func css_radial_size_has_radial_extent(components: const ak.Vector[CssRadialSizeStyleValueComponent]*) -> bool:
    for component in ak.vector_span_const(components):
        match component:
            .radial_extent(extent):
                _ = extent
                return true
            .css_style_value(value):
                _ = value
    return false

## interpolate_value_impl's `case StyleValue::Type::RadialSize`.
func css_interpolate_radial_size(element: DomElement*, from_: CssStyleValue*, to: CssStyleValue*, delta: f32, allow_discrete: CssAllowDiscrete) -> CssStyleValue*?:
    let from_components = css_radial_size_style_value_components(css_style_value_as_radial_size(from_))
    let to_components = css_radial_size_style_value_components(css_style_value_as_radial_size(to))

    # https://drafts.csswg.org/css-images-4/#interpolating-gradients
    # https://drafts.csswg.org/css-shapes-1/#basic-shape-interpolation
    # FIXME: Radial extents should disallow interpolation for basic-shape values but should be converted into their
    #        equivalent length-percentage values for radial gradients
    if css_radial_size_has_radial_extent(&from_components) or css_radial_size_has_radial_extent(&to_components):
        return none

    var radial_size_calculation_context = css_interpolation_length_percentage_context()
    _ = ak.hash_map_set(&radial_size_calculation_context.accepted_ranges_by_type, CssValueType.length, CssNumericRange(min = 0.0, max = f64(ak.f32_max)))
    let context = &radial_size_calculation_context

    if ak.vector_size(&from_components) == 1 and ak.vector_size(&to_components) == 1:
        let from_component = css_radial_size_component_value(ak.vector_at_const(&from_components, 0))
        let to_component = css_radial_size_component_value(ak.vector_at_const(&to_components, 0))

        let interpolated_value = css_interpolate_value(element, context, from_component, to_component, delta, allow_discrete) else return none

        var components: ak.Vector[CssRadialSizeStyleValueComponent]
        ak.vector_append_t(&components, CssRadialSizeStyleValueComponent.css_style_value(interpolated_value))
        return css_radial_size_style_value_create(components).css_style_value()

    let from_horizontal_component = css_radial_size_component_value(ak.vector_at_const(&from_components, 0))
    let from_vertical_component = css_radial_size_component_value(ak.vector_at_const(&from_components, 1)) if ak.vector_size(&from_components) > 1 else from_horizontal_component

    let to_horizontal_component = css_radial_size_component_value(ak.vector_at_const(&to_components, 0))
    let to_vertical_component = css_radial_size_component_value(ak.vector_at_const(&to_components, 1)) if ak.vector_size(&to_components) > 1 else to_horizontal_component

    let interpolated_horizontal = css_interpolate_value(element, context, from_horizontal_component, to_horizontal_component, delta, allow_discrete)
    let interpolated_vertical = css_interpolate_value(element, context, from_vertical_component, to_vertical_component, delta, allow_discrete)

    let horizontal = interpolated_horizontal else return none
    let vertical = interpolated_vertical else return none

    var components: ak.Vector[CssRadialSizeStyleValueComponent]
    ak.vector_append_t(&components, CssRadialSizeStyleValueComponent.css_style_value(horizontal))
    ak.vector_append_t(&components, CssRadialSizeStyleValueComponent.css_style_value(vertical))
    return css_radial_size_style_value_create(components).css_style_value()

## interpolate_value_impl's `normalized_super_ellipse_half_corner` lambda.
# https://drafts.csswg.org/css-borders-4/#normalized-superellipse-half-corner
func css_normalized_super_ellipse_half_corner(s: f64) -> f64:
    #  To compute the normalized superellipse half corner given a superellipse parameter s, return the first matching statement, switching on s:

    # -∞ Return 0.
    if s == -ak.infinity_f64:
        return 0.0

    # ∞ Return 1.
    if s == ak.infinity_f64:
        return 1.0

    # Otherwise
    # 1. Let k be 0.5^abs(s).
    let k = math.pow(0.5, math.abs(s))

    # 2. Let convexHalfCorner be 0.5^k.
    let convex_half_corner = math.pow(0.5, k)

    # 3. If s is less than 0, return 1 - convexHalfCorner.
    if s < 0.0:
        return 1.0 - convex_half_corner

    # 4. Return convexHalfCorner.
    return convex_half_corner

## interpolate_value_impl's `interpolation_value_to_super_ellipse_parameter` lambda.
func css_interpolation_value_to_super_ellipse_parameter(interpolation_value: f64) -> f64:
    # To convert a <number [0,1]> interpolationValue back to a superellipse parameter, switch on interpolationValue:

    # 0 Return -∞.
    if interpolation_value == 0.0:
        return -ak.infinity_f64

    # 0.5 Return 0.
    if interpolation_value == 0.5:
        return 0.0

    # 1 Return ∞.
    if interpolation_value == 1.0:
        return ak.infinity_f64

    # Otherwise
    # 1. Let convexHalfCorner be interpolationValue.
    var convex_half_corner = interpolation_value

    # 2. If interpolationValue is less than 0.5, set convexHalfCorner to 1 - interpolationValue.
    if interpolation_value < 0.5:
        convex_half_corner = 1.0 - interpolation_value

    # 3. Let k be ln(0.5) / ln(convexHalfCorner).
    let k = math.log(0.5) / math.log(convex_half_corner)

    # 4. Let s be log2(k).
    var s = math.log2(k)

    # AD-HOC: The logs above can introduce slight inaccuracies, this can interfere with the behaviour of
    #         serializing superellipse style values as their equivalent keywords as that relies on exact
    #         equality. To mitigate this we simply round to a whole number if we are sufficiently near
    if math.abs(math.round(s) - s) < f64(ak.f32_epsilon):
        s = math.round(s)

    # 5. If interpolationValue is less than 0.5, return -s.
    if interpolation_value < 0.5:
        return -s

    # 6. Return s.
    return s

## interpolate_value_impl's `case StyleValue::Type::Superellipse`.
# https://drafts.csswg.org/css-borders-4/#corner-shape-interpolation
func css_interpolate_superellipse(from_: CssStyleValue*, to: CssStyleValue*, delta: f32) -> CssStyleValue*:
    let from_normalized_value = css_normalized_super_ellipse_half_corner(css_superellipse_style_value_parameter(css_style_value_as_superellipse(from_)))
    let to_normalized_value = css_normalized_super_ellipse_half_corner(css_superellipse_style_value_parameter(css_style_value_as_superellipse(to)))

    let interpolated_value = css_interpolate_raw_f64(from_normalized_value, to_normalized_value, delta, CssNumericRange(min = 0.0, max = 1.0))

    return css_superellipse_style_value_create(css_interpolation_number(css_interpolation_value_to_super_ellipse_parameter(interpolated_value))).css_style_value()

## interpolate_value_impl's `case StyleValue::Type::ValueList`.
func css_interpolate_value_list(element: DomElement*, calculation_context: const CssCalculationContext*, from_: CssStyleValue*, to: CssStyleValue*, delta: f32) -> CssStyleValue*?:
    let from_list = css_style_value_as_value_list(from_)
    let to_list = css_style_value_as_value_list(to)
    if css_style_value_list_size(from_list) != css_style_value_list_size(to_list):
        return none

    # FIXME: If the number of components or the types of corresponding components do not match,
    # or if any component value uses discrete animation and the two corresponding values do not match,
    # then the property values combine as discrete.
    let from_values = css_style_value_list_values(from_list)
    let to_values = css_style_value_list_values(to_list)
    var interpolated_values: ak.Vector[CssStyleValue*]
    ak.vector_ensure_capacity(&interpolated_values, css_style_value_list_size(from_list))
    for i in 0..<css_style_value_list_size(from_list):
        let interpolated = css_interpolate_value(element, calculation_context, *ak.vector_at_const(&from_values, i), *ak.vector_at_const(&to_values, i), delta, CssAllowDiscrete.no) else return none

        ak.vector_append_t(&interpolated_values, interpolated)

    return css_style_value_list_create(interpolated_values, css_style_value_list_separator(from_list)).css_style_value()

# mark: Repeatable lists ========================================================================

## interpolate_repeatable_list's `make_single_value_list` lambda.
func css_make_single_value_list(value: CssStyleValue*, size: usize, separator: CssStyleValueListSeparator) -> CssStyleValue*:
    var values: ak.Vector[CssStyleValue*]
    ak.vector_ensure_capacity(&values, size)
    for i in 0..<size:
        _ = i
        ak.vector_append_t(&values, value)
    return css_style_value_list_create(values, separator).css_style_value()

## Ported from Libraries/LibWeb/CSS/Interpolation.cpp:2030 Web::CSS::interpolate_repeatable_list.
pub func css_interpolate_repeatable_list(element: DomElement*, calculation_context: const CssCalculationContext*, from_: CssStyleValue*, to: CssStyleValue*, delta: f32, allow_discrete: CssAllowDiscrete) -> CssStyleValue*?:
    # https://www.w3.org/TR/web-animations/#repeatable-list
    # Same as by computed value except that if the two lists have differing numbers of items, they are first repeated to the least common multiple number of items.
    # Each item is then combined by computed value.
    # If a pair of values cannot be combined or if any component value uses discrete animation, then the property values combine as discrete.

    var from_list = from_
    var to_list = to
    if not css_style_value_is_value_list(from_) and css_style_value_is_value_list(to):
        from_list = css_make_single_value_list(from_, css_style_value_list_size(css_style_value_as_value_list(to)), css_style_value_list_separator(css_style_value_as_value_list(to)))
    elif not css_style_value_is_value_list(to) and css_style_value_is_value_list(from_):
        to_list = css_make_single_value_list(to, css_style_value_list_size(css_style_value_as_value_list(from_)), css_style_value_list_separator(css_style_value_as_value_list(from_)))
    elif not css_style_value_is_value_list(from_) and not css_style_value_is_value_list(to):
        return css_interpolate_value(element, calculation_context, from_, to, delta, allow_discrete)

    # make_repeatable_list:
    # If the number of components or the types of corresponding components do not match,
    # or if any component value uses discrete animation and the two corresponding values do not match,
    # then the property values combine as discrete
    let from_values = css_style_value_as_value_list(from_list)
    let to_values = css_style_value_as_value_list(to_list)
    var interpolated_values: ak.Vector[CssStyleValue*]
    let list_size = ak.lcm_usize(css_style_value_list_size(from_values), css_style_value_list_size(to_values))
    for i in 0..<list_size:
        let value = css_interpolate_value(element, calculation_context, css_style_value_list_value_at(from_values, i, true), css_style_value_list_value_at(to_values, i, true), delta, CssAllowDiscrete.no) else return css_interpolate_discrete(from_, to, delta, allow_discrete)
        ak.vector_append_t(&interpolated_values, value)
    return css_style_value_list_create(interpolated_values, css_style_value_list_separator(from_values)).css_style_value()

# mark: Accumulating filters ====================================================================

## accumulate_filter_function's `initial_value_for` lambda.
func css_filter_initial_value_for_accumulation(value: const CssFilterValue*) -> CssFilterValue:
    match *value:
        .css_blur(blur):
            _ = blur
            return CssFilterValue.css_blur(CssBlur(radius = css_interpolation_zero_px()))
        .css_drop_shadow(drop_shadow):
            _ = drop_shadow
            return CssFilterValue.css_drop_shadow(CssDropShadow(offset_x = css_interpolation_zero_px(), offset_y = css_interpolation_zero_px(), radius = css_interpolation_zero_px(), color = none))
        .css_hue_rotate(hue_rotate):
            _ = hue_rotate
            return CssFilterValue.css_hue_rotate(CssHueRotate(angle = css_angle_style_value_create(css_angle_make_degrees(0.0)).css_style_value()))
        .css_color(color):
            var default_value_for_accumulation = 0.0
            match color.operation:
                .grayscale, .invert, .sepia:
                    default_value_for_accumulation = 0.0
                .brightness, .contrast, .opacity, .saturate:
                    default_value_for_accumulation = 1.0
                _:
                    trap("VERIFY_NOT_REACHED")
            return CssFilterValue.css_color(CssColor(operation = color.operation, amount = css_interpolation_number(default_value_for_accumulation)))
        .css_url(url):
            _ = url
            trap("VERIFY_NOT_REACHED")

## accumulate_filter_function's `add_lengths` lambda.
func css_filter_add_lengths(a: CssStyleValue*, b: CssStyleValue*) -> CssStyleValue*:
    let a_length = css_length_from_style_value(a, none)
    let b_length = css_length_from_style_value(b, none)
    let a_value = css_length_absolute_length_to_px_without_rounding(&a_length)
    let b_value = css_length_absolute_length_to_px_without_rounding(&b_length)

    return css_length_style_value_create(css_length_make_px_f64(a_value + b_value)).css_style_value()

## A drop shadow's radius in pixels, or zero without one.
func css_filter_drop_shadow_radius_px(radius: CssStyleValue*?) -> f64:
    let value = radius else return 0.0
    let length = css_length_from_style_value(value, none)
    return css_length_absolute_length_to_px_without_rounding(&length)

## AK::min(255, a + b) over a color channel.
func css_filter_add_channels(a: u8, b: u8) -> u8:
    let sum = (i32)a + (i32)b
    return (u8)(sum if sum < 255 else 255)

## `filter_value.has<FilterOperation::Blur>()` then `get<FilterOperation::Blur>()`: the blur, or none.
func css_filter_value_as_blur(value: const CssFilterValue*) -> CssBlur?:
    match *value:
        .css_blur(blur):
            return blur
        _:
            return none

## `filter_value.has<FilterOperation::HueRotate>()` then `get<...>()`: the hue rotation, or none.
func css_filter_value_as_hue_rotate(value: const CssFilterValue*) -> CssHueRotate?:
    match *value:
        .css_hue_rotate(hue_rotate):
            return hue_rotate
        _:
            return none

## `filter_value.has<FilterOperation::Color>()` then `get<...>()`: the color filter, or none.
func css_filter_value_as_color(value: const CssFilterValue*) -> CssColor?:
    match *value:
        .css_color(color):
            return color
        _:
            return none

## `filter_value.has<FilterOperation::DropShadow>()` then `get<...>()`: the drop shadow, or none.
func css_filter_value_as_drop_shadow(value: const CssFilterValue*) -> CssDropShadow?:
    match *value:
        .css_drop_shadow(drop_shadow):
            return drop_shadow
        _:
            return none

## accumulate_filter_function's `accumulate_filter` lambda.
func css_accumulate_filter(underlying: const CssFilterValue*, animated: const CssFilterValue*) -> CssFilterValue?:
    match *underlying:
        .css_blur(underlying_blur):
            let animated_blur = css_filter_value_as_blur(animated) else return none

            return CssFilterValue.css_blur(CssBlur(radius = css_length_style_value_create(css_length_make_px_f64(f64(css_blur_resolved_radius(&underlying_blur) + css_blur_resolved_radius(&animated_blur)))).css_style_value()))
        .css_hue_rotate(underlying_rotate):
            let animated_rotate = css_filter_value_as_hue_rotate(animated) else return none

            return CssFilterValue.css_hue_rotate(CssHueRotate(angle = css_angle_style_value_create(css_angle_make_degrees(f64(css_hue_rotate_angle_degrees(&underlying_rotate) + css_hue_rotate_angle_degrees(&animated_rotate)))).css_style_value()))
        .css_color(underlying_color):
            let animated_color = css_filter_value_as_color(animated) else return none
            if underlying_color.operation != animated_color.operation:
                return none

            let underlying_amount = css_color_resolved_amount(&underlying_color)
            let animated_amount = css_color_resolved_amount(&animated_color)

            var accumulated = 0.0
            match underlying_color.operation:
                .brightness, .contrast, .opacity, .saturate:
                    accumulated = f64(underlying_amount + animated_amount) - 1.0
                .grayscale, .invert, .sepia:
                    accumulated = f64(underlying_amount + animated_amount)
                _:
                    trap("VERIFY_NOT_REACHED")

            return CssFilterValue.css_color(CssColor(operation = underlying_color.operation, amount = css_interpolation_number(accumulated)))
        .css_drop_shadow(underlying_shadow):
            let animated_shadow = css_filter_value_as_drop_shadow(animated) else return none

            let offset_x = css_filter_add_lengths(underlying_shadow.offset_x, animated_shadow.offset_x)
            let offset_y = css_filter_add_lengths(underlying_shadow.offset_y, animated_shadow.offset_y)
            var accumulated_radius: CssStyleValue*? = none
            if underlying_shadow.radius != none or animated_shadow.radius != none:
                let underlying_radius = css_filter_drop_shadow_radius_px(underlying_shadow.radius)
                let animated_radius = css_filter_drop_shadow_radius_px(animated_shadow.radius)
                accumulated_radius = css_length_style_value_create(css_length_make_px_f64(underlying_radius + animated_radius)).css_style_value()

            var accumulated_color = animated_shadow.color
            if let underlying_color_value = underlying_shadow.color:
                if let animated_color_value = animated_shadow.color:
                    let color_resolution_context = CssColorResolutionContext()
                    let maybe_underlying_color = css_style_value_to_color(underlying_color_value, color_resolution_context)
                    let maybe_animated_color = css_style_value_to_color(animated_color_value, color_resolution_context)
                    if let underlying_color = maybe_underlying_color:
                        if let animated_color = maybe_animated_color:
                            var accumulated: gfx.Color = ---
                            gfx.color_construct_u8_u8_u8_u8(&accumulated, css_filter_add_channels(gfx.color_red(&underlying_color), gfx.color_red(&animated_color)), css_filter_add_channels(gfx.color_green(&underlying_color), gfx.color_green(&animated_color)), css_filter_add_channels(gfx.color_blue(&underlying_color), gfx.color_blue(&animated_color)), css_filter_add_channels(gfx.color_alpha(&underlying_color), gfx.color_alpha(&animated_color)))
                            accumulated_color = css_color_style_value_create_from_color(accumulated, CssColorSyntax.legacy).css_style_value()

            return CssFilterValue.css_drop_shadow(CssDropShadow(offset_x = offset_x, offset_y = offset_y, radius = accumulated_radius, color = accumulated_color))
        .css_url(url):
            _ = url
            return none

## Ported from Libraries/LibWeb/CSS/Interpolation.cpp:2076 Web::CSS::accumulate_filter_function.
# https://drafts.fxtf.org/filter-effects/#accumulation
pub func css_accumulate_filter_function(underlying_list: CssFilterValueListStyleValue*, animated_list: CssFilterValueListStyleValue*) -> ak.Vector[CssFilterValue]:
    # Accumulation of <filter-value-list>s follows the same matching and extending rules as interpolation, falling
    # back to replace behavior if the lists do not match. However instead of interpolating the matching
    # <filter-function> pairs, their arguments are arithmetically added together - except in the case of
    # <filter-function>s whose initial value for interpolation is 1, which combine using one-based addition:
    # Vresult = Va + Vb - 1

    # Extend shorter list with initial values
    let underlying_size = css_filter_value_list_style_value_size(underlying_list)
    let animated_size = css_filter_value_list_style_value_size(animated_list)
    let max_size = animated_size if underlying_size < animated_size else underlying_size
    var extended_underlying: ak.Vector[CssFilterValue]
    var extended_animated: ak.Vector[CssFilterValue]

    for i in 0..<max_size:
        if i < underlying_size:
            ak.vector_append_t(&extended_underlying, *ak.vector_at_const(&underlying_list.m_filter_value_list, i))
        else:
            ak.vector_append_t(&extended_underlying, css_filter_initial_value_for_accumulation(ak.vector_at_const(&animated_list.m_filter_value_list, i)))

        if i < animated_size:
            ak.vector_append_t(&extended_animated, *ak.vector_at_const(&animated_list.m_filter_value_list, i))
        else:
            ak.vector_append_t(&extended_animated, css_filter_initial_value_for_accumulation(ak.vector_at_const(&underlying_list.m_filter_value_list, i)))

    var result: ak.Vector[CssFilterValue]
    ak.vector_ensure_capacity(&result, max_size)
    for i in 0..<max_size:
        let accumulated = css_accumulate_filter(ak.vector_at_const(&extended_underlying, i), ak.vector_at_const(&extended_animated, i)) else return ak.Vector[CssFilterValue]()
        ak.vector_append_t(&result, accumulated)
    return result

# mark: interpolate_value =======================================================================

## Ported from Libraries/LibWeb/CSS/Interpolation.cpp:2246 Web::CSS::interpolate_value.
pub func css_interpolate_value(element: DomElement*, calculation_context: const CssCalculationContext*, from_: CssStyleValue*, to: CssStyleValue*, delta: f32, allow_discrete: CssAllowDiscrete) -> CssStyleValue*?:
    if let result = css_interpolate_value_impl(element, calculation_context, from_, to, delta, allow_discrete):
        return result
    return css_interpolate_discrete(from_, to, delta, allow_discrete)