color-contrast-js 2.0.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/API.md ADDED
@@ -0,0 +1,183 @@
1
+ # colorContrast.js API
2
+
3
+ All functions are named exports from `color-contrast-js`. TypeScript declarations are included in the package.
4
+
5
+ ```js
6
+ import { contrastRatio, meetsContrast } from "color-contrast-js";
7
+ ```
8
+
9
+ ## Common options
10
+
11
+ Functions that resolve transparency accept an optional opaque fallback color:
12
+
13
+ ```ts
14
+ type ColorOptions = {
15
+ fallback?: string; // default: "#fff"
16
+ };
17
+ ```
18
+
19
+ ## `backgroundTone(color, options?)`
20
+
21
+ Classifies the supplied background as `"light"` or `"dark"`. The result describes the background, not the recommended text color.
22
+
23
+ ```js
24
+ backgroundTone("#111"); // "dark"
25
+ backgroundTone("tomato"); // "light"
26
+ ```
27
+
28
+ ## `analyzeColor(color, options?)`
29
+
30
+ Returns the resolved color, WCAG relative luminance, black and white contrast ratios, the background tone, recommended text color, and AA/AAA compliance.
31
+
32
+ ```js
33
+ const result = analyzeColor("#FF5733");
34
+
35
+ result.backgroundTone; // "light"
36
+ result.recommendedTextColor; // "#000000"
37
+ result.contrastWithBlack; // approximately 6.66
38
+ result.blackText.aa; // true
39
+ ```
40
+
41
+ The returned object has this shape:
42
+
43
+ ```ts
44
+ type ColorAnalysis = {
45
+ color: string;
46
+ resolvedColor: string;
47
+ luminance: number;
48
+ recommendedTextColor: "#000000" | "#FFFFFF";
49
+ backgroundTone: "light" | "dark";
50
+ contrastWithBlack: number;
51
+ contrastWithWhite: number;
52
+ blackText: ContrastCompliance;
53
+ whiteText: ContrastCompliance;
54
+ };
55
+
56
+ type ContrastCompliance = {
57
+ aa: boolean;
58
+ aaLarge: boolean;
59
+ aaa: boolean;
60
+ aaaLarge: boolean;
61
+ };
62
+ ```
63
+
64
+ ## `contrastRatio(foreground, background, options?)`
65
+
66
+ Returns a WCAG contrast ratio from 1 to 21. Translucent foregrounds are composited over the background before calculation.
67
+
68
+ ```js
69
+ contrastRatio("#000", "#fff"); // 21
70
+ ```
71
+
72
+ ## `bestTextColor(background, candidates?, options?)`
73
+
74
+ Returns the candidate with the strongest contrast against the background. Candidates default to black and white.
75
+
76
+ ```js
77
+ bestTextColor("#336699"); // "#FFFFFF"
78
+ bestTextColor("#336699", ["#fff", "#000", "#ff0"]); // "#fff"
79
+ ```
80
+
81
+ ## `meetsContrast(foreground, background, options?)`
82
+
83
+ Checks a foreground/background pair against WCAG AA or AAA thresholds.
84
+
85
+ ```js
86
+ meetsContrast("#000", "#FF5733", {
87
+ level: "AA", // "AA" or "AAA"
88
+ textSize: "normal", // "normal" or "large"
89
+ }); // true
90
+ ```
91
+
92
+ ## `compositeColors(foreground, background)`
93
+
94
+ Composites a translucent foreground over a background and returns CSS RGB or RGBA.
95
+
96
+ ```js
97
+ compositeColors("rgb(0 0 0 / 50%)", "#fff");
98
+ // "rgb(128, 128, 128)"
99
+ ```
100
+
101
+ ## `parseColor(color)`
102
+
103
+ Parses a supported CSS color and returns numeric sRGB channels from 0–255 plus alpha from 0–1. Returns `null` for unsupported input.
104
+
105
+ ```js
106
+ parseColor("tomato");
107
+ // { r: 255, g: 99, b: 71, a: 1 }
108
+ ```
109
+
110
+ Supported inputs include HEX, RGB, HSL, named colors, Lab, LCH, Oklab, OKLCH, and CSS `color()` spaces. Wide-gamut inputs are converted to sRGB and clipped to the sRGB gamut.
111
+
112
+ ## `analyzePalette(colors, options?)`
113
+
114
+ Returns `analyzeColor()` results for every palette entry.
115
+
116
+ ```js
117
+ analyzePalette(["#000", "#fff", "tomato"]);
118
+ ```
119
+
120
+ ## `contrastMatrix(colors, options?)`
121
+
122
+ Returns a pairwise matrix where rows are foreground colors and columns are background colors.
123
+
124
+ ```js
125
+ contrastMatrix(["#000", "#fff"]);
126
+ // [[1, 21], [21, 1]]
127
+ ```
128
+
129
+ ## `getBackgroundColor(element, options?)`
130
+
131
+ Resolves an element's visible `background-color`, compositing translucent ancestor background colors until it reaches an opaque color or the fallback.
132
+
133
+ ```js
134
+ getBackgroundColor(document.querySelector(".card"));
135
+ ```
136
+
137
+ This helper handles solid and translucent CSS background colors. It does not analyze background images, gradients, blend modes, canvas content, or video.
138
+
139
+ ## `analyzeElement(element, options?)`
140
+
141
+ Analyzes an element's computed foreground, resolved background, font size and weight. It selects the applicable WCAG normal/large text thresholds automatically.
142
+
143
+ ```js
144
+ const result = analyzeElement(document.querySelector(".card"));
145
+
146
+ result.foregroundColor;
147
+ result.backgroundColor;
148
+ result.contrastRatio;
149
+ result.textSize; // "normal" or "large"
150
+ result.aa;
151
+ result.aaa;
152
+ ```
153
+
154
+ ## `applyBackgroundTone(target, options?)`
155
+
156
+ Applies a class describing the background tone to one element, a selector, or an iterable of elements. Every element is evaluated independently.
157
+
158
+ ```js
159
+ applyBackgroundTone(".card");
160
+
161
+ applyBackgroundTone(".card", {
162
+ lightClass: "on-light",
163
+ darkClass: "on-dark",
164
+ fallback: "#fff",
165
+ root: document,
166
+ });
167
+ ```
168
+
169
+ The function returns the affected elements.
170
+
171
+ ## Historical aliases
172
+
173
+ The original project names remain available as aliases:
174
+
175
+ - `colorBrightness` and `colourBrightness` → `backgroundTone`
176
+ - `applyColorBrightness` and `applyColourBrightness` → `applyBackgroundTone`
177
+ - `analyseColour` → `analyzeColor`
178
+ - `parseColour` → `parseColor`
179
+ - `bestTextColour` → `bestTextColor`
180
+ - `compositeColours` → `compositeColors`
181
+ - `getBackgroundColour` → `getBackgroundColor`
182
+ - `analyseElement` → `analyzeElement`
183
+ - `analysePalette` → `analyzePalette`
package/CHANGELOG.md ADDED
@@ -0,0 +1,37 @@
1
+ # Changelog
2
+
3
+ ## 2.0.0 — 2026-07-13
4
+
5
+ - Rebuilt the project as colorContrast.js, a browser-ready ES module with compatibility aliases for the original plugin names.
6
+ - Added readable and minified module builds plus a complete API reference.
7
+ - Relicensed the project under the MIT License.
8
+ - Added `backgroundTone` and `applyBackgroundTone` as clearer canonical names for the original brightness helpers.
9
+ - Added `analyzeColor`, `contrastRatio`, `bestTextColor`, `meetsContrast`, `compositeColors`, palette analysis, and contrast matrices.
10
+ - Added `analyzeElement` for computed foreground/background contrast and font-aware WCAG checks.
11
+ - Added HEX, RGB, HSL, named-color, Lab, LCH, Oklab, OKLCH, and CSS `color()` parsing.
12
+ - Switched the recommended contrast calculation to WCAG relative luminance.
13
+ - Added DOM helpers for individual elements, selectors and element collections.
14
+ - Added alpha compositing through translucent ancestor backgrounds, building on the proposal in [#9](https://github.com/jamiebrittain/colorContrast.js/pull/9) by [@ioliva](https://github.com/ioliva).
15
+ - Added TypeScript declarations, automated tests, npm package metadata, and third-party notices.
16
+ - Added strict runtime validation for parsed channel objects and consistent option errors.
17
+ - Centralized WCAG thresholds and corrected the 14pt bold large-text boundary.
18
+ - Added guarded npm packaging, readable/minified build parity checks, and continuous integration.
19
+ - Added [hex.madebynifty.com](https://hex.madebynifty.com/) as the live v2 example.
20
+
21
+ ## 1.3.0 — 2026-07-13
22
+
23
+ This release is intended to be tagged from a dedicated legacy-only commit before the v2 release. It is a GitHub compatibility release for existing jQuery users, not part of the `color-contrast-js` npm package.
24
+
25
+ - Fixed every element in a jQuery collection receiving the first element's result ([#7](https://github.com/jamiebrittain/colorContrast.js/issues/7)).
26
+ - Improved zero-alpha and transparent background detection.
27
+ - Included the HTML element when looking for an inherited background.
28
+ - Added a white fallback for a completely transparent document.
29
+ - Preserved the v1 brightness algorithm and class behavior.
30
+ - Left elements unchanged when their computed background color cannot be parsed.
31
+ - Documented that partially transparent backgrounds remain a v1 limitation; use v2 for compositing-aware analysis.
32
+ - Added regression tests and reproducible minification.
33
+
34
+ ## 1.2.0 — 2015-12-29
35
+
36
+ - Added a terminating semicolon for compatibility with concatenated scripts.
37
+ - Updated the project metadata and version header.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013-2026 Jamie Brittain
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,13 @@
1
+ # Third-party notices
2
+
3
+ The bundled CSS colour conversions use Culori.
4
+
5
+ MIT License
6
+
7
+ Copyright (c) 2018 Dan Burzo
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.