Truly independent web browser
LibWeb: Use the page color of Ladybird's pages as the dark canvas
Andreas Klingcc72e4231e3e
diff --git a/Libraries/LibCompositing/Rust/src/force_dark.rs b/Libraries/LibCompositing/Rust/src/force_dark.rsindex 82d27c4e6ba..2cd3bf05d52 100644--- a/Libraries/LibCompositing/Rust/src/force_dark.rs+++ b/Libraries/LibCompositing/Rust/src/force_dark.rs@@ -84,20 +84,23 @@ pub fn should_invert(color: Color, role: ForceDarkRole, settings: &ForceDarkSettbrightness(color) < threshold}-// Grays landing between these two snap down to the lower one, so the near-blacks an inverted page produces come out-// as one dark surface color instead of a spread of almost-identical values.+// Grays landing between these two snap to the dark surface color, so the near-blacks an inverted page produces come+// out as one surface instead of a spread of almost-identical values.const GRAY_BRIGHTNESS_THRESHOLD: f32 = 32.0 / 255.0;-const GRAY_ADJUSTED_BRIGHTNESS: f32 = 18.0 / 255.0;+const GRAY_BAND_FLOOR: f32 = 18.0 / 255.0;-// Lightness inversion is L -> LIGHTNESS_LIFT - L, clamped to 1.0. The value is twice the Oklab lightness of sRGB-// mid-gray, which makes mid-gray invert to itself and lands white inside the gray band above.-const LIGHTNESS_LIFT: f32 = 1.20;+// Lightness inversion is L -> LIGHTNESS_LIFT - L, clamped to 1.0. The value is one plus the Oklab lightness of the dark+// surface gray, so white inverts onto the dark surface by itself and the grays above it keep their spacing. Snapping+// white up to the surface instead would crush it against the light grays that land just above the gray band.+const LIGHTNESS_LIFT: f32 = 1.2264;// The contrast floor selection colors are raised to, against the dark surface color; Chrome's// color_utils::kMinimumVisibleContrastRatio.const MINIMUM_VISIBLE_CONTRAST_RATIO: f64 = 3.0;const MINIMUM_READABLE_CONTRAST_RATIO: f64 = 4.5;-const DARK_SURFACE: Color = Color::from_rgb(18, 18, 18);+// NB: This is the dark Canvas system color, so that a page darkened here lands on the same surface as a page that is+// dark on its own.+const DARK_SURFACE: Color = Color::from_rgb(28, 28, 30);fn srgb_channel_to_linear(c: f32) -> f32 {if c <= 0.04045 {@@ -159,9 +162,13 @@ pub fn adjust_gray(color: Color) -> Color {const EPSILON: f32 = 1.0 / 255.0;let is_gray = (r - g).abs() <= EPSILON && (r - b).abs() <= EPSILON;- if is_gray && r < GRAY_BRIGHTNESS_THRESHOLD && r > GRAY_ADJUSTED_BRIGHTNESS {- let level = (GRAY_ADJUSTED_BRIGHTNESS * 255.0).round() as u8;- return Color::from_rgba(level, level, level, color.alpha());+ if is_gray && r < GRAY_BRIGHTNESS_THRESHOLD && r > GRAY_BAND_FLOOR {+ return Color::from_rgba(+ DARK_SURFACE.red(),+ DARK_SURFACE.green(),+ DARK_SURFACE.blue(),+ color.alpha(),+ );}color}@@ -685,19 +692,18 @@ mod tests {#[test]fn white_lands_on_the_dark_surface_value() {- // White inverts to just inside adjust_gray's band and snaps to the surface value. Pinning the exact result+ // White inverts to the surface gray, inside adjust_gray's band, and snaps to the surface value. Pinning the exact result// matters: a lift landing white below the band crushes it toward black, and brightness > 0 wouldn't notice.- assert_eq!(invert_lightness(WHITE), Color::from_rgb(18, 18, 18));+ assert_eq!(invert_lightness(WHITE), DARK_SURFACE);}#[test]- fn mid_gray_is_a_fixed_point() {- // The lift is chosen so that sRGB mid-gray inverts to itself, making the inversion a reflection about the- // midpoint rather than a slide toward one end.+ fn mid_gray_stays_near_the_middle() {+ // The lift slides the inversion a little toward light, so mid-gray comes out slightly lighter, not far off.let inverted = invert_lightness(Color::from_rgb(128, 128, 128));assert!(- inverted.red().abs_diff(128) <= 1,- "mid-gray should invert to itself, got {}",+ inverted.red().abs_diff(136) <= 1,+ "mid-gray should invert to about 136, got {}",inverted.red());}@@ -735,7 +741,7 @@ mod tests {#[test]fn adjust_gray_snaps_near_black_grays_to_the_dark_surface_value() {- assert_eq!(adjust_gray(Color::from_rgb(20, 20, 20)), Color::from_rgb(18, 18, 18));+ assert_eq!(adjust_gray(Color::from_rgb(20, 20, 20)), DARK_SURFACE);// Not a gray: left alone even though it is dark.let dark_blue = Color::from_rgb(0, 0, 40);assert_eq!(adjust_gray(dark_blue), dark_blue);diff --git a/Libraries/LibWeb/Rust/src/css/color_resolution.rs b/Libraries/LibWeb/Rust/src/css/color_resolution.rsindex 8b36b575049..4f28d4d058b 100644--- a/Libraries/LibWeb/Rust/src/css/color_resolution.rs+++ b/Libraries/LibWeb/Rust/src/css/color_resolution.rs@@ -549,8 +549,9 @@ fn button_text(dark: bool) -> Rgba {if dark { rgb(255, 255, 255) } else { rgb(0, 0, 0) }}+// NB: The dark canvas is the page color of Ladybird's own pages, --page in ladybird.css.fn canvas(dark: bool) -> Rgba {- if dark { rgb(18, 18, 18) } else { rgb(255, 255, 255) }+ if dark { rgb(28, 28, 30) } else { rgb(255, 255, 255) }}fn canvas_text(dark: bool) -> Rgba {@@ -2403,7 +2404,7 @@ mod tests {};assert_eq!(to_color(&keyword_value(keyword::CANVAS), &dark_input),- Some(rgb(18, 18, 18))+ Some(rgb(28, 28, 30)));assert_eq!(to_color(&keyword_value(keyword::AUTO), &EMPTY_INPUT), None);@@ -2494,7 +2495,7 @@ mod tests {system_color_for_keyword(keyword::CANVAS, false),Some(rgb(255, 255, 255)));- assert_eq!(system_color_for_keyword(keyword::CANVAS, true), Some(rgb(18, 18, 18)));+ assert_eq!(system_color_for_keyword(keyword::CANVAS, true), Some(rgb(28, 28, 30)));assert_eq!(system_color_for_keyword(keyword::SCROLLBAR, false),Some(rgb(255, 255, 255))diff --git a/Libraries/LibWebCommon/CSS/SystemColor.cpp b/Libraries/LibWebCommon/CSS/SystemColor.cppindex b47dca5ede6..45cd777f840 100644--- a/Libraries/LibWebCommon/CSS/SystemColor.cpp+++ b/Libraries/LibWebCommon/CSS/SystemColor.cpp@@ -50,10 +50,11 @@ Color button_text(PreferredColorScheme scheme)return Color(0, 0, 0);}+// NB: The dark canvas is the page color of Ladybird's own pages, --page in ladybird.css.Color canvas(PreferredColorScheme scheme){if (scheme == PreferredColorScheme::Dark) {- return Color(18, 18, 18);+ return Color(28, 28, 30);}return Color(255, 255, 255);}diff --git a/Services/Compositor/DisplayListPlayerSkia.cpp b/Services/Compositor/DisplayListPlayerSkia.cppindex a416e0868f5..3c68155d36a 100644--- a/Services/Compositor/DisplayListPlayerSkia.cpp+++ b/Services/Compositor/DisplayListPlayerSkia.cpp@@ -467,9 +467,10 @@ sk_sp<SkColorFilter> force_dark_image_color_filter()// be an exit-time destructor. The filter takes no uniforms, so one instance serves every draw.static auto* filter = [] {auto result = SkRuntimeEffect::MakeForColorFilter(SkString(R"(- const float LIGHTNESS_LIFT = 1.20;+ const float LIGHTNESS_LIFT = 1.2264;const float GRAY_BRIGHTNESS_THRESHOLD = 32.0 / 255.0;- const float GRAY_ADJUSTED_BRIGHTNESS = 18.0 / 255.0;+ const float GRAY_BAND_FLOOR = 18.0 / 255.0;+ const float3 DARK_SURFACE = float3(28.0, 28.0, 30.0) / 255.0;float3 to_linear(float3 c) {return mix(c / 12.92, pow((c + 0.055) / 1.055, float3(2.4)), step(float3(0.04045), c));@@ -516,8 +517,8 @@ sk_sp<SkColorFilter> force_dark_image_color_filter()// Grays landing in the band snap to one dark surface color, rather than a spread of near-blacks.float epsilon = 1.0 / 255.0;if (abs(inverted.r - inverted.g) <= epsilon && abs(inverted.r - inverted.b) <= epsilon- && inverted.r < GRAY_BRIGHTNESS_THRESHOLD && inverted.r > GRAY_ADJUSTED_BRIGHTNESS) {- inverted = float3(GRAY_ADJUSTED_BRIGHTNESS);+ && inverted.r < GRAY_BRIGHTNESS_THRESHOLD && inverted.r > GRAY_BAND_FLOOR) {+ inverted = DARK_SURFACE;}return half4(half3(inverted) * color.a, color.a);diff --git a/Tests/LibWeb/Ref/expected/force-dark-inverts-backgrounds-only-ref.html b/Tests/LibWeb/Ref/expected/force-dark-inverts-backgrounds-only-ref.htmlindex 51bd3504ffc..7d88a7dd0ca 100644--- a/Tests/LibWeb/Ref/expected/force-dark-inverts-backgrounds-only-ref.html+++ b/Tests/LibWeb/Ref/expected/force-dark-inverts-backgrounds-only-ref.html@@ -1,9 +1,9 @@<!DOCTYPE html><html class="reftest-wait"><style>- body { margin: 0; background: #808080; }- /* rgb(18, 18, 18) is what white inverts to: the lift lands it inside the gray band, which snaps it here. */- #box { width: 100px; height: 100px; background: rgb(18, 18, 18); border: 20px solid #cc0000; }+ body { margin: 0; background: #848484; }+ /* rgb(28, 28, 30) is what white inverts to: the lift lands it on the surface gray, which snaps it here. */+ #box { width: 100px; height: 100px; background: rgb(28, 28, 30); border: 20px solid #cc0000; }</style><div id="box"></div><script>diff --git a/Tests/LibWeb/Ref/expected/force-dark-inverts-images-like-fills-ref.html b/Tests/LibWeb/Ref/expected/force-dark-inverts-images-like-fills-ref.htmlindex f4fb39224ea..27207b96e28 100644--- a/Tests/LibWeb/Ref/expected/force-dark-inverts-images-like-fills-ref.html+++ b/Tests/LibWeb/Ref/expected/force-dark-inverts-images-like-fills-ref.html@@ -2,7 +2,7 @@<html class="reftest-wait"><style>body { margin: 0; background: #808080; }- div { position: absolute; top: 0; width: 100px; height: 100px; background: rgb(18, 18, 18); }+ div { position: absolute; top: 0; width: 100px; height: 100px; background: rgb(28, 28, 30); }#a { left: 0; }#b { left: 100px; }</style>diff --git a/Tests/LibWeb/Ref/expected/force-dark-judges-box-shadows-as-backgrounds-ref.html b/Tests/LibWeb/Ref/expected/force-dark-judges-box-shadows-as-backgrounds-ref.htmlindex 85f3e0075b0..91ae1c5a2dc 100644--- a/Tests/LibWeb/Ref/expected/force-dark-judges-box-shadows-as-backgrounds-ref.html+++ b/Tests/LibWeb/Ref/expected/force-dark-judges-box-shadows-as-backgrounds-ref.html@@ -2,12 +2,12 @@<html class="reftest-wait"><style>body { margin: 0; background: #808080; }- /* White inverts to the dark surface color, rgb(18, 18, 18); black is under the background threshold and stays. */- div { width: 100px; height: 100px; margin: 20px; background: rgb(18, 18, 18); }+ /* White inverts to the dark surface color, rgb(28, 28, 30); black is under the background threshold and stays. */+ div { width: 100px; height: 100px; margin: 20px; background: rgb(28, 28, 30); }#dark-shadow { box-shadow: 10px 10px 0 #000000; }- #light-shadow { box-shadow: 10px 10px 0 rgb(18, 18, 18); }+ #light-shadow { box-shadow: 10px 10px 0 rgb(28, 28, 30); }#dark-inset-shadow { box-shadow: inset 10px 10px 0 #000000; }- #light-inset-shadow { background: #000000; box-shadow: inset 10px 10px 0 rgb(18, 18, 18); }+ #light-inset-shadow { background: #000000; box-shadow: inset 10px 10px 0 rgb(28, 28, 30); }</style><div id="dark-shadow"></div><div id="light-shadow"></div>diff --git a/Tests/LibWeb/Ref/expected/force-dark-leaves-mask-coverage-alone-ref.html b/Tests/LibWeb/Ref/expected/force-dark-leaves-mask-coverage-alone-ref.htmlindex 62f975f22e8..9c38ecf935a 100644--- a/Tests/LibWeb/Ref/expected/force-dark-leaves-mask-coverage-alone-ref.html+++ b/Tests/LibWeb/Ref/expected/force-dark-leaves-mask-coverage-alone-ref.html@@ -1,7 +1,7 @@<!DOCTYPE html><html class="reftest-wait"><style>- body { margin: 0; background: #808080; }+ body { margin: 0; background: #848484; }</style><svg width="120" height="120" viewBox="-10 -10 120 120"><mask id="mask">@@ -9,7 +9,7 @@<rect x="0" y="0" width="50" height="100" fill="black" /></mask><rect x="-10" y="-10" width="120" height="120" fill="white" />- <rect x="10" y="10" width="80" height="80" fill="#808080" mask="url(#mask)" />+ <rect x="10" y="10" width="80" height="80" fill="#848484" mask="url(#mask)" /></svg><script>// The same page with force-dark off.diff --git a/Tests/LibWeb/Ref/input/force-dark-inverts-backgrounds-only.html b/Tests/LibWeb/Ref/input/force-dark-inverts-backgrounds-only.htmlindex a544629ebc1..184242bf2ce 100644--- a/Tests/LibWeb/Ref/input/force-dark-inverts-backgrounds-only.html+++ b/Tests/LibWeb/Ref/input/force-dark-inverts-backgrounds-only.html@@ -2,8 +2,8 @@<html class="reftest-wait"><link rel="match" href="../expected/force-dark-inverts-backgrounds-only-ref.html"><style>- /* Mid-gray is the fixed point of the inversion, so the page behind stays put either way. */- body { margin: 0; background: #808080; }+ /* #848484, just above mid-gray, is the fixed point of the inversion, so the page behind stays put either way. */+ body { margin: 0; background: #848484; }#box { width: 100px; height: 100px; background: #ffffff; border: 20px solid #cc0000; }</style><div id="box"></div>diff --git a/Tests/LibWeb/Ref/input/force-dark-leaves-mask-coverage-alone.html b/Tests/LibWeb/Ref/input/force-dark-leaves-mask-coverage-alone.htmlindex 6f06def2b79..39eafcd270b 100644--- a/Tests/LibWeb/Ref/input/force-dark-leaves-mask-coverage-alone.html+++ b/Tests/LibWeb/Ref/input/force-dark-leaves-mask-coverage-alone.html@@ -2,7 +2,7 @@<html class="reftest-wait"><link rel="match" href="../expected/force-dark-leaves-mask-coverage-alone-ref.html"><style>- body { margin: 0; background: #808080; }+ body { margin: 0; background: #848484; }</style><svg width="120" height="120" viewBox="-10 -10 120 120"><mask id="mask">@@ -10,10 +10,10 @@<rect x="0" y="0" width="50" height="100" fill="black" /></mask><rect x="-10" y="-10" width="120" height="120" fill="white" />- <rect x="10" y="10" width="80" height="80" fill="#808080" mask="url(#mask)" />+ <rect x="10" y="10" width="80" height="80" fill="#848484" mask="url(#mask)" /></svg><script>- // Every color here is a fixed point of the filter: white clears the foreground threshold, and #808080 is the+ // Every color here is a fixed point of the filter: white clears the foreground threshold, and #848484 is the// lightness the inversion maps onto itself. The only way the page can change is if force-dark reaches the mask,// where the black half is coverage rather than color, and inverting it would reveal the left half of the box.if (window.internals)diff --git a/Tests/LibWeb/Screenshot/expected-macos/color-scheme.png b/Tests/LibWeb/Screenshot/expected-macos/color-scheme.pngindex b0b3060b03d..2e7067447fc 100644Binary files a/Tests/LibWeb/Screenshot/expected-macos/color-scheme.png and b/Tests/LibWeb/Screenshot/expected-macos/color-scheme.png differdiff --git a/Tests/LibWeb/Screenshot/expected/checkbox-accent-color-and-color-scheme.png b/Tests/LibWeb/Screenshot/expected/checkbox-accent-color-and-color-scheme.pngindex 2cbd20fa18d..4659dca9ae7 100644Binary files a/Tests/LibWeb/Screenshot/expected/checkbox-accent-color-and-color-scheme.png and b/Tests/LibWeb/Screenshot/expected/checkbox-accent-color-and-color-scheme.png differdiff --git a/Tests/LibWeb/Screenshot/expected/color-scheme.png b/Tests/LibWeb/Screenshot/expected/color-scheme.pngindex e5c4c56a964..eac8ff4dc46 100644Binary files a/Tests/LibWeb/Screenshot/expected/color-scheme.png and b/Tests/LibWeb/Screenshot/expected/color-scheme.png differdiff --git a/Tests/LibWeb/Text/expected/HTML/meta-theme-color-resolution.txt b/Tests/LibWeb/Text/expected/HTML/meta-theme-color-resolution.txtindex 6100ffd4171..b37052fd5b6 100644--- a/Tests/LibWeb/Text/expected/HTML/meta-theme-color-resolution.txt+++ b/Tests/LibWeb/Text/expected/HTML/meta-theme-color-resolution.txt@@ -2,4 +2,4 @@ rgb(calc(10em / 1px) 0 0): rgb(200, 0, 0)light-dark(red, rgb(0 0 200)): rgb(0, 0, 200)currentcolor: rgb(0, 128, 0)rgb(from currentcolor r g 100): rgb(0, 128, 100)-Canvas: rgb(18, 18, 18)+Canvas: rgb(28, 28, 30)diff --git a/Tests/LibWeb/Text/expected/css/compositor-background-color-animation-scheme-dependent-colors.txt b/Tests/LibWeb/Text/expected/css/compositor-background-color-animation-scheme-dependent-colors.txtindex 743aecd8968..294b1b108f1 100644--- a/Tests/LibWeb/Text/expected/css/compositor-background-color-animation-scheme-dependent-colors.txt+++ b/Tests/LibWeb/Text/expected/css/compositor-background-color-animation-scheme-dependent-colors.txt@@ -5,7 +5,7 @@ light-dark-font-relative:background-color-animation keyframes=[rgba(200, 0, 0, 0), rgb(0, 0, 255)]compositor background-color framesystem:- background-color-animation keyframes=[rgb(18, 18, 18), rgb(255, 255, 255)]+ background-color-animation keyframes=[rgb(28, 28, 30), rgb(255, 255, 255)]compositor background-color framescripted-light-dark-font-relative:background-color-animation keyframes=[rgba(200, 0, 0, 0), rgb(0, 0, 255)]diff --git a/Tests/LibWeb/Text/expected/css/environment-change-restyles-environment-readers.txt b/Tests/LibWeb/Text/expected/css/environment-change-restyles-environment-readers.txtindex c645a415c29..6daca2d2e68 100644--- a/Tests/LibWeb/Text/expected/css/environment-change-restyles-environment-readers.txt+++ b/Tests/LibWeb/Text/expected/css/environment-change-restyles-environment-readers.txt@@ -4,5 +4,5 @@ device pixel ratio 2: lightDark=rgb(1, 1, 1) systemColor=rgb(255, 255, 255) regidevice pixel ratio 2 restyled: [html, thinBorder, imageSet]device pixel ratio 1: lightDark=rgb(1, 1, 1) systemColor=rgb(255, 255, 255) registered=rgb(3, 3, 3) thinBorder=1pxdevice pixel ratio 1 restyled: [html, thinBorder, imageSet]-preferred dark: lightDark=rgb(2, 2, 2) systemColor=rgb(18, 18, 18) registered=rgb(4, 4, 4) thinBorder=1px+preferred dark: lightDark=rgb(2, 2, 2) systemColor=rgb(28, 28, 30) registered=rgb(4, 4, 4) thinBorder=1pxpreferred dark restyled: [html, head, body, lightDark, systemColor, declaresScheme, registered, thinBorder, imageSet, plain, plainChild, script, script@2, script@3, out]