dymokomi/luce-browser-render
Source of release 0.6.2
0.6.2 / src / raster / color_math.lucb
#==============================================================================================
#
# color_math - the per-lane math of Skia's color-conversion stages
#
# DESCRIPTION:
# Port of the highp stages of Skia m144's src/opts/SkRasterPipeline_opts.h that convert
# colors between color spaces, as one lane computes them on arm64 NEON (mad is a fused
# vfmaq_f32, nmad vfmsq_f32, min/max vminq/vmaxq, round vcvtnq): unpremul and
# unpremul_polar, and the CSS color stages gradients interpolated in other
# color spaces end with (css_lab_to_xyz, css_oklab_to_linear_srgb, css_hcl_to_lab
# with Skia's sin_/cos_ approximations, css_hsl_to_srgb, css_hwb_to_srgb). Plain
# arithmetic is unfused, as Skia is built (-ffp-contract=off).
#
# Copyright (c) 2018 Google Inc. All rights reserved.
# Copyright (c) 2026 the luce-browser authors.
# SPDX-License-Identifier: BSD-3-Clause (see LICENSE-skia)
#
#==============================================================================================
# mark: Alpha ==================================================================================
## unpremul: rgb divided by alpha (0 where 1/alpha is not below infinity).
func color_unpremul(c: f32[4]) -> f32[4]:
let inv = 1.0 / c[3]
let scale = inv if inv < math32.infinity else 0.0
return [c[0] * scale, c[1] * scale, c[2] * scale, c[3]]
## unpremul_polar: the two non-hue channels divided by alpha.
func color_unpremul_polar(c: f32[4]) -> f32[4]:
let inv = 1.0 / c[3]
let scale = inv if inv < math32.infinity else 0.0
return [c[0], c[1] * scale, c[2] * scale, c[3]]
# mark: CSS color stages =======================================================================
## css_lab_to_xyz: CIE Lab to XYZ D50.
func css_lab_to_xyz(c: f32[4]) -> f32[4]:
let k: f32 = 24389.0 / 27.0
let e: f32 = 216.0 / 24389.0
let f1 = (c[0] + 16.0) * (1.0 / 116.0)
let f0 = (c[1] * (1.0 / 500.0)) + f1
let f2 = f1 - (c[2] * (1.0 / 200.0))
let c0 = f0 * f0 * f0
let c1 = f1 * f1 * f1
let c2 = f2 * f2 * f2
let x = c0 if c0 > e else (116.0 * f0 - 16.0) * (1.0 / k)
let y = c1 if c[0] > k * e else c[0] * (1.0 / k)
let z = c2 if c2 > e else (116.0 * f2 - 16.0) * (1.0 / k)
let d50_x: f32 = 0.3457 / 0.3585
let d50_z: f32 = (1.0 - 0.3457 - 0.3585) / 0.3585
return [x * d50_x, y * 1.0, z * d50_z, c[3]]
## css_oklab_to_linear_srgb.
func css_oklab_to_linear_srgb(c: f32[4]) -> f32[4]:
let l_ = c[0] + 0.3963377774 * c[1] + 0.2158037573 * c[2]
let m_ = c[0] - 0.1055613458 * c[1] - 0.0638541728 * c[2]
let s_ = c[0] - 0.0894841775 * c[1] - 1.2914855480 * c[2]
let l = l_ * l_ * l_
let m = m_ * m_ * m_
let s = s_ * s_ * s_
return [4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s, -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s, c[3]]
## sin5q_: sin(x * 2 * pi) for x in [-1/4, 1/4], a degree 5 polynomial.
func sin5q(x: f32) -> f32:
let x2 = x * x
return x * math32.fma(math32.fma(x2, 74.4388885, -41.1693687), x2, 6.28230858)
## sin_.
func pipeline_sin(x0: f32) -> f32:
let one_over_pi2: f32 = 1.0 / (2.0 * 3.14159265)
var x = math32.fma(x0, -one_over_pi2, 0.25)
x = 0.25 - math32.abs(x - math32.floor(x + 0.5))
return sin5q(x)
## cos_.
func pipeline_cos(x0: f32) -> f32:
let one_over_pi2: f32 = 1.0 / (2.0 * 3.14159265)
var x = x0 * one_over_pi2
x = 0.25 - math32.abs(x - math32.floor(x + 0.5))
return sin5q(x)
## css_hcl_to_lab: hue (degrees), chroma, lightness to L, a, b.
func css_hcl_to_lab(c: f32[4]) -> f32[4]:
let hue_radians = c[0] * (3.14159265 / 180.0)
return [c[2], c[1] * pipeline_cos(hue_radians), c[1] * pipeline_sin(hue_radians), c[3]]
## mod_: x - y * floor(x / y), with the multiply by 1 / y and a fused nmad.
func pipeline_mod(x: f32, y: f32) -> f32:
return math32.fma(-y, math32.floor(x * (1.0 / y)), x)
## css_hsl_to_srgb_.
func css_hsl_to_srgb_rgb(h0: f32, s0: f32, l0: f32) -> (f32, f32, f32):
let h = pipeline_mod(h0, 360.0)
let s = s0 * 0.01
let l = l0 * 0.01
let k0 = pipeline_mod(0.0 + h * (1.0 / 30.0), 12.0)
let k1 = pipeline_mod(8.0 + h * (1.0 / 30.0), 12.0)
let k2 = pipeline_mod(4.0 + h * (1.0 / 30.0), 12.0)
let a = s * f32_vmin(l, 1.0 - l)
return (l - a * f32_vmax(-1.0, f32_vmin(f32_vmin(k0 - 3.0, 9.0 - k0), 1.0)), l - a * f32_vmax(-1.0, f32_vmin(f32_vmin(k1 - 3.0, 9.0 - k1), 1.0)),
l - a * f32_vmax(-1.0, f32_vmin(f32_vmin(k2 - 3.0, 9.0 - k2), 1.0)))
## css_hsl_to_srgb.
func css_hsl_to_srgb(c: f32[4]) -> f32[4]:
let rgb = css_hsl_to_srgb_rgb(c[0], c[1], c[2])
return [rgb.0, rgb.1, rgb.2, c[3]]
## css_hwb_to_srgb.
func css_hwb_to_srgb(c: f32[4]) -> f32[4]:
let g = c[1] * 0.01
let b = c[2] * 0.01
let gray = g / (g + b)
let rgb = css_hsl_to_srgb_rgb(c[0], 100.0, 50.0)
if (g + b) >= 1.0:
return [gray, gray, gray, c[3]]
return [rgb.0 * (1.0 - g - b) + g, rgb.1 * (1.0 - g - b) + g, rgb.2 * (1.0 - g - b) + g, c[3]]