@csstools/postcss-contrast-color-function 1.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/CHANGELOG.md +10 -0
- package/LICENSE.md +18 -0
- package/README.md +115 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.mjs +1 -0
- package/package.json +75 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Changes to PostCSS Contrast Color Function
|
|
2
|
+
|
|
3
|
+
### 1.0.0
|
|
4
|
+
|
|
5
|
+
_March 31, 2024_
|
|
6
|
+
|
|
7
|
+
- Initial version
|
|
8
|
+
- Updated [`@csstools/color-helpers`](https://github.com/csstools/postcss-plugins/tree/main/packages/color-helpers) to [`4.1.0`](https://github.com/csstools/postcss-plugins/tree/main/packages/color-helpers/CHANGELOG.md#410) (minor)
|
|
9
|
+
- Updated [`@csstools/postcss-progressive-custom-properties`](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-progressive-custom-properties) to [`3.2.0`](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-progressive-custom-properties/CHANGELOG.md#320) (minor)
|
|
10
|
+
- Updated [`@csstools/css-color-parser`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-color-parser) to [`1.6.3`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-color-parser/CHANGELOG.md#163) (patch)
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
MIT No Attribution (MIT-0)
|
|
2
|
+
|
|
3
|
+
Copyright © CSSTools Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the “Software”), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
9
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
10
|
+
so.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
13
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
14
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
15
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
16
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
17
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
18
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# PostCSS Contrast Color Function [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][PostCSS]
|
|
2
|
+
|
|
3
|
+
[<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/postcss-contrast-color-function.svg" height="20">][npm-url] [<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url] [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]<br><br>[<img alt="Baseline Status" src="https://cssdb.org/images/badges-baseline/contrast-color-function.svg" height="20">][css-url] [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/contrast-color-function.svg" height="20">][css-url]
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @csstools/postcss-contrast-color-function --save-dev
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
[PostCSS Contrast Color Function] lets you dynamically specify a text color with adequate contrast following the [CSS Color 5 Specification].
|
|
10
|
+
|
|
11
|
+
```pcss
|
|
12
|
+
.dynamic {
|
|
13
|
+
color: contrast-color(oklch(82% 0.2 330));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.max {
|
|
17
|
+
color: contrast-color(oklch(30% 0.2 79) max);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/* becomes */
|
|
21
|
+
|
|
22
|
+
.dynamic {
|
|
23
|
+
color: color(display-p3 0.15441 0 0.16001);
|
|
24
|
+
color: contrast-color(oklch(82% 0.2 330));
|
|
25
|
+
}@supports not (color: contrast-color(red max)) {@media (prefers-contrast: more) {.dynamic {
|
|
26
|
+
color: rgb(0, 0, 0);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}@supports not (color: contrast-color(red max)) {@media (prefers-contrast: less) {.dynamic {
|
|
30
|
+
color: color(display-p3 0.29262 0 0.30192);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.max {
|
|
36
|
+
color: rgb(255, 255, 255);
|
|
37
|
+
color: contrast-color(oklch(30% 0.2 79) max);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
Add [PostCSS Contrast Color Function] to your project:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install postcss @csstools/postcss-contrast-color-function --save-dev
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Use it as a [PostCSS] plugin:
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
const postcss = require('postcss');
|
|
53
|
+
const postcssContrastColorFunction = require('@csstools/postcss-contrast-color-function');
|
|
54
|
+
|
|
55
|
+
postcss([
|
|
56
|
+
postcssContrastColorFunction(/* pluginOptions */)
|
|
57
|
+
]).process(YOUR_CSS /*, processOptions */);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
[PostCSS Contrast Color Function] runs in all Node environments, with special
|
|
61
|
+
instructions for:
|
|
62
|
+
|
|
63
|
+
- [Node](INSTALL.md#node)
|
|
64
|
+
- [PostCSS CLI](INSTALL.md#postcss-cli)
|
|
65
|
+
- [PostCSS Load Config](INSTALL.md#postcss-load-config)
|
|
66
|
+
- [Webpack](INSTALL.md#webpack)
|
|
67
|
+
- [Next.js](INSTALL.md#nextjs)
|
|
68
|
+
- [Gulp](INSTALL.md#gulp)
|
|
69
|
+
- [Grunt](INSTALL.md#grunt)
|
|
70
|
+
|
|
71
|
+
## Options
|
|
72
|
+
|
|
73
|
+
### preserve
|
|
74
|
+
|
|
75
|
+
The `preserve` option determines whether the original notation
|
|
76
|
+
is preserved. By default, it is preserved.
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
postcssContrastColorFunction({ preserve: false })
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```pcss
|
|
83
|
+
.dynamic {
|
|
84
|
+
color: contrast-color(oklch(82% 0.2 330));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.max {
|
|
88
|
+
color: contrast-color(oklch(30% 0.2 79) max);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/* becomes */
|
|
92
|
+
|
|
93
|
+
.dynamic {
|
|
94
|
+
color: color(display-p3 0.15441 0 0.16001);
|
|
95
|
+
}@media (prefers-contrast: more) {.dynamic {
|
|
96
|
+
color: rgb(0, 0, 0);
|
|
97
|
+
}
|
|
98
|
+
}@media (prefers-contrast: less) {.dynamic {
|
|
99
|
+
color: color(display-p3 0.29262 0 0.30192);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.max {
|
|
104
|
+
color: rgb(255, 255, 255);
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|
109
|
+
[css-url]: https://cssdb.org/#contrast-color-function
|
|
110
|
+
[discord]: https://discord.gg/bUadyRwkJS
|
|
111
|
+
[npm-url]: https://www.npmjs.com/package/@csstools/postcss-contrast-color-function
|
|
112
|
+
|
|
113
|
+
[PostCSS]: https://github.com/postcss/postcss
|
|
114
|
+
[PostCSS Contrast Color Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-contrast-color-function
|
|
115
|
+
[CSS Color 5 Specification]: https://drafts.csswg.org/css-color-5/#contrast-color
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("@csstools/postcss-progressive-custom-properties"),o=require("@csstools/utilities"),r=require("@csstools/css-tokenizer"),s=require("@csstools/css-parser-algorithms"),t=require("@csstools/css-color-parser"),n=require("@csstools/color-helpers");const a=/^contrast-color$/i;function parseContrastColor(e){if(!s.isFunctionNode(e)||!a.test(e.getName()))return!1;const o=e.value.filter((e=>!s.isWhitespaceNode(e)&&!s.isCommentNode(e)));if(o.length>2)return!1;const t=o[0],n=o[1];return!!t&&(n?!(!s.isTokenNode(n)||n.value[0]!==r.TokenType.Ident||"max"!==n.value[4].value.toLowerCase())&&[t,"max"]:[t])}var c;!function(e){e[e.MORE=0]="MORE",e[e.LESS=1]="LESS",e[e.NO_PREFERENCE=2]="NO_PREFERENCE"}(c||(c={}));const l=/\bcontrast-color\(/i;function transformContrastColor(e,o,a=0){const i=s.replaceComponentValues(s.parseCommaSeparatedListOfComponentValues(r.tokenize({css:e})),(e=>{const a=parseContrastColor(e);if(!a)return;const[l,i]=a;if("max"===i){const o=t.color(e);if(!o)return;return t.serializeRGB(o,!0)}if(i)return;const u=t.color(new s.FunctionNode([r.TokenType.Function,"contrast-color(",-1,-1,{value:"contrast-color"}],[r.TokenType.CloseParen,")",-1,-1,void 0],[l,new s.TokenNode([r.TokenType.Ident,"max",-1,-1,{value:"max"}])]));if(!u)return;if(o===c.MORE)return t.serializeRGB(u,!0);const p=t.color(l);if(!p)return;let f=0;const m=t.color(t.serializeRGB(p,!0));if(!m)return;{const e=n.contrast_ratio_wcag_2_1(m.channels,[0,0,0]),r=n.contrast_ratio_wcag_2_1(m.channels,[1,1,1]);f=o===c.LESS?e>=r?.3:.9:e>=r?.2:.95}const v=t.color(new s.FunctionNode([r.TokenType.Function,"oklch(",-1,-1,{value:"oklch"}],[r.TokenType.CloseParen,")",-1,-1,void 0],[new s.TokenNode([r.TokenType.Ident,"from",-1,-1,{value:"from"}]),l,new s.TokenNode([r.TokenType.Number,f.toString(),-1,-1,{value:f,type:r.NumberType.Number}]),new s.TokenNode([r.TokenType.Ident,"c",-1,-1,{value:"c"}]),new s.TokenNode([r.TokenType.Ident,"h",-1,-1,{value:"h"}])]));if(!v)return;const d=t.color(t.serializeRGB(v,!0));if(!d)return;return n.contrast_ratio_wcag_2_1(m.channels,d.channels)<4.5?t.serializeRGB(u,!0):t.serializeP3(v,!0)})),u=s.stringify(i);return u===e?e:a>10?u:l.test(u)?transformContrastColor(u,o,a+1):u}const basePlugin=e=>({postcssPlugin:"postcss-contrast-color-function",prepare:()=>({postcssPlugin:"postcss-contrast-color-function",Declaration(r,{atRule:s}){const t=r.parent;if(!t)return;if(!l.test(r.value))return;if(o.hasFallback(r))return;if(o.hasSupportsAtRuleAncestor(r,l))return;const n=transformContrastColor(r.value,c.NO_PREFERENCE);if(n===r.value)return;const a=transformContrastColor(r.value,c.LESS);if(a===r.value)return;const i=transformContrastColor(r.value,c.MORE);if(i!==r.value){if(r.cloneBefore({value:n}),n!==a){const o=t.clone();o.removeAll(),o.append(r.clone({value:a}));const n=s({name:"media",params:"(prefers-contrast: less)",source:t.source});if(n.append(o),e?.preserve){const e=s({name:"supports",params:"not (color: contrast-color(red max))",source:t.source});e.append(n),t.after(e)}else t.after(n)}if(n!==i){const o=t.clone();o.removeAll(),o.append(r.clone({value:i}));const n=s({name:"media",params:"(prefers-contrast: more)",source:t.source});if(n.append(o),e?.preserve){const e=s({name:"supports",params:"not (color: contrast-color(red max))",source:t.source});e.append(n),t.after(e)}else t.after(n)}e?.preserve||r.remove()}}})});basePlugin.postcss=!0;const postcssPlugin=o=>{const r=Object.assign({enableProgressiveCustomProperties:!0,preserve:!0},o);return r.enableProgressiveCustomProperties&&r.preserve?{postcssPlugin:"postcss-contrast-color-function",plugins:[e(),basePlugin(r)]}:basePlugin(r)};postcssPlugin.postcss=!0,module.exports=postcssPlugin;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PluginCreator } from 'postcss';
|
|
2
|
+
|
|
3
|
+
/** postcss-contrast-color-function plugin options */
|
|
4
|
+
export declare type pluginOptions = {
|
|
5
|
+
/** Preserve the original notation. default: true */
|
|
6
|
+
preserve?: boolean;
|
|
7
|
+
/** Enable "@csstools/postcss-progressive-custom-properties". default: true */
|
|
8
|
+
enableProgressiveCustomProperties?: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
declare const postcssPlugin: PluginCreator<pluginOptions>;
|
|
12
|
+
export default postcssPlugin;
|
|
13
|
+
|
|
14
|
+
export { }
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import r from"@csstools/postcss-progressive-custom-properties";import{hasFallback as o,hasSupportsAtRuleAncestor as e}from"@csstools/utilities";import{TokenType as s,tokenize as t,NumberType as n}from"@csstools/css-tokenizer";import{isFunctionNode as c,isWhitespaceNode as a,isCommentNode as l,isTokenNode as u,replaceComponentValues as i,parseCommaSeparatedListOfComponentValues as p,FunctionNode as f,TokenNode as m,stringify as v}from"@csstools/css-parser-algorithms";import{color as C,serializeRGB as E,serializeP3 as d}from"@csstools/css-color-parser";import{contrast_ratio_wcag_2_1 as g}from"@csstools/color-helpers";const P=/^contrast-color$/i;function parseContrastColor(r){if(!c(r)||!P.test(r.getName()))return!1;const o=r.value.filter((r=>!a(r)&&!l(r)));if(o.length>2)return!1;const e=o[0],t=o[1];return!!e&&(t?!(!u(t)||t.value[0]!==s.Ident||"max"!==t.value[4].value.toLowerCase())&&[e,"max"]:[e])}var h;!function(r){r[r.MORE=0]="MORE",r[r.LESS=1]="LESS",r[r.NO_PREFERENCE=2]="NO_PREFERENCE"}(h||(h={}));const R=/\bcontrast-color\(/i;function transformContrastColor(r,o,e=0){const c=i(p(t({css:r})),(r=>{const e=parseContrastColor(r);if(!e)return;const[t,c]=e;if("max"===c){const o=C(r);if(!o)return;return E(o,!0)}if(c)return;const a=C(new f([s.Function,"contrast-color(",-1,-1,{value:"contrast-color"}],[s.CloseParen,")",-1,-1,void 0],[t,new m([s.Ident,"max",-1,-1,{value:"max"}])]));if(!a)return;if(o===h.MORE)return E(a,!0);const l=C(t);if(!l)return;let u=0;const i=C(E(l,!0));if(!i)return;{const r=g(i.channels,[0,0,0]),e=g(i.channels,[1,1,1]);u=o===h.LESS?r>=e?.3:.9:r>=e?.2:.95}const p=C(new f([s.Function,"oklch(",-1,-1,{value:"oklch"}],[s.CloseParen,")",-1,-1,void 0],[new m([s.Ident,"from",-1,-1,{value:"from"}]),t,new m([s.Number,u.toString(),-1,-1,{value:u,type:n.Number}]),new m([s.Ident,"c",-1,-1,{value:"c"}]),new m([s.Ident,"h",-1,-1,{value:"h"}])]));if(!p)return;const v=C(E(p,!0));if(!v)return;return g(i.channels,v.channels)<4.5?E(a,!0):d(p,!0)})),a=v(c);return a===r?r:e>10?a:R.test(a)?transformContrastColor(a,o,e+1):a}const basePlugin=r=>({postcssPlugin:"postcss-contrast-color-function",prepare:()=>({postcssPlugin:"postcss-contrast-color-function",Declaration(s,{atRule:t}){const n=s.parent;if(!n)return;if(!R.test(s.value))return;if(o(s))return;if(e(s,R))return;const c=transformContrastColor(s.value,h.NO_PREFERENCE);if(c===s.value)return;const a=transformContrastColor(s.value,h.LESS);if(a===s.value)return;const l=transformContrastColor(s.value,h.MORE);if(l!==s.value){if(s.cloneBefore({value:c}),c!==a){const o=n.clone();o.removeAll(),o.append(s.clone({value:a}));const e=t({name:"media",params:"(prefers-contrast: less)",source:n.source});if(e.append(o),r?.preserve){const r=t({name:"supports",params:"not (color: contrast-color(red max))",source:n.source});r.append(e),n.after(r)}else n.after(e)}if(c!==l){const o=n.clone();o.removeAll(),o.append(s.clone({value:l}));const e=t({name:"media",params:"(prefers-contrast: more)",source:n.source});if(e.append(o),r?.preserve){const r=t({name:"supports",params:"not (color: contrast-color(red max))",source:n.source});r.append(e),n.after(r)}else n.after(e)}r?.preserve||s.remove()}}})});basePlugin.postcss=!0;const postcssPlugin=o=>{const e=Object.assign({enableProgressiveCustomProperties:!0,preserve:!0},o);return e.enableProgressiveCustomProperties&&e.preserve?{postcssPlugin:"postcss-contrast-color-function",plugins:[r(),basePlugin(e)]}:basePlugin(e)};postcssPlugin.postcss=!0;export{postcssPlugin as default};
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@csstools/postcss-contrast-color-function",
|
|
3
|
+
"description": "Dynamically specify a text color with adequate contrast",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"contributors": [
|
|
6
|
+
{
|
|
7
|
+
"name": "Antonio Laguna",
|
|
8
|
+
"email": "antonio@laguna.es",
|
|
9
|
+
"url": "https://antonio.laguna.es"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"name": "Romain Menke",
|
|
13
|
+
"email": "romainmenke@gmail.com"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT-0",
|
|
17
|
+
"funding": [
|
|
18
|
+
{
|
|
19
|
+
"type": "github",
|
|
20
|
+
"url": "https://github.com/sponsors/csstools"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"type": "opencollective",
|
|
24
|
+
"url": "https://opencollective.com/csstools"
|
|
25
|
+
}
|
|
26
|
+
],
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": "^14 || ^16 || >=18"
|
|
29
|
+
},
|
|
30
|
+
"type": "module",
|
|
31
|
+
"main": "dist/index.cjs",
|
|
32
|
+
"module": "dist/index.mjs",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": {
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"default": "./dist/index.mjs"
|
|
38
|
+
},
|
|
39
|
+
"require": {
|
|
40
|
+
"default": "./dist/index.cjs"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"CHANGELOG.md",
|
|
46
|
+
"LICENSE.md",
|
|
47
|
+
"README.md",
|
|
48
|
+
"dist"
|
|
49
|
+
],
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@csstools/color-helpers": "^4.1.0",
|
|
52
|
+
"@csstools/css-color-parser": "^1.6.3",
|
|
53
|
+
"@csstools/css-parser-algorithms": "^2.6.1",
|
|
54
|
+
"@csstools/css-tokenizer": "^2.2.4",
|
|
55
|
+
"@csstools/postcss-progressive-custom-properties": "^3.2.0",
|
|
56
|
+
"@csstools/utilities": "^1.0.0"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"postcss": "^8.4"
|
|
60
|
+
},
|
|
61
|
+
"homepage": "https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-contrast-color-function#readme",
|
|
62
|
+
"repository": {
|
|
63
|
+
"type": "git",
|
|
64
|
+
"url": "git+https://github.com/csstools/postcss-plugins.git",
|
|
65
|
+
"directory": "plugins/postcss-contrast-color-function"
|
|
66
|
+
},
|
|
67
|
+
"bugs": "https://github.com/csstools/postcss-plugins/issues",
|
|
68
|
+
"keywords": [
|
|
69
|
+
"contrast",
|
|
70
|
+
"contrast-color",
|
|
71
|
+
"css",
|
|
72
|
+
"postcss-plugin",
|
|
73
|
+
"wcag"
|
|
74
|
+
]
|
|
75
|
+
}
|