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()"); + } +}