3232 }
3333}
3434
35+ // Colors
36+ @function to-rgb ($value ) {
37+ @return red ($value ), green ($value ), blue ($value );
38+ }
39+
40+ // stylelint-disable scss/dollar-variable-pattern
41+ @function rgba-css-var ($identifier , $target ) {
42+ @if $identifier == ' body' and $target == ' bg' {
43+ @return rgba (
44+ var (--#{$variable-prefix}#{$identifier}-bg-rgb ),
45+ var (--#{$variable-prefix}#{$target}-opacity )
46+ );
47+ }
48+ @if $identifier == ' body' and $target == ' text' {
49+ @return rgba (
50+ var (--#{$variable-prefix}#{$identifier}-color-rgb ),
51+ var (--#{$variable-prefix}#{$target}-opacity )
52+ );
53+ } @else {
54+ @return rgba (
55+ var (--#{$variable-prefix}#{$identifier}-rgb ),
56+ var (--#{$variable-prefix}#{$target}-opacity )
57+ );
58+ }
59+ }
60+
61+ @function map-loop ($map , $func , $args ... ) {
62+ $_map : ();
63+
64+ @each $key , $value in $map {
65+ // allow to pass the $key and $value of the map as an function argument
66+ $_args : ();
67+ @each $arg in $args {
68+ $_args : append ($_args , if ($arg == ' $key' , $key , if ($arg == ' $value' , $value , $arg )));
69+ }
70+
71+ $_map : map-merge (
72+ $_map ,
73+ (
74+ $key : call (get-function ($func ), $_args ... ),
75+ )
76+ );
77+ }
78+
79+ @return $_map ;
80+ }
81+ // stylelint-enable scss/dollar-variable-pattern
82+
83+ @function varify ($list ) {
84+ $result : null;
85+ @each $entry in $list {
86+ $result : append ($result , var (--#{$variable-prefix}#{$entry} ), space );
87+ }
88+ @return $result ;
89+ }
90+
3591// Internal Bootstrap function to turn maps into its negative variant.
3692// It prefixes the keys with `n` and makes the value negative.
3793@function negativify-map ($map ) {
60116 @return $result ;
61117}
62118
119+ // Merge multiple maps
120+ @function map-merge-multiple ($maps ... ) {
121+ $merged-maps : ();
122+
123+ @each $map in $maps {
124+ $merged-maps : map-merge ($merged-maps , $map );
125+ }
126+ @return $merged-maps ;
127+ }
128+
63129// Replace `$search` with `$replace` in `$string`
64130// Used on our SVG icon backgrounds for custom forms.
65131//
101167// Color contrast
102168// See https://github.com/twbs/bootstrap/pull/30168
103169
104- // A list of pre-calculated numbers of pow(( $value / 255 + .055) / 1.055, 2.4). (from 0 to 255)
170+ // A list of pre-calculated numbers of pow(divide((divide( $value, 255) + .055), 1.055) , 2.4). (from 0 to 255)
105171// stylelint-disable-next-line scss/dollar-variable-default, scss/dollar-variable-pattern
106172$_luminance-list : 0.0008 0.001 0.0011 0.0013 0.0015 0.0017 0.002 0.0022 0.0025 0.0027 0.003 0.0033
107173 0.0037 0.004 0.0044 0.0048 0.0052 0.0056 0.006 0.0065 0.007 0.0075 0.008 0.0086 0.0091 0.0097
@@ -152,7 +218,7 @@ $_luminance-list: 0.0008 0.001 0.0011 0.0013 0.0015 0.0017 0.002 0.0022 0.0025 0
152218 $l1 : luminance ($background );
153219 $l2 : luminance (opaque ($background , $foreground ));
154220
155- @return if ($l1 > $l2 , ($l1 + 0.05 ) / ( $l2 + 0.05 ), ($l2 + 0.05 ) / ( $l1 + 0.05 ));
221+ @return if ($l1 > $l2 , divide ($l1 + 0.05 , $l2 + 0.05 ), divide ($l2 + 0.05 , $l1 + 0.05 ));
156222}
157223
158224// Return WCAG2.0 relative luminance
@@ -166,7 +232,11 @@ $_luminance-list: 0.0008 0.001 0.0011 0.0013 0.0015 0.0017 0.002 0.0022 0.0025 0
166232 );
167233
168234 @each $name , $value in $rgb {
169- $value : if ($value / 255 < 0.03928 , $value / 255 / 12.92 , nth ($_luminance-list , $value + 1 ));
235+ $value : if (
236+ divide ($value , 255 ) < 0.03928 ,
237+ divide (divide ($value , 255 ), 12.92 ),
238+ nth ($_luminance-list , $value + 1 )
239+ );
170240 $rgb : map-merge (
171241 $rgb ,
172242 (
@@ -240,9 +310,55 @@ $_luminance-list: 0.0008 0.001 0.0011 0.0013 0.0015 0.0017 0.002 0.0022 0.0025 0
240310 @return $value1 - $value2 ;
241311 }
242312
313+ @if type-of ($value2 ) != number {
314+ $value2 : unquote (' (' ) + $value2 + unquote (' )' );
315+ }
316+
243317 @return if (
244318 $return-calc == true ,
245319 calc (#{$value1 } - #{$value2 } ),
246320 $value1 + unquote (' - ' ) + $value2
247321 );
248322}
323+
324+ @function divide ($dividend , $divisor , $precision : 10 ) {
325+ $sign : if ($dividend > 0 and $divisor > 0 or $dividend < 0 and $divisor < 0 , 1 , -1 );
326+ $dividend : abs ($dividend );
327+ $divisor : abs ($divisor );
328+ @if $dividend == 0 {
329+ @return 0 ;
330+ }
331+ @if $divisor == 0 {
332+ @error " Cannot divide by 0" ;
333+ }
334+ $remainder : $dividend ;
335+ $result : 0 ;
336+ $factor : 10 ;
337+ @while ($remainder > 0 and $precision >= 0 ) {
338+ $quotient : 0 ;
339+ @while ($remainder >= $divisor ) {
340+ $remainder : $remainder - $divisor ;
341+ $quotient : $quotient + 1 ;
342+ }
343+ $result : $result * 10 + $quotient ;
344+ $factor : $factor * 0.1 ;
345+ $remainder : $remainder * 10 ;
346+ $precision : $precision - 1 ;
347+ @if ($precision < 0 and $remainder >= $divisor * 5 ) {
348+ $result : $result + 1 ;
349+ }
350+ }
351+ $result : $result * $factor * $sign ;
352+ $dividend-unit : unit ($dividend );
353+ $divisor-unit : unit ($divisor );
354+ $unit-map : (
355+ ' px' : 1px ,
356+ ' rem' : 1rem ,
357+ ' em' : 1em ,
358+ ' %' : 1% ,
359+ );
360+ @if ($dividend-unit != $divisor-unit and map-has-key ($unit-map , $dividend-unit )) {
361+ $result : $result * map-get ($unit-map , $dividend-unit );
362+ }
363+ @return $result ;
364+ }
0 commit comments