UNPKG

4.39 kBSCSSView Raw
1// Foundation for Sites by ZURB
2// foundation.zurb.com
3// Licensed under MIT Open Source
4
5@import 'math';
6
7$contrast-warnings: true !default;
8
9////
10/// @group functions
11////
12
13/// Checks the luminance of `$color`.
14///
15/// @param {Color} $color - Color to check the luminance of.
16///
17/// @returns {Number} The luminance of `$color`.
18@function color-luminance($color) {
19 // Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
20 // Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
21 $rgba: red($color), green($color), blue($color);
22 $rgba2: ();
23
24 @for $i from 1 through 3 {
25 $rgb: nth($rgba, $i);
26 $rgb: $rgb / 255;
27
28 $rgb: if($rgb < 0.03928, $rgb / 12.92, pow(($rgb + 0.055) / 1.055, 2.4));
29
30 $rgba2: append($rgba2, $rgb);
31 }
32
33 @return 0.2126 * nth($rgba2, 1) + 0.7152 * nth($rgba2, 2) + 0.0722 * nth($rgba2, 3);
34}
35
36/// Checks the contrast ratio of two colors.
37///
38/// @param {Color} $color1 - First color to compare.
39/// @param {Color} $color2 - Second color to compare.
40///
41/// @returns {Number} The contrast ratio of the compared colors.
42@function color-contrast($color1, $color2) {
43 // Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
44 // Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
45 $luminance1: color-luminance($color1) + 0.05;
46 $luminance2: color-luminance($color2) + 0.05;
47 $ratio: $luminance1 / $luminance2;
48
49 @if $luminance2 > $luminance1 {
50 $ratio: 1 / $ratio;
51 }
52
53 $ratio: round($ratio * 10) / 10;
54
55 @return $ratio;
56}
57
58/// Checks the luminance of `$base`, and returns the color from `$colors` (list of colors) that has the most contrast.
59///
60/// @param {Color} $base - Color to check luminance.
61/// @param {List} $colors [($white, $black)] - Colors to compare.
62/// @param {Number} $tolerance [$global-color-pick-contrast-tolerance] - Contrast tolerance.
63///
64/// @returns {Color} the color from `$colors` (list of colors) that has the most contrast.
65@function color-pick-contrast($base, $colors: ($white, $black), $tolerance: $global-color-pick-contrast-tolerance) {
66 $contrast: color-contrast($base, nth($colors, 1));
67 $best: nth($colors, 1);
68
69 @for $i from 2 through length($colors) {
70 $current-contrast: color-contrast($base, nth($colors, $i));
71 @if ($current-contrast - $contrast > $tolerance) {
72 $contrast: color-contrast($base, nth($colors, $i));
73 $best: nth($colors, $i);
74 }
75 }
76
77 @if ($contrast-warnings and $contrast < 3) {
78 @warn "Contrast ratio of #{$best} on #{$base} is pretty bad, just #{$contrast}";
79 }
80
81 @return $best;
82}
83
84/// Scales a color to be darker if it's light, or lighter if it's dark. Use this function to tint a color appropriate to its lightness.
85///
86/// @param {Color} $color - Color to scale.
87/// @param {Percentage} $scale [5%] - Amount to scale up or down.
88/// @param {Percentage} $threshold [40%] - Threshold of lightness to check against.
89///
90/// @returns {Color} A scaled color.
91@function smart-scale($color, $scale: 5%, $threshold: 40%) {
92 @if lightness($color) > $threshold {
93 $scale: -$scale;
94 }
95 @return scale-color($color, $lightness: $scale);
96}
97
98/// Get color from foundation-palette
99///
100/// @param {key} color key from foundation-palette
101///
102/// @returns {Color} color from foundation-palette
103@function get-color($key) {
104 @if map-has-key($foundation-palette, $key) {
105 @return map-get($foundation-palette, $key);
106 }
107 @else {
108 @error 'given $key is not available in $foundation-palette';
109 }
110}
111
112/// Transfers the colors in the `$foundation-palette`map into variables, such as `$primary-color` and `$secondary-color`. Call this mixin below the Global section of your settings file to properly migrate your codebase.
113@mixin add-foundation-colors() {
114 @if map-has-key($foundation-palette, primary) {
115 $primary-color: map-get($foundation-palette, primary) !global;
116 }
117 @if map-has-key($foundation-palette, secondary) {
118 $secondary-color: map-get($foundation-palette, secondary) !global;
119 }
120 @if map-has-key($foundation-palette, success) {
121 $success-color: map-get($foundation-palette, success) !global;
122 }
123 @if map-has-key($foundation-palette, warning) {
124 $warning-color: map-get($foundation-palette, warning) !global;
125 }
126 @if map-has-key($foundation-palette, alert) {
127 $alert-color: map-get($foundation-palette, alert) !global;
128 }
129}