postcss-lab-function 3.1.2 → 4.0.3

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 CHANGED
@@ -1,5 +1,28 @@
1
1
  # Changes to PostCSS Lab Function
2
2
 
3
+ ### 4.0.3 (January 2, 2022)
4
+
5
+ - Removed Sourcemaps from package tarball.
6
+ - Moved CLI to CLI Package. See [announcement](https://github.com/csstools/postcss-plugins/discussions/121).
7
+
8
+ ### 4.0.2 (December 13, 2021)
9
+
10
+ - Changed: now uses `postcss-value-parser` for parsing.
11
+ - Updated: documentation
12
+ - Added: support for CSS variables with `preserve: true` option.
13
+ - Fixed: Hue values with units in `lch` functions are now correctly handled.
14
+ - Fixed: Rounding of values to match current browser behavior.
15
+
16
+ ### 4.0.1 (November 18, 2021)
17
+
18
+ - Added: Safeguards against postcss-values-parser potentially throwing an error.
19
+ - Updated: postcss-value-parser to 6.0.1 (patch)
20
+
21
+ ### 4.0.0 (September 17, 2021)
22
+
23
+ - Updated: Support for PostCS 8+ (major).
24
+ - Updated: Support for Node 12+ (major).
25
+
3
26
  ### 3.1.2 (April 25, 2020)
4
27
 
5
28
  - Updated: Publish
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [<img alt="npm version" src="https://img.shields.io/npm/v/postcss-lab-function.svg" height="20">][npm-url]
4
4
  [<img alt="CSS Standard Status" src="https://cssdb.org/badge/lab-function.svg" height="20">][css-url]
5
- [<img alt="build status" src="https://img.shields.io/travis/csstools/postcss-lab-function/master.svg" height="20">][cli-url]
5
+ [<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url]
6
6
  [<img alt="support chat" src="https://img.shields.io/badge/support-chat-blue.svg" height="20">][git-url]
7
7
 
8
8
 
@@ -28,18 +28,10 @@
28
28
  Add [PostCSS Lab Function] to your project:
29
29
 
30
30
  ```bash
31
- npm install postcss-lab-function --save-dev
31
+ npm install postcss postcss-lab-function --save-dev
32
32
  ```
33
33
 
34
- Use [PostCSS Lab Function] to process your CSS:
35
-
36
- ```js
37
- const postcssLabFunction = require('postcss-lab-function');
38
-
39
- postcssLabFunction.process(YOUR_CSS /*, processOptions, pluginOptions */);
40
- ```
41
-
42
- Or use it as a [PostCSS] plugin:
34
+ Use it as a [PostCSS] plugin:
43
35
 
44
36
  ```js
45
37
  const postcss = require('postcss');
@@ -83,7 +75,7 @@ postcssLabFunction({ preserve: true })
83
75
  }
84
76
  ```
85
77
 
86
- [cli-url]: https://travis-ci.org/csstools/postcss-lab-function
78
+ [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
87
79
  [css-url]: https://cssdb.org/#lab-function
88
80
  [git-url]: https://gitter.im/postcss/postcss
89
81
  [npm-url]: https://www.npmjs.com/package/postcss-lab-function
@@ -93,4 +85,4 @@ postcssLabFunction({ preserve: true })
93
85
  [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
94
86
  [PostCSS]: https://github.com/postcss/postcss
95
87
  [PostCSS Loader]: https://github.com/postcss/postcss-loader
96
- [PostCSS Lab Function]: https://github.com/csstools/postcss-lab-function
88
+ [PostCSS Lab Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-lab-function
@@ -0,0 +1,3 @@
1
+ export declare function labToSRgb(lab: [number, number, number]): [number, number, number];
2
+ export declare function lchToSRgb(lch: [number, number, number]): [number, number, number];
3
+ export declare function xyz2rgb(xyz: [number, number, number]): [number, number, number];
@@ -0,0 +1,2 @@
1
+ import type { Node } from 'postcss';
2
+ export declare function hasSupportsAtRuleAncestor(node: Node): boolean;
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var e=t(require("postcss-value-parser"));function n(t){return a(u(t,!0))}function r(t){return a(u(function(t){return[t[0],t[1]*Math.cos(t[2]*Math.PI/180),t[1]*Math.sin(t[2]*Math.PI/180)]}(t),!1))}function u(t,e){const[n,r,u]=t,a=Math.min(Math.max(n,0),100);let l=0,p=0;e?(l=Math.min(Math.max(r,-127),128),p=Math.min(Math.max(u,-127),128)):(l=r,p=u);const v=(a+16)/116,h=l/500+v,b=v-p/200,[m,M,g]=[Math.pow(h,3)>s?Math.pow(h,3):(116*h-16)/d,a>d*s?Math.pow((a+16)/116,3):a/d,Math.pow(b,3)>s?Math.pow(b,3):(116*b-16)/d],[w,y,x]=o([m*i,M*c,g*f],[[.9554734527042182,-.023098536874261423,.0632593086610217],[-.028369706963208136,1.0099954580058226,.021041398966943008],[.012314001688319899,-.020507696433477912,1.3303659366080753]]);return[w,y,x]}function a(t){const[e,n,r]=t,[u,a,l]=o([e,n,r],[[3.2409699419045226,-1.537383177570094,-.4986107602930034],[-.9692436362808796,1.8759675015077202,.04155505740717559],[.05563007969699366,-.20397695888897652,1.0569715142428786]]),[i,c,f]=[u,a,l].map((t=>t>.31308?1.055*Math.pow(t/100,1/2.4)*100-5.5:12.92*t));return[i,c,f]}function o(t,e){return e.map((e=>e.reduce(((e,n,r)=>e+t[r]*l*(n*l)/l/l),0)))}const l=1e8,[i,c,f]=[96.422,100,82.521],s=Math.pow(6,3)/Math.pow(29,3),d=Math.pow(29,3)/Math.pow(3,3);function p(t){const u=t.value,a=t.nodes.slice().filter((t=>"comment"!==t.type&&"space"!==t.type));let o=null;if("lab"===u?o=function(t){if(!h(t[0]))return null;if(!v(t[1]))return null;if(!v(t[2]))return null;const n={l:e.default.unit(t[0].value),lNode:t[0],a:e.default.unit(t[1].value),aNode:t[1],b:e.default.unit(t[2].value),bNode:t[2]};M(t[3])&&(n.slash=t[3]);(b(t[4])||m(t[4]))&&(n.alpha=t[4]);return n}(a):"lch"===u&&(o=function(t){if(!h(t[0]))return null;if(!v(t[1]))return null;if(!function(t){if(!t||"word"!==t.type)return!1;if(!y(t))return!1;const n=e.default.unit(t.value);if(!n)return!1;return!!n.number&&("deg"===n.unit||"grad"===n.unit||"rad"===n.unit||"turn"===n.unit||""===n.unit)}(t[2]))return null;const n={l:e.default.unit(t[0].value),lNode:t[0],c:e.default.unit(t[1].value),cNode:t[1],h:e.default.unit(t[2].value),hNode:t[2]};if(function(t){switch(t.unit){case"deg":return void(t.unit="");case"rad":return t.unit="",void(t.number=(180*parseFloat(t.number)/Math.PI).toString());case"grad":return t.unit="",void(t.number=(.9*parseFloat(t.number)).toString());case"turn":t.unit="",t.number=(360*parseFloat(t.number)).toString()}}(n.h),""!==n.h.unit)return null;M(t[3])&&(n.slash=t[3]);(b(t[4])||m(t[4])||function(t){return t&&"function"===t.type&&"var"===t.value}(t[4]))&&(n.alpha=t[4]);return n}(a)),!o)return;if(a.length>3&&(!o.slash||!o.alpha))return;t.value="rgb",function(t,n,r){if(!n||!r)return;if(t.value="rgba",n.value=",",n.before="",!function(t){if(!t||"word"!==t.type)return!1;if(!y(t))return!1;const n=e.default.unit(t.value);if(!n)return!1;return!!n.number}(r))return;const u=e.default.unit(r.value);if(!u)return;"%"===u.unit&&(u.number=String(parseFloat(u.number)/100),r.value=String(u.number))}(t,o.slash,o.alpha);const[l,i,c]=function(t){if(g(t))return[t.lNode,t.aNode,t.bNode];return[t.lNode,t.cNode,t.hNode]}(o),[f,s,d]=function(t){if(g(t))return[t.l,t.a,t.b];return[t.l,t.c,t.h]}(o),p=("lab"===u?n:r)([f.number,s.number,d.number].map((t=>parseFloat(t)))).map((t=>Math.max(Math.min(Math.round(2.55*t),255),0)));t.nodes.splice(t.nodes.indexOf(l)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),t.nodes.splice(t.nodes.indexOf(i)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),w(t.nodes,l,{...l,value:String(p[0])}),w(t.nodes,i,{...i,value:String(p[1])}),w(t.nodes,c,{...c,value:String(p[2])})}function v(t){if(!t||"word"!==t.type)return!1;if(!y(t))return!1;const n=e.default.unit(t.value);return!!n&&(!!n.number&&""===n.unit)}function h(t){if(!t||"word"!==t.type)return!1;if(!y(t))return!1;const n=e.default.unit(t.value);return!!n&&"%"===n.unit}function b(t){if(!t||"word"!==t.type)return!1;if(!y(t))return!1;const n=e.default.unit(t.value);return!!n&&("%"===n.unit||""===n.unit)}function m(t){return t&&"function"===t.type&&"calc"===t.value}function M(t){return t&&"div"===t.type&&"/"===t.value}function g(t){return void 0!==t.a}function w(t,e,n){const r=t.indexOf(e);t[r]=n}function y(t){if(!t||!t.value)return!1;try{return!1!==e.default.unit(t.value)}catch(t){return!1}}const x=t=>{const n="preserve"in Object(t)&&Boolean(t.preserve);return{postcssPlugin:"postcss-lab-function",Declaration:(t,{result:r,postcss:u})=>{if(n&&function(t){let e=t.parent;for(;e;)if("atrule"===e.type){if("supports"===e.name&&-1!==e.params.indexOf("(color: lab(0% 0 0)) and (color: lch(0% 0 0))"))return!0;e=e.parent}else e=e.parent;return!1}(t))return;const a=t.value;if(!/(^|[^\w-])(lab|lch)\(/i.test(a))return;let o;try{o=e.default(a)}catch(e){t.warn(r,`Failed to parse value '${a}' as a lab or hcl function. Leaving the original value intact.`)}if(void 0===o)return;o.walk((t=>{t.type&&"function"===t.type&&("lab"!==t.value&&"lch"!==t.value||p(t))}));const l=String(o);if(l!==a)if(n&&t.variable){const e=t.parent,n="(color: lab(0% 0 0)) and (color: lch(0% 0 0))",r=u.atRule({name:"supports",params:n,source:t.source}),a=e.clone();a.removeAll(),a.append(t.clone()),r.append(a);let o=e,i=e.next();for(;o&&i&&"atrule"===i.type&&"supports"===i.name&&i.params===n;)o=i,i=i.next();o.after(r),t.value=l}else n?t.cloneBefore({value:l}):t.value=l}}};x.postcss=!0,x._labToSRgb=n,x._lchToSRgb=r,module.exports=x;
@@ -0,0 +1,6 @@
1
+ import type { PluginCreator } from 'postcss';
2
+ /** Transform lab() and lch() functions in CSS. */
3
+ declare const postcssPlugin: PluginCreator<{
4
+ preserve: boolean;
5
+ }>;
6
+ export default postcssPlugin;
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import n from"postcss-value-parser";function t(n){return u(r(n,!0))}function e(n){return u(r(function(n){return[n[0],n[1]*Math.cos(n[2]*Math.PI/180),n[1]*Math.sin(n[2]*Math.PI/180)]}(n),!1))}function r(n,t){const[e,r,u]=n,o=Math.min(Math.max(e,0),100);let p=0,d=0;t?(p=Math.min(Math.max(r,-127),128),d=Math.min(Math.max(u,-127),128)):(p=r,d=u);const h=(o+16)/116,v=p/500+h,m=h-d/200,[b,M,g]=[Math.pow(v,3)>s?Math.pow(v,3):(116*v-16)/f,o>f*s?Math.pow((o+16)/116,3):o/f,Math.pow(m,3)>s?Math.pow(m,3):(116*m-16)/f],[w,y,x]=a([b*i,M*l,g*c],[[.9554734527042182,-.023098536874261423,.0632593086610217],[-.028369706963208136,1.0099954580058226,.021041398966943008],[.012314001688319899,-.020507696433477912,1.3303659366080753]]);return[w,y,x]}function u(n){const[t,e,r]=n,[u,o,i]=a([t,e,r],[[3.2409699419045226,-1.537383177570094,-.4986107602930034],[-.9692436362808796,1.8759675015077202,.04155505740717559],[.05563007969699366,-.20397695888897652,1.0569715142428786]]),[l,c,s]=[u,o,i].map((n=>n>.31308?1.055*Math.pow(n/100,1/2.4)*100-5.5:12.92*n));return[l,c,s]}function a(n,t){return t.map((t=>t.reduce(((t,e,r)=>t+n[r]*o*(e*o)/o/o),0)))}const o=1e8,[i,l,c]=[96.422,100,82.521],s=Math.pow(6,3)/Math.pow(29,3),f=Math.pow(29,3)/Math.pow(3,3);function p(r){const u=r.value,a=r.nodes.slice().filter((n=>"comment"!==n.type&&"space"!==n.type));let o=null;if("lab"===u?o=function(t){if(!h(t[0]))return null;if(!d(t[1]))return null;if(!d(t[2]))return null;const e={l:n.unit(t[0].value),lNode:t[0],a:n.unit(t[1].value),aNode:t[1],b:n.unit(t[2].value),bNode:t[2]};b(t[3])&&(e.slash=t[3]);(v(t[4])||m(t[4]))&&(e.alpha=t[4]);return e}(a):"lch"===u&&(o=function(t){if(!h(t[0]))return null;if(!d(t[1]))return null;if(!function(t){if(!t||"word"!==t.type)return!1;if(!w(t))return!1;const e=n.unit(t.value);if(!e)return!1;return!!e.number&&("deg"===e.unit||"grad"===e.unit||"rad"===e.unit||"turn"===e.unit||""===e.unit)}(t[2]))return null;const e={l:n.unit(t[0].value),lNode:t[0],c:n.unit(t[1].value),cNode:t[1],h:n.unit(t[2].value),hNode:t[2]};if(function(n){switch(n.unit){case"deg":return void(n.unit="");case"rad":return n.unit="",void(n.number=(180*parseFloat(n.number)/Math.PI).toString());case"grad":return n.unit="",void(n.number=(.9*parseFloat(n.number)).toString());case"turn":n.unit="",n.number=(360*parseFloat(n.number)).toString()}}(e.h),""!==e.h.unit)return null;b(t[3])&&(e.slash=t[3]);(v(t[4])||m(t[4])||function(n){return n&&"function"===n.type&&"var"===n.value}(t[4]))&&(e.alpha=t[4]);return e}(a)),!o)return;if(a.length>3&&(!o.slash||!o.alpha))return;r.value="rgb",function(t,e,r){if(!e||!r)return;if(t.value="rgba",e.value=",",e.before="",!function(t){if(!t||"word"!==t.type)return!1;if(!w(t))return!1;const e=n.unit(t.value);if(!e)return!1;return!!e.number}(r))return;const u=n.unit(r.value);if(!u)return;"%"===u.unit&&(u.number=String(parseFloat(u.number)/100),r.value=String(u.number))}(r,o.slash,o.alpha);const[i,l,c]=function(n){if(M(n))return[n.lNode,n.aNode,n.bNode];return[n.lNode,n.cNode,n.hNode]}(o),[s,f,p]=function(n){if(M(n))return[n.l,n.a,n.b];return[n.l,n.c,n.h]}(o),y=("lab"===u?t:e)([s.number,f.number,p.number].map((n=>parseFloat(n)))).map((n=>Math.max(Math.min(Math.round(2.55*n),255),0)));r.nodes.splice(r.nodes.indexOf(i)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),r.nodes.splice(r.nodes.indexOf(l)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),g(r.nodes,i,{...i,value:String(y[0])}),g(r.nodes,l,{...l,value:String(y[1])}),g(r.nodes,c,{...c,value:String(y[2])})}function d(t){if(!t||"word"!==t.type)return!1;if(!w(t))return!1;const e=n.unit(t.value);return!!e&&(!!e.number&&""===e.unit)}function h(t){if(!t||"word"!==t.type)return!1;if(!w(t))return!1;const e=n.unit(t.value);return!!e&&"%"===e.unit}function v(t){if(!t||"word"!==t.type)return!1;if(!w(t))return!1;const e=n.unit(t.value);return!!e&&("%"===e.unit||""===e.unit)}function m(n){return n&&"function"===n.type&&"calc"===n.value}function b(n){return n&&"div"===n.type&&"/"===n.value}function M(n){return void 0!==n.a}function g(n,t,e){const r=n.indexOf(t);n[r]=e}function w(t){if(!t||!t.value)return!1;try{return!1!==n.unit(t.value)}catch(n){return!1}}const y=t=>{const e="preserve"in Object(t)&&Boolean(t.preserve);return{postcssPlugin:"postcss-lab-function",Declaration:(t,{result:r,postcss:u})=>{if(e&&function(n){let t=n.parent;for(;t;)if("atrule"===t.type){if("supports"===t.name&&-1!==t.params.indexOf("(color: lab(0% 0 0)) and (color: lch(0% 0 0))"))return!0;t=t.parent}else t=t.parent;return!1}(t))return;const a=t.value;if(!/(^|[^\w-])(lab|lch)\(/i.test(a))return;let o;try{o=n(a)}catch(n){t.warn(r,`Failed to parse value '${a}' as a lab or hcl function. Leaving the original value intact.`)}if(void 0===o)return;o.walk((n=>{n.type&&"function"===n.type&&("lab"!==n.value&&"lch"!==n.value||p(n))}));const i=String(o);if(i!==a)if(e&&t.variable){const n=t.parent,e="(color: lab(0% 0 0)) and (color: lch(0% 0 0))",r=u.atRule({name:"supports",params:e,source:t.source}),a=n.clone();a.removeAll(),a.append(t.clone()),r.append(a);let o=n,l=n.next();for(;o&&l&&"atrule"===l.type&&"supports"===l.name&&l.params===e;)o=l,l=l.next();o.after(r),t.value=i}else e?t.cloneBefore({value:i}):t.value=i}}};y.postcss=!0,y._labToSRgb=t,y._lchToSRgb=e;export{y as default};
@@ -0,0 +1,3 @@
1
+ import type { FunctionNode } from 'postcss-value-parser';
2
+ declare function onCSSFunction(node: FunctionNode): void;
3
+ export default onCSSFunction;
package/package.json CHANGED
@@ -1,77 +1,41 @@
1
1
  {
2
2
  "name": "postcss-lab-function",
3
- "version": "3.1.2",
3
+ "version": "4.0.3",
4
4
  "description": "Use lab() and lch() color functions in CSS",
5
5
  "author": "Jonathan Neal <jonathantneal@hotmail.com>",
6
6
  "license": "CC0-1.0",
7
- "repository": "csstools/postcss-lab-function",
8
- "homepage": "https://github.com/csstools/postcss-lab-function#readme",
9
- "bugs": "https://github.com/csstools/postcss-lab-function/issues",
10
- "main": "dist/index.cjs.js",
11
- "module": "dist/index.esm.mjs",
7
+ "homepage": "https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-lab-function#readme",
8
+ "bugs": "https://github.com/csstools/postcss-plugins/issues",
9
+ "main": "dist/index.cjs",
10
+ "module": "dist/index.mjs",
11
+ "types": "./dist/index.d.ts",
12
12
  "files": [
13
+ "CHANGELOG.md",
14
+ "LICENSE.md",
15
+ "README.md",
13
16
  "dist"
14
17
  ],
15
18
  "scripts": {
16
- "build": "npx rollup -c .rollup.js",
17
- "build:watch": "npx rollup -c .rollup.js --watch",
18
- "lint": "npx eslint --cache src",
19
- "lint:fix": "npx eslint --cache --fix",
20
- "pretest": "npm install && npm run build",
21
- "test": "npm run lint && npm run tape",
22
- "tape": "npx postcss-tape",
23
- "prepublishOnly": "npm test"
19
+ "build": "rollup -c ../../rollup/default.js",
20
+ "clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true });\"",
21
+ "lint": "eslint ./src --ext .js --ext .ts --ext .mjs --no-error-on-unmatched-pattern",
22
+ "prepublishOnly": "npm run clean && npm run build && npm run test",
23
+ "stryker": "stryker run --logLevel error",
24
+ "test": "node ./test/color/test.mjs && postcss-tape --ci && npm run test:exports",
25
+ "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs"
24
26
  },
25
27
  "engines": {
26
- "node": ">=10.0.0"
28
+ "node": "^12 || ^14 || >=16"
27
29
  },
28
30
  "dependencies": {
29
- "@csstools/convert-colors": "^2.0.0",
30
- "postcss": "^7.0.27",
31
- "postcss-values-parser": "^3.2.0"
31
+ "postcss-value-parser": "^4.2.0"
32
32
  },
33
33
  "devDependencies": {
34
- "@babel/core": "^7.9.0",
35
- "@babel/preset-env": "^7.9.5",
36
- "babel-eslint": "^10.1.0",
37
- "eslint": "^6.8.0",
38
- "postcss-tape": "^5.0.2",
39
- "rollup": "^2.7.2",
40
- "rollup-plugin-babel": "^4.4.0"
34
+ "postcss": "^8.3.6",
35
+ "postcss-tape": "^6.0.1"
41
36
  },
42
- "babel": {
43
- "presets": [
44
- [
45
- "@babel/env",
46
- {
47
- "targets": "maintained node versions"
48
- }
49
- ]
50
- ]
51
- },
52
- "eslintConfig": {
53
- "env": {
54
- "es6": true,
55
- "node": true
56
- },
57
- "extends": "eslint:recommended",
58
- "parser": "babel-eslint"
59
- },
60
- "rollup": {
61
- "input": "src/index.js",
62
- "plugins": [
63
- "rollup-plugin-babel"
64
- ],
65
- "output": [
66
- {
67
- "file": "dist/index.cjs.js",
68
- "format": "cjs"
69
- },
70
- {
71
- "file": "dist/index.esm.js",
72
- "format": "esm"
73
- }
74
- ]
37
+ "peerDependencies": {
38
+ "postcss": "^8.3"
75
39
  },
76
40
  "keywords": [
77
41
  "postcss",
@@ -90,5 +54,13 @@
90
54
  "syntax",
91
55
  "space",
92
56
  "comma"
93
- ]
57
+ ],
58
+ "repository": {
59
+ "type": "git",
60
+ "url": "https://github.com/csstools/postcss-plugins.git",
61
+ "directory": "plugins/postcss-lab-function"
62
+ },
63
+ "volta": {
64
+ "extends": "../../package.json"
65
+ }
94
66
  }
package/dist/index.cjs.js DELETED
@@ -1,195 +0,0 @@
1
- 'use strict';
2
-
3
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
-
5
- var postcss = _interopDefault(require('postcss'));
6
- var postcssValuesParser = require('postcss-values-parser');
7
- var convertColors = require('@csstools/convert-colors');
8
-
9
- /** @type {(decl: CSSFunction) => void} Transform lab() and lch() functions. */
10
-
11
- const onCSSFunction = node => {
12
- /** @type {{ name: string, nodes: CSSNode[] }} */
13
- const {
14
- name,
15
- nodes
16
- } = node;
17
-
18
- if (isColorFunctionName(name)) {
19
- if (isLabFunctionContents(nodes) || isLchFunctionContents(nodes)) {
20
- // rename the Color function to `rgb`
21
- Object.assign(node, {
22
- name: 'rgb'
23
- });
24
- /** @type {CSSPunctuation} Slash punctuation before the Alpha channel. */
25
-
26
- const slashNode = nodes[3];
27
- /** @type {CSSNumber} Alpha channel. */
28
-
29
- const alphaNode = nodes[4];
30
-
31
- if (alphaNode) {
32
- if (isPercentage(alphaNode) && !isCalc(alphaNode)) {
33
- // transform the Alpha channel from a Percentage to (0-1) Number
34
- Object.assign(alphaNode, {
35
- value: String(alphaNode.value / 100),
36
- unit: ''
37
- });
38
- } // if the color is fully opaque (i.e. the Alpha channel is `1`)
39
-
40
-
41
- if (alphaNode.value === '1') {
42
- // remove the Slash and Alpha channel
43
- slashNode.remove();
44
- alphaNode.remove();
45
- } else {
46
- // otherwise, rename the Color function to `rgba`
47
- Object.assign(node, {
48
- name: 'rgba'
49
- });
50
- }
51
- } // replace a remaining Slash with a Comma
52
-
53
-
54
- if (slashNode && isSlash(slashNode)) {
55
- slashNode.replaceWith(commaNode.clone());
56
- }
57
- /** Extracted Color channels. */
58
-
59
-
60
- const [channelNode1, channelNode2, channelNode3] = nodes;
61
- /** Corresponding Color transformer. */
62
-
63
- const toRGB = isLabColorFunctionName(name) ? convertColors.lab2rgb : convertColors.lch2rgb;
64
- /** RGB channels from the source color. */
65
-
66
- const rgbValues = toRGB(...[channelNode1.value, channelNode2.value, channelNode3.value].map(channelNumber => parseFloat(channelNumber))).map(channelValue => Math.max(Math.min(parseInt(channelValue * 2.55), 255), 0));
67
- channelNode3.replaceWith(channelNode3.clone({
68
- value: String(rgbValues[2])
69
- }));
70
- channelNode2.replaceWith(channelNode2.clone({
71
- value: String(rgbValues[1])
72
- }), commaNode.clone());
73
- channelNode1.replaceWith(channelNode1.clone({
74
- value: String(rgbValues[0]),
75
- unit: ''
76
- }), commaNode.clone());
77
- }
78
- }
79
- };
80
- const commaNode = postcssValuesParser.parse(',').first;
81
- /** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */
82
-
83
- const createRegExpTest = Function.bind.bind(RegExp.prototype.test);
84
- /** Return whether the function name is `lab` or `lch`. */
85
-
86
- const isColorFunctionName = createRegExpTest(/^(lab|lch)$/i);
87
- /** Return whether the function name is `calc`. */
88
-
89
- const isCalcFunctionName = createRegExpTest(/^calc$/i);
90
- /** Return whether the function name is `lab`. */
91
-
92
- const isLabColorFunctionName = createRegExpTest(/^lab$/i);
93
- /** Return whether the unit is alpha-like. */
94
-
95
- const isAlphaLikeUnit = createRegExpTest(/^%?$/i);
96
- /** Return whether the unit is hue-like. */
97
-
98
- const isHueLikeUnit = createRegExpTest(/^(deg|grad|rad|turn)?$/i);
99
- /** @type {(node: CSSFunction) => boolean} Return whether the node is an Alpha-like unit. */
100
-
101
- const isAlphaValue = node => isCalc(node) || node.type === 'numeric' && isAlphaLikeUnit(node.unit);
102
- /** @type {(node: CSSFunction) => boolean} Return whether the node is a calc() function. */
103
-
104
-
105
- const isCalc = node => node.type === 'func' && isCalcFunctionName(node.name);
106
- /** @type {(node: CSSNumber) => boolean} Return whether the node is a Hue-like unit. */
107
-
108
-
109
- const isHue = node => isCalc(node) || node.type === 'numeric' && isHueLikeUnit(node.unit);
110
- /** @type {(node: CSSNumber) => boolean} Return whether the node is a Number unit. */
111
-
112
-
113
- const isNumber = node => isCalc(node) || node.type === 'numeric' && node.unit === '';
114
- /** @type {(node: CSSNumber) => boolean} Return whether the node is a Percentage unit. */
115
-
116
-
117
- const isPercentage = node => isCalc(node) || node.type === 'numeric' && node.unit === '%';
118
- /** @type {(node: CSSOperator) => boolean} Return whether the node is a Slash delimiter. */
119
-
120
-
121
- const isSlash = node => node.type === 'operator' && node.value === '/';
122
- /** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lab() function. */
123
-
124
-
125
- const isLabFunctionContents = nodes => nodes.every((node, index) => typeof labFunctionContents[index] === 'function' && labFunctionContents[index](node));
126
- /** Set of nodes in a lab() function. */
127
-
128
-
129
- const labFunctionContents = [isPercentage, isNumber, isNumber, isSlash, isAlphaValue];
130
- /** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lch() function. */
131
-
132
- const isLchFunctionContents = nodes => nodes.every((node, index) => typeof lchFunctionContents[index] === 'function' && lchFunctionContents[index](node));
133
- /** Set of nodes in a lch() function. */
134
-
135
-
136
- const lchFunctionContents = [isPercentage, isNumber, isHue, isSlash, isAlphaValue];
137
- /** @typedef {import('postcss-values-parser').Func} CSSFunction */
138
-
139
- /** @typedef {import('postcss-values-parser').Node} CSSNode */
140
-
141
- /** @typedef {import('postcss-values-parser').Numeric} CSSNumber */
142
-
143
- /** @typedef {import('postcss-values-parser').Operator} CSSOperator */
144
-
145
- /** @typedef {import('postcss-values-parser').Punctuation} CSSPunctuation */
146
-
147
- var options = {
148
- /** Whether to preserve the original functional color declaration. */
149
- preserve: false
150
- };
151
-
152
- /** @type {(decl: CSSDeclaration) => void} Transform lab() and lch() functions in CSS Declarations. */
153
-
154
- const onCSSDeclaration = decl => {
155
- const {
156
- value: originalValue
157
- } = decl;
158
-
159
- if (hasAnyColorFunction(originalValue)) {
160
- const valueAST = postcssValuesParser.parse(originalValue);
161
- valueAST.walkType('func', onCSSFunction);
162
- const modifiedValue = String(valueAST);
163
-
164
- if (modifiedValue !== originalValue) {
165
- if (options.preserve) decl.cloneBefore({
166
- value: modifiedValue
167
- });else decl.value = modifiedValue;
168
- }
169
- }
170
- };
171
- /** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */
172
-
173
- const createRegExpTest$1 = Function.bind.bind(RegExp.prototype.test);
174
- /** Return whether the value has a lab() or lch() function. */
175
-
176
- const hasAnyColorFunction = createRegExpTest$1(/(^|[^\w-])(lab|lch)\(/i);
177
- /** @typedef {import('postcss').Declaration} CSSDeclaration */
178
-
179
- /** Transform lab() and lch() functions in CSS. */
180
-
181
- const postcssPlugin = postcss.plugin('postcss-lab-function',
182
- /** @type {PostCSSPluginInitializer} */
183
- opts => {
184
- options.preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;
185
- return root => {
186
- root.walkDecls(onCSSDeclaration);
187
- };
188
- });
189
- /** @typedef {import('postcss').Root} CSSRoot */
190
-
191
- /** @typedef {(root: CSSRoot) => void} PostCSSTransformCallback */
192
-
193
- /** @typedef {(opts: options) => PostCSSTransformCallback} PostCSSPluginInitializer */
194
-
195
- module.exports = postcssPlugin;
package/dist/index.esm.js DELETED
@@ -1,191 +0,0 @@
1
- import postcss from 'postcss';
2
- import { parse } from 'postcss-values-parser';
3
- import { lab2rgb, lch2rgb } from '@csstools/convert-colors';
4
-
5
- /** @type {(decl: CSSFunction) => void} Transform lab() and lch() functions. */
6
-
7
- const onCSSFunction = node => {
8
- /** @type {{ name: string, nodes: CSSNode[] }} */
9
- const {
10
- name,
11
- nodes
12
- } = node;
13
-
14
- if (isColorFunctionName(name)) {
15
- if (isLabFunctionContents(nodes) || isLchFunctionContents(nodes)) {
16
- // rename the Color function to `rgb`
17
- Object.assign(node, {
18
- name: 'rgb'
19
- });
20
- /** @type {CSSPunctuation} Slash punctuation before the Alpha channel. */
21
-
22
- const slashNode = nodes[3];
23
- /** @type {CSSNumber} Alpha channel. */
24
-
25
- const alphaNode = nodes[4];
26
-
27
- if (alphaNode) {
28
- if (isPercentage(alphaNode) && !isCalc(alphaNode)) {
29
- // transform the Alpha channel from a Percentage to (0-1) Number
30
- Object.assign(alphaNode, {
31
- value: String(alphaNode.value / 100),
32
- unit: ''
33
- });
34
- } // if the color is fully opaque (i.e. the Alpha channel is `1`)
35
-
36
-
37
- if (alphaNode.value === '1') {
38
- // remove the Slash and Alpha channel
39
- slashNode.remove();
40
- alphaNode.remove();
41
- } else {
42
- // otherwise, rename the Color function to `rgba`
43
- Object.assign(node, {
44
- name: 'rgba'
45
- });
46
- }
47
- } // replace a remaining Slash with a Comma
48
-
49
-
50
- if (slashNode && isSlash(slashNode)) {
51
- slashNode.replaceWith(commaNode.clone());
52
- }
53
- /** Extracted Color channels. */
54
-
55
-
56
- const [channelNode1, channelNode2, channelNode3] = nodes;
57
- /** Corresponding Color transformer. */
58
-
59
- const toRGB = isLabColorFunctionName(name) ? lab2rgb : lch2rgb;
60
- /** RGB channels from the source color. */
61
-
62
- const rgbValues = toRGB(...[channelNode1.value, channelNode2.value, channelNode3.value].map(channelNumber => parseFloat(channelNumber))).map(channelValue => Math.max(Math.min(parseInt(channelValue * 2.55), 255), 0));
63
- channelNode3.replaceWith(channelNode3.clone({
64
- value: String(rgbValues[2])
65
- }));
66
- channelNode2.replaceWith(channelNode2.clone({
67
- value: String(rgbValues[1])
68
- }), commaNode.clone());
69
- channelNode1.replaceWith(channelNode1.clone({
70
- value: String(rgbValues[0]),
71
- unit: ''
72
- }), commaNode.clone());
73
- }
74
- }
75
- };
76
- const commaNode = parse(',').first;
77
- /** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */
78
-
79
- const createRegExpTest = Function.bind.bind(RegExp.prototype.test);
80
- /** Return whether the function name is `lab` or `lch`. */
81
-
82
- const isColorFunctionName = createRegExpTest(/^(lab|lch)$/i);
83
- /** Return whether the function name is `calc`. */
84
-
85
- const isCalcFunctionName = createRegExpTest(/^calc$/i);
86
- /** Return whether the function name is `lab`. */
87
-
88
- const isLabColorFunctionName = createRegExpTest(/^lab$/i);
89
- /** Return whether the unit is alpha-like. */
90
-
91
- const isAlphaLikeUnit = createRegExpTest(/^%?$/i);
92
- /** Return whether the unit is hue-like. */
93
-
94
- const isHueLikeUnit = createRegExpTest(/^(deg|grad|rad|turn)?$/i);
95
- /** @type {(node: CSSFunction) => boolean} Return whether the node is an Alpha-like unit. */
96
-
97
- const isAlphaValue = node => isCalc(node) || node.type === 'numeric' && isAlphaLikeUnit(node.unit);
98
- /** @type {(node: CSSFunction) => boolean} Return whether the node is a calc() function. */
99
-
100
-
101
- const isCalc = node => node.type === 'func' && isCalcFunctionName(node.name);
102
- /** @type {(node: CSSNumber) => boolean} Return whether the node is a Hue-like unit. */
103
-
104
-
105
- const isHue = node => isCalc(node) || node.type === 'numeric' && isHueLikeUnit(node.unit);
106
- /** @type {(node: CSSNumber) => boolean} Return whether the node is a Number unit. */
107
-
108
-
109
- const isNumber = node => isCalc(node) || node.type === 'numeric' && node.unit === '';
110
- /** @type {(node: CSSNumber) => boolean} Return whether the node is a Percentage unit. */
111
-
112
-
113
- const isPercentage = node => isCalc(node) || node.type === 'numeric' && node.unit === '%';
114
- /** @type {(node: CSSOperator) => boolean} Return whether the node is a Slash delimiter. */
115
-
116
-
117
- const isSlash = node => node.type === 'operator' && node.value === '/';
118
- /** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lab() function. */
119
-
120
-
121
- const isLabFunctionContents = nodes => nodes.every((node, index) => typeof labFunctionContents[index] === 'function' && labFunctionContents[index](node));
122
- /** Set of nodes in a lab() function. */
123
-
124
-
125
- const labFunctionContents = [isPercentage, isNumber, isNumber, isSlash, isAlphaValue];
126
- /** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lch() function. */
127
-
128
- const isLchFunctionContents = nodes => nodes.every((node, index) => typeof lchFunctionContents[index] === 'function' && lchFunctionContents[index](node));
129
- /** Set of nodes in a lch() function. */
130
-
131
-
132
- const lchFunctionContents = [isPercentage, isNumber, isHue, isSlash, isAlphaValue];
133
- /** @typedef {import('postcss-values-parser').Func} CSSFunction */
134
-
135
- /** @typedef {import('postcss-values-parser').Node} CSSNode */
136
-
137
- /** @typedef {import('postcss-values-parser').Numeric} CSSNumber */
138
-
139
- /** @typedef {import('postcss-values-parser').Operator} CSSOperator */
140
-
141
- /** @typedef {import('postcss-values-parser').Punctuation} CSSPunctuation */
142
-
143
- var options = {
144
- /** Whether to preserve the original functional color declaration. */
145
- preserve: false
146
- };
147
-
148
- /** @type {(decl: CSSDeclaration) => void} Transform lab() and lch() functions in CSS Declarations. */
149
-
150
- const onCSSDeclaration = decl => {
151
- const {
152
- value: originalValue
153
- } = decl;
154
-
155
- if (hasAnyColorFunction(originalValue)) {
156
- const valueAST = parse(originalValue);
157
- valueAST.walkType('func', onCSSFunction);
158
- const modifiedValue = String(valueAST);
159
-
160
- if (modifiedValue !== originalValue) {
161
- if (options.preserve) decl.cloneBefore({
162
- value: modifiedValue
163
- });else decl.value = modifiedValue;
164
- }
165
- }
166
- };
167
- /** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */
168
-
169
- const createRegExpTest$1 = Function.bind.bind(RegExp.prototype.test);
170
- /** Return whether the value has a lab() or lch() function. */
171
-
172
- const hasAnyColorFunction = createRegExpTest$1(/(^|[^\w-])(lab|lch)\(/i);
173
- /** @typedef {import('postcss').Declaration} CSSDeclaration */
174
-
175
- /** Transform lab() and lch() functions in CSS. */
176
-
177
- const postcssPlugin = postcss.plugin('postcss-lab-function',
178
- /** @type {PostCSSPluginInitializer} */
179
- opts => {
180
- options.preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;
181
- return root => {
182
- root.walkDecls(onCSSDeclaration);
183
- };
184
- });
185
- /** @typedef {import('postcss').Root} CSSRoot */
186
-
187
- /** @typedef {(root: CSSRoot) => void} PostCSSTransformCallback */
188
-
189
- /** @typedef {(opts: options) => PostCSSTransformCallback} PostCSSPluginInitializer */
190
-
191
- export default postcssPlugin;