Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-render

Source of release 0.6.2

0.6.2 / src / raster / gradient_color_spaces.lucb

#==============================================================================================
#
#   gradient_color_spaces - the RGB color spaces gradients interpolate through, and the
#                           SkColorSpaceXformSteps between them and sRGB
#
#   DESCRIPTION:
#       Skia m144 interpolates a gradient's colors in an intermediate SkColorSpace
#       (SkGradientBaseShader.cpp's intermediate_color_space) and converts between it and
#       sRGB with SkColorSpaceXformSteps: unpremul, linearize, a gamut matrix, encode and
#       premul, each skipped when it changes nothing. The spaces are luce-color's port of
#       SkColorSpace::MakeRGB over Skia's named transfer functions and gamuts, the steps its
#       port of SkColorSpaceXformSteps (`icc.Steps`), run by the color_xform stage
#       (color_xform.lucb) as SkConvertPixels' and the shader's pipelines run them.
#
#       Copyright (c) 2018 Google Inc. All rights reserved.
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-3-Clause (see LICENSE-skia)
#
#==============================================================================================

## intermediate_color_space: the RGB space a gradient interpolated in `cs` converts its colors
## into first (the destination, sRGB, for the sRGB-based spaces).
func gradient_intermediate_space(cs: GradientColorSpace) -> icc.ColorSpace:
    match cs:
        .srgb_linear, .oklab, .oklch:
            return icc.ColorSpace.srgb_linear()
        .lab, .lch:
            return gradient_make_rgb(&icc.tf_linear, &icc.gamut_xyz)
        .display_p3:
            return gradient_make_rgb(&icc.tf_srgb, &icc.gamut_display_p3)
        .rec2020:
            return gradient_make_rgb(&icc.tf_rec2020, &icc.gamut_rec2020)
        .prophoto_rgb:
            # Skia computes the linear ProPhoto RGB to XYZ D50 matrix from the named primaries.
            let to_xyz = icc.primaries_prophoto_rgb.to_xyzd50() else trap("raster: ProPhoto RGB's primaries make a matrix")
            return gradient_make_rgb(&icc.tf_prophoto_rgb, &to_xyz)
        .a98_rgb:
            let to_xyz = icc.gamut_adobe_rgb()
            return gradient_make_rgb(&icc.tf_a98_rgb, &to_xyz)
        _:
            return icc.ColorSpace.srgb()

## SkColorSpace::MakeRGB of a named transfer function and gamut, which always make one.
func gradient_make_rgb(curve: const icc.TransferFunction*, to_xyz: const icc.Matrix3x3*) -> icc.ColorSpace:
    return icc.ColorSpace.rgb(curve, to_xyz) else trap("raster: a named transfer function is valid")

## The color_xform steps from `src` with `src_alpha` to `dst` with `dst_alpha`; none when no
## step runs.
func gradient_color_xform(src: const icc.ColorSpace*, src_alpha: icc.AlphaType, dst: const icc.ColorSpace*, dst_alpha: icc.AlphaType) -> ColorXform?:
    return ColorXform.make(icc.Steps.make(src, src_alpha, dst, dst_alpha))