@csstools/postcss-gamut-mapping 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 +8 -0
- package/LICENSE.md +18 -0
- package/README.md +84 -0
- package/dist/has-conditional-ancestor.d.ts +2 -0
- package/dist/has-override-decl.d.ts +5 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.mjs +1 -0
- package/package.json +94 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Changes to PostCSS gamut-mapping
|
|
2
|
+
|
|
3
|
+
### 1.0.0
|
|
4
|
+
|
|
5
|
+
_October 9, 2023_
|
|
6
|
+
|
|
7
|
+
- Initial version
|
|
8
|
+
- Updated [`@csstools/css-color-parser`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-color-parser) to [`1.4.0`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-color-parser/CHANGELOG.md#140) (minor)
|
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,84 @@
|
|
|
1
|
+
# PostCSS Gamut Mapping [<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-gamut-mapping.svg" height="20">][npm-url] [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/gamut-mapping.svg" height="20">][css-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]
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @csstools/postcss-gamut-mapping --save-dev
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
[PostCSS Gamut Mapping] lets you use wide gamut colors with gamut mapping for specific displays following the [CSS Color 4 Specification].
|
|
10
|
+
|
|
11
|
+
When out of gamut colors are naively clipped the result can be radically different.
|
|
12
|
+
A saturated and bright color will be much darker after clipping.
|
|
13
|
+
|
|
14
|
+
To correctly adjust colors for a narrow gamut display, the colors must be mapped.
|
|
15
|
+
This is done by lowering the `chroma` in `oklch` until the color is in gamut.
|
|
16
|
+
|
|
17
|
+
Using the `@media (color-gamut)` media feature makes it possible to only use the wide gamut colors on displays that support them.
|
|
18
|
+
|
|
19
|
+
```pcss
|
|
20
|
+
p {
|
|
21
|
+
background-color: oklch(80% 0.05 0.39 / 0.5);
|
|
22
|
+
color: oklch(20% 0.234 0.39 / 0.5);
|
|
23
|
+
border-color: color(display-p3 0 1 0);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* becomes */
|
|
27
|
+
|
|
28
|
+
p {
|
|
29
|
+
background-color: oklch(80% 0.05 0.39 / 0.5);
|
|
30
|
+
color: rgba(50, 0, 20, 0.5);
|
|
31
|
+
border-color: rgb(0, 249, 66);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@media (color-gamut: rec2020) {
|
|
35
|
+
p {
|
|
36
|
+
color: oklch(20% 0.234 0.39 / 0.5);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@media (color-gamut: p3) {
|
|
41
|
+
p {
|
|
42
|
+
border-color: color(display-p3 0 1 0);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
Add [PostCSS Gamut Mapping] to your project:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm install postcss @csstools/postcss-gamut-mapping --save-dev
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Use it as a [PostCSS] plugin:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const postcss = require('postcss');
|
|
59
|
+
const postcssGamutMapping = require('@csstools/postcss-gamut-mapping');
|
|
60
|
+
|
|
61
|
+
postcss([
|
|
62
|
+
postcssGamutMapping(/* pluginOptions */)
|
|
63
|
+
]).process(YOUR_CSS /*, processOptions */);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
[PostCSS Gamut Mapping] runs in all Node environments, with special
|
|
67
|
+
instructions for:
|
|
68
|
+
|
|
69
|
+
- [Node](INSTALL.md#node)
|
|
70
|
+
- [PostCSS CLI](INSTALL.md#postcss-cli)
|
|
71
|
+
- [PostCSS Load Config](INSTALL.md#postcss-load-config)
|
|
72
|
+
- [Webpack](INSTALL.md#webpack)
|
|
73
|
+
- [Next.js](INSTALL.md#nextjs)
|
|
74
|
+
- [Gulp](INSTALL.md#gulp)
|
|
75
|
+
- [Grunt](INSTALL.md#grunt)
|
|
76
|
+
|
|
77
|
+
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|
78
|
+
[css-url]: https://cssdb.org/#gamut-mapping
|
|
79
|
+
[discord]: https://discord.gg/bUadyRwkJS
|
|
80
|
+
[npm-url]: https://www.npmjs.com/package/@csstools/postcss-gamut-mapping
|
|
81
|
+
|
|
82
|
+
[PostCSS]: https://github.com/postcss/postcss
|
|
83
|
+
[PostCSS Gamut Mapping]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-gamut-mapping
|
|
84
|
+
[CSS Color 4 Specification]: https://www.w3.org/TR/css-color-4/#gamut-mapping
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("@csstools/css-tokenizer"),o=require("@csstools/css-parser-algorithms"),a=require("@csstools/css-color-parser");const t=/\bcolor-gamut\b/i;function hasConditionalAncestor(e){let o=e.parent;for(;o;)if("atrule"===o.type){if("media"===o.name.toLowerCase()&&t.test(o.params))return!0;o=o.parent}else o=o.parent;return!1}function hasOverrideOrFallback(e){var o;const a=e.prop.toLowerCase();let t=!1,s=!1;const r=(null==(o=e.parent)?void 0:o.nodes)??[],n=r.indexOf(e);for(let e=0;e<r.length;e++){if(e===n)continue;const o=r[e];if("decl"===o.type&&o.prop.toLowerCase()===a){if(!(e<n)){t=!0;break}s=!0,e=n}}return{hasOverride:t,hasFallback:s}}const s=/\b(?:color|lab|lch|oklab|oklch)\(/i,r=/^(?:color|lab|lch|oklab|oklch)$/i,creator=()=>({postcssPlugin:"postcss-gamut-mapping",prepare(){const t=new WeakMap;return{OnceExit:(n,{postcss:l})=>{n.walkDecls((n=>{const i=n.value;if(!s.test(i))return;if(!n.parent||hasConditionalAncestor(n))return;const{hasOverride:c,hasFallback:p}=hasOverrideOrFallback(n);if(c)return;const u=t.get(n.parent)||{conditionalRules:[],propNames:new Set,lastConditionParams:{media:void 0},lastConditionalRule:void 0};t.set(n.parent,u);let d=!1;const m=o.replaceComponentValues(o.parseCommaSeparatedListOfComponentValues(e.tokenize({css:i})),(e=>{if(!o.isFunctionNode(e)||!r.test(e.getName()))return;const t=a.color(e);return!t||t.syntaxFlags.has(a.SyntaxFlag.HasNoneKeywords)||a.colorDataFitsRGB_Gamut(t)?void 0:(d||a.colorDataFitsDisplayP3_Gamut(t)||(d=!0),a.serializeRGB(t,!0))})),f=o.stringify(m);if(f===i)return;const v=`(color-gamut: ${d?"rec2020":"p3"})`;if(u.lastConditionParams.media!==v&&(u.lastConditionalRule=void 0),u.lastConditionalRule)return p||n.cloneBefore({value:f}),u.lastConditionalRule.append(n.clone()),void n.remove();p||n.cloneBefore({value:f});const h=l.atRule({name:"media",params:v,source:n.parent.source,raws:{before:"\n\n",after:"\n"}}),C=n.parent.clone();C.removeAll(),C.raws.before="\n",C.append(n.clone()),n.remove(),u.lastConditionParams.media=h.params,u.lastConditionalRule=C,h.append(C),u.conditionalRules.push(h)})),n.walk((e=>{const o=t.get(e);o&&0!==o.conditionalRules.length&&o.conditionalRules.reverse().forEach((o=>{e.after(o)}))}))}}}});creator.postcss=!0,module.exports=creator;
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{tokenize as e}from"@csstools/css-tokenizer";import{replaceComponentValues as o,parseCommaSeparatedListOfComponentValues as t,isFunctionNode as a,stringify as r}from"@csstools/css-parser-algorithms";import{color as n,SyntaxFlag as s,colorDataFitsRGB_Gamut as l,colorDataFitsDisplayP3_Gamut as i,serializeRGB as c}from"@csstools/css-color-parser";const p=/\bcolor-gamut\b/i;function hasConditionalAncestor(e){let o=e.parent;for(;o;)if("atrule"===o.type){if("media"===o.name.toLowerCase()&&p.test(o.params))return!0;o=o.parent}else o=o.parent;return!1}function hasOverrideOrFallback(e){var o;const t=e.prop.toLowerCase();let a=!1,r=!1;const n=(null==(o=e.parent)?void 0:o.nodes)??[],s=n.indexOf(e);for(let e=0;e<n.length;e++){if(e===s)continue;const o=n[e];if("decl"===o.type&&o.prop.toLowerCase()===t){if(!(e<s)){a=!0;break}r=!0,e=s}}return{hasOverride:a,hasFallback:r}}const d=/\b(?:color|lab|lch|oklab|oklch)\(/i,u=/^(?:color|lab|lch|oklab|oklch)$/i,creator=()=>({postcssPlugin:"postcss-gamut-mapping",prepare(){const p=new WeakMap;return{OnceExit:(m,{postcss:f})=>{m.walkDecls((m=>{const h=m.value;if(!d.test(h))return;if(!m.parent||hasConditionalAncestor(m))return;const{hasOverride:v,hasFallback:b}=hasOverrideOrFallback(m);if(v)return;const k=p.get(m.parent)||{conditionalRules:[],propNames:new Set,lastConditionParams:{media:void 0},lastConditionalRule:void 0};p.set(m.parent,k);let C=!1;const g=o(t(e({css:h})),(e=>{if(!a(e)||!u.test(e.getName()))return;const o=n(e);return!o||o.syntaxFlags.has(s.HasNoneKeywords)||l(o)?void 0:(C||i(o)||(C=!0),c(o,!0))})),w=r(g);if(w===h)return;const R=`(color-gamut: ${C?"rec2020":"p3"})`;if(k.lastConditionParams.media!==R&&(k.lastConditionalRule=void 0),k.lastConditionalRule)return b||m.cloneBefore({value:w}),k.lastConditionalRule.append(m.clone()),void m.remove();b||m.cloneBefore({value:w});const O=f.atRule({name:"media",params:R,source:m.parent.source,raws:{before:"\n\n",after:"\n"}}),F=m.parent.clone();F.removeAll(),F.raws.before="\n",F.append(m.clone()),m.remove(),k.lastConditionParams.media=O.params,k.lastConditionalRule=F,O.append(F),k.conditionalRules.push(O)})),m.walk((e=>{const o=p.get(e);o&&0!==o.conditionalRules.length&&o.conditionalRules.reverse().forEach((o=>{e.after(o)}))}))}}}});creator.postcss=!0;export{creator as default};
|
package/package.json
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@csstools/postcss-gamut-mapping",
|
|
3
|
+
"description": "Gamut map css colors to fit display specific gamuts",
|
|
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
|
+
"main": "dist/index.cjs",
|
|
31
|
+
"module": "dist/index.mjs",
|
|
32
|
+
"types": "dist/index.d.ts",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"import": "./dist/index.mjs",
|
|
37
|
+
"require": "./dist/index.cjs",
|
|
38
|
+
"default": "./dist/index.mjs"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"CHANGELOG.md",
|
|
43
|
+
"LICENSE.md",
|
|
44
|
+
"README.md",
|
|
45
|
+
"dist"
|
|
46
|
+
],
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@csstools/css-color-parser": "^1.4.0",
|
|
49
|
+
"@csstools/css-parser-algorithms": "2.3.2",
|
|
50
|
+
"@csstools/css-tokenizer": "^2.2.1"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"postcss": "^8.4"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@csstools/postcss-tape": "*"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "rollup -c ../../rollup/default.mjs",
|
|
60
|
+
"docs": "node ../../.github/bin/generate-docs/install.mjs && node ../../.github/bin/generate-docs/readme.mjs",
|
|
61
|
+
"lint": "node ../../.github/bin/format-package-json.mjs",
|
|
62
|
+
"prepublishOnly": "npm run build && npm run test",
|
|
63
|
+
"test": "node .tape.mjs && node ./test/_import.mjs && node ./test/_require.cjs",
|
|
64
|
+
"test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs"
|
|
65
|
+
},
|
|
66
|
+
"homepage": "https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-gamut-mapping#readme",
|
|
67
|
+
"repository": {
|
|
68
|
+
"type": "git",
|
|
69
|
+
"url": "https://github.com/csstools/postcss-plugins.git",
|
|
70
|
+
"directory": "plugins/postcss-gamut-mapping"
|
|
71
|
+
},
|
|
72
|
+
"bugs": "https://github.com/csstools/postcss-plugins/issues",
|
|
73
|
+
"keywords": [
|
|
74
|
+
"color",
|
|
75
|
+
"css",
|
|
76
|
+
"display-p3",
|
|
77
|
+
"gamut mapping",
|
|
78
|
+
"p3",
|
|
79
|
+
"postcss",
|
|
80
|
+
"postcss-plugin",
|
|
81
|
+
"rec2020",
|
|
82
|
+
"srgb",
|
|
83
|
+
"wide gamut"
|
|
84
|
+
],
|
|
85
|
+
"csstools": {
|
|
86
|
+
"cssdbId": "gamut-mapping",
|
|
87
|
+
"exportName": "postcssGamutMapping",
|
|
88
|
+
"humanReadableName": "PostCSS Gamut Mapping",
|
|
89
|
+
"specUrl": "https://www.w3.org/TR/css-color-4/#gamut-mapping"
|
|
90
|
+
},
|
|
91
|
+
"volta": {
|
|
92
|
+
"extends": "../../package.json"
|
|
93
|
+
}
|
|
94
|
+
}
|