sass-apca 1.0.4 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -1
- package/apca.scss +53 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,7 +27,30 @@ $contrast-black-on-white: apca.contrast(black, white); // 106.0406668287
|
|
|
27
27
|
$contrast-white-on-black: apca.contrast(white, black); // -107.8847261151
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
### Text color recommendation
|
|
31
|
+
Checks a light and a dark text color against a given background and returns the text color with the best contrast.
|
|
32
|
+
|
|
33
|
+
| Parameter | Type | Default | Description |
|
|
34
|
+
|:--- |:--- |:--- |:--- |
|
|
35
|
+
| $backgroundColor | [Sass color]() | | The background color to check against |
|
|
36
|
+
| $lightColor | [Sass color]() | white | [optional] A light text color |
|
|
37
|
+
| $darkColor | [Sass color]() | black | [optional] A dark text color |
|
|
38
|
+
|
|
39
|
+
```scss
|
|
40
|
+
@use 'sass-apca/apca';
|
|
41
|
+
|
|
42
|
+
body {
|
|
43
|
+
background-color: #ccc;
|
|
44
|
+
|
|
45
|
+
// either white or black text color
|
|
46
|
+
color: apca.recommend-text-color(#ccc); // white
|
|
47
|
+
|
|
48
|
+
// with custom light and dark text colors
|
|
49
|
+
color: apca.recommend-text-color(#ccc, #eee, #111); // #111
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## About contrast levels
|
|
31
54
|
These general levels are appropriate for use by themselves, without the need to reference a lookup table. APCA reports contrast as an L<sup>c</sup> value (lightness contrast) from L<sup>c</sup> 0 to L<sup>c</sup> 105+. For accessibility, consider L<sup>c</sup> 15 the point of invisibility for many users, and L<sup>c</sup> 90 is preferred for body text. [^2]
|
|
32
55
|
|
|
33
56
|
- **L<sup>c</sup> 90** • Preferred level for fluent text and columns of body text with a font no smaller than 14px/weight 400 (normal).
|
package/apca.scss
CHANGED
|
@@ -90,3 +90,56 @@ $_blueMultiplier: 0.072175;
|
|
|
90
90
|
@return ($polarity + $_Woffset) * 100;
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Compares contrasts of light or dark text against a given background and recommends
|
|
96
|
+
* usage of the text color with the best contrast.
|
|
97
|
+
*
|
|
98
|
+
* @param {color} $backgroundColor Background color
|
|
99
|
+
* @param {color} $lightColor [optional] Preferred light color, default is white
|
|
100
|
+
* @param {color} $darkColor [optional] Preferred dark color, default is black
|
|
101
|
+
*
|
|
102
|
+
* @return {color} Light or dark text color, whichever provides the better contrast against the chosen background
|
|
103
|
+
*/
|
|
104
|
+
@function recommend-text-color($backgroundColor, $lightColor: white, $darkColor: black) {
|
|
105
|
+
@if (meta.type-of($backgroundColor) != 'color') {
|
|
106
|
+
@error "Type Error: apca.recommend-text-color expects a color as first argument but received #{meta.type-of($backgroundColor)}. Please provide a valid color.";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
@if (meta.type-of($lightColor) != 'color') {
|
|
110
|
+
@error "Type Error: apca.recommend-text-color expects a color as second argument but received #{meta.type-of($lightColor)}. Please provide a valid color.";
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
@if (meta.type-of($backgroundColor) != 'color') {
|
|
114
|
+
@error "Type Error: apca.recommend-text-color expects a color as third argument but received #{meta.type-of($darkColor)}. Please provide a valid color.";
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
$lightContrast: math.abs(contrast($lightColor, $backgroundColor));
|
|
118
|
+
$darkContrast: math.abs(contrast($darkColor, $backgroundColor));
|
|
119
|
+
$contrastColor: null;
|
|
120
|
+
$bestContrast: null;
|
|
121
|
+
|
|
122
|
+
@if ($darkContrast > $lightContrast) {
|
|
123
|
+
$contrastColor: $darkColor;
|
|
124
|
+
$bestContrast: $darkContrast;
|
|
125
|
+
} @else {
|
|
126
|
+
$contrastColor: $lightColor;
|
|
127
|
+
$bestContrast: $lightContrast
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
@if ($bestContrast < 15) {
|
|
131
|
+
@warn "Your best contrast is #{$bestContrast}. This is lower than 15, the absolute minimum for any non-text that needs to be discernible and differentiable, and is no less than 6px in its smallest dimension. This may include disabled large buttons. Designers should treat anything below this level as invisible, as it will not be visible for many users. This minimum level should be avoided for any items important to the use, understanding, or interaction of the site.";
|
|
132
|
+
} @else if ($bestContrast < 30) {
|
|
133
|
+
@warn "Your best contrast is #{$bestContrast}. This is lower than 30, the absolute minimum for any text not listed above. This includes placeholder text and disabled element text. This is also the minimum for large/solid semantic & understandable non-text elements.";
|
|
134
|
+
} @else if ($bestContrast < 45) {
|
|
135
|
+
@warn "Your best contrast is #{$bestContrast}. This is lower than 45, the minimum for larger, heavier text (36px normal weight or 24px bold) such as headlines. This is also the minimum for pictograms with fine details.";
|
|
136
|
+
} @else if ($bestContrast < 60) {
|
|
137
|
+
@warn "Your best contrast is #{$bestContrast}. This is lower than 60, the minimum level recommended for content text that is not body, column, or block text. In other words, text you want people to read. The minimums: 24px normal weight (400) or 16px/700 (bold). These values based on the reference font Helvetica.";
|
|
138
|
+
} @else if ($bestContrast < 75) {
|
|
139
|
+
@warn "Your best contrast is #{$bestContrast}. This is lower than 75, the minimum level for columns of body text with a font no smaller than 18px/400. Lc 75 should be considered a minimum for text where readability is important.";
|
|
140
|
+
} @else if ($bestContrast < 90) {
|
|
141
|
+
@warn "Your best contrast is #{$bestContrast}. This is lower than 90, the preferred level for fluent text and columns of body text with a font no smaller than 14px/weight 400 (normal).";
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
@return $contrastColor;
|
|
145
|
+
}
|
package/package.json
CHANGED