From 5dd2f37ed5916cf9f8298171179e17cf7ab8a2a0 Mon Sep 17 00:00:00 2001 From: cyphercodes Date: Mon, 1 Jun 2026 06:19:35 +0300 Subject: [PATCH] Fix luminance with non-integer color channels --- scss/_functions.scss | 2 +- scss/tests/functions/_luminance.test.scss | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 scss/tests/functions/_luminance.test.scss diff --git a/scss/_functions.scss b/scss/_functions.scss index 59d431a15b93..50122076eb18 100644 --- a/scss/_functions.scss +++ b/scss/_functions.scss @@ -188,7 +188,7 @@ $_luminance-list: .0008 .001 .0011 .0013 .0015 .0017 .002 .0022 .0025 .0027 .003 ); @each $name, $value in $rgb { - $value: if(divide($value, 255) < .04045, divide(divide($value, 255), 12.92), nth($_luminance-list, $value + 1)); + $value: if(divide($value, 255) < .04045, divide(divide($value, 255), 12.92), nth($_luminance-list, min(255, max(0, round($value))) + 1)); $rgb: map-merge($rgb, ($name: $value)); } diff --git a/scss/tests/functions/_luminance.test.scss b/scss/tests/functions/_luminance.test.scss new file mode 100644 index 000000000000..55b4575ff864 --- /dev/null +++ b/scss/tests/functions/_luminance.test.scss @@ -0,0 +1,22 @@ +@import "../../functions"; + +// Simulate Sass color channel functions returning non-integer channel values. +@function red($color) { + @return 241.5; +} + +@function green($color) { + @return 135.5; +} + +@function blue($color) { + @return 8.5; +} + +@include describe("luminance function") { + @include it("should round non-integer channel values before reading the luminance table") { + $result: luminance(#f7941c); + + @include assert-true($result > 0 and $result < 1, "Non-integer color channels should not crash luminance()"); + } +}