@csstools/postcss-alpha-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 +9 -0
- package/LICENSE.md +18 -0
- package/README.md +135 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.mjs +1 -0
- package/package.json +71 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Changes to PostCSS Alpha Function
|
|
2
|
+
|
|
3
|
+
### 1.0.0
|
|
4
|
+
|
|
5
|
+
_August 22, 2025_
|
|
6
|
+
|
|
7
|
+
- Initial version
|
|
8
|
+
- Updated [`@csstools/css-color-parser`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-color-parser) to [`3.1.0`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-color-parser/CHANGELOG.md#310) (minor)
|
|
9
|
+
- Updated [`@csstools/postcss-progressive-custom-properties`](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-progressive-custom-properties) to [`4.2.0`](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-progressive-custom-properties/CHANGELOG.md#420) (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,135 @@
|
|
|
1
|
+
# PostCSS Alpha Function [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][PostCSS]
|
|
2
|
+
|
|
3
|
+
`npm install @csstools/postcss-alpha-function --save-dev`
|
|
4
|
+
|
|
5
|
+
[PostCSS Alpha Function] lets you use the `alpha` function in
|
|
6
|
+
CSS, following the [CSS Color] specification.
|
|
7
|
+
|
|
8
|
+
```css
|
|
9
|
+
.color {
|
|
10
|
+
color: alpha(from #dddd / calc(alpha / 2));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
:root {
|
|
14
|
+
--a-color: alpha(from rgb(2 1 0 / var(--a)) / calc(alpha / 2));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/* becomes */
|
|
18
|
+
|
|
19
|
+
.color {
|
|
20
|
+
color: rgb(from #dddd r g b / calc(alpha / 2));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
:root {
|
|
24
|
+
--a-color: rgb(from rgb(2 1 0 / var(--a)) r g b / calc(alpha / 2));
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
Add [PostCSS Alpha Function] to your project:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install postcss @csstools/postcss-alpha-function --save-dev
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Use it as a [PostCSS] plugin:
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
const postcss = require('postcss');
|
|
40
|
+
const postcssAlphaFunction = require('@csstools/postcss-alpha-function');
|
|
41
|
+
|
|
42
|
+
postcss([
|
|
43
|
+
postcssAlphaFunction(/* pluginOptions */)
|
|
44
|
+
]).process(YOUR_CSS /*, processOptions */);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
## Options
|
|
50
|
+
|
|
51
|
+
### preserve
|
|
52
|
+
|
|
53
|
+
The `preserve` option determines whether the original notation
|
|
54
|
+
is preserved. By default, it is not preserved.
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
postcssAlphaFunction({ preserve: true })
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```css
|
|
61
|
+
.color {
|
|
62
|
+
color: alpha(from #dddd / calc(alpha / 2));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
:root {
|
|
66
|
+
--a-color: alpha(from rgb(2 1 0 / var(--a)) / calc(alpha / 2));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/* becomes */
|
|
70
|
+
|
|
71
|
+
.color {
|
|
72
|
+
color: rgb(from #dddd r g b / calc(alpha / 2));
|
|
73
|
+
color: alpha(from #dddd / calc(alpha / 2));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
:root {
|
|
77
|
+
--a-color: rgb(from rgb(2 1 0 / var(--a)) r g b / calc(alpha / 2));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@supports (color: alpha(from red / 1)) and (color: rgb(0 0 0 / 0)) {
|
|
81
|
+
:root {
|
|
82
|
+
--a-color: alpha(from rgb(2 1 0 / var(--a)) / calc(alpha / 2));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### enableProgressiveCustomProperties
|
|
88
|
+
|
|
89
|
+
The `enableProgressiveCustomProperties` option determines whether the original notation
|
|
90
|
+
is wrapped with `@supports` when used in Custom Properties. By default, it is enabled.
|
|
91
|
+
|
|
92
|
+
> [!NOTE]
|
|
93
|
+
> We only recommend disabling this when you set `preserve` to `false` or if you bring your own fix for Custom Properties.
|
|
94
|
+
> See what the plugin does in its [README](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-progressive-custom-properties#readme).
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
postcssAlphaFunction({ enableProgressiveCustomProperties: false })
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```css
|
|
101
|
+
.color {
|
|
102
|
+
color: alpha(from #dddd / calc(alpha / 2));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
:root {
|
|
106
|
+
--a-color: alpha(from rgb(2 1 0 / var(--a)) / calc(alpha / 2));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/* becomes */
|
|
110
|
+
|
|
111
|
+
.color {
|
|
112
|
+
color: rgb(from #dddd r g b / calc(alpha / 2));
|
|
113
|
+
color: alpha(from #dddd / calc(alpha / 2));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
:root {
|
|
117
|
+
--a-color: rgb(from rgb(2 1 0 / var(--a)) r g b / calc(alpha / 2));
|
|
118
|
+
--a-color: alpha(from rgb(2 1 0 / var(--a)) / calc(alpha / 2));
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
_Custom properties do not fallback to the previous declaration_
|
|
123
|
+
|
|
124
|
+
## Copyright : color conversions
|
|
125
|
+
|
|
126
|
+
This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/tree/main/css-color-4. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
|
|
127
|
+
|
|
128
|
+
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|
129
|
+
[css-url]: https://cssdb.org/#alpha-function
|
|
130
|
+
[discord]: https://discord.gg/bUadyRwkJS
|
|
131
|
+
[npm-url]: https://www.npmjs.com/package/@csstools/postcss-alpha-function
|
|
132
|
+
|
|
133
|
+
[PostCSS]: https://github.com/postcss/postcss
|
|
134
|
+
[PostCSS Alpha Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-alpha-function
|
|
135
|
+
[CSS Color]: https://drafts.csswg.org/css-color-5/#relative-alpha
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("@csstools/postcss-progressive-custom-properties"),o=require("@csstools/css-tokenizer"),s=require("@csstools/css-color-parser"),t=require("@csstools/utilities"),r=require("@csstools/css-parser-algorithms");const n=/\balpha\(/i,a=/^alpha$/i,l=new Set(["srgb","srgb-linear","display-p3","display-p3-linear","a98-rgb","prophoto-rgb","rec2020","xyz","xyz-d50","xyz-d65"]),basePlugin=e=>({postcssPlugin:"postcss-color-function",Declaration(i){const c=i.value;if(!n.test(c))return;if(t.hasFallback(i))return;if(t.hasSupportsAtRuleAncestor(i,n))return;const v=o.tokenize({css:c}),u=r.replaceComponentValues(r.parseCommaSeparatedListOfComponentValues(v),e=>{if(r.isFunctionNode(e)&&a.test(e.getName()))for(let t=0;t<e.value.length;t++){let n=e.value[t];for(;r.isWhiteSpaceOrCommentNode(n);)t++,n=e.value[t];for(r.isTokenNode(n)&&o.isTokenIdent(n.value)&&"from"===n.value[4].value.toLowerCase()&&(t++,n=e.value[t]);r.isWhiteSpaceOrCommentNode(n);)t++,n=e.value[t];const a=n;if(r.isFunctionNode(a)){const s=a.getName().toLowerCase();if("var"===s)return;if("color"===s){let s="";for(let e=0;e<a.value.length;e++){const t=a.value[e];if(r.isTokenNode(t)&&o.isTokenIdent(t.value)&&l.has(t.value[4].value.toLowerCase())){if(s)return;s=t.value[4].value}}if(!s)return;e.name[1]="color(",e.name[4].value="color";let n=["r","g","b"];return"xyz"!==s&&"xyz-d50"!==s&&"xyz-d65"!==s||(n=["x","y","z"]),void e.value.splice(t+1,0,new r.WhitespaceNode([[o.TokenType.Whitespace," ",-1,-1,void 0]]),new r.TokenNode([o.TokenType.Ident,s,-1,-1,{value:s}]),new r.WhitespaceNode([[o.TokenType.Whitespace," ",-1,-1,void 0]]),new r.TokenNode([o.TokenType.Ident,n[0],-1,-1,{value:n[0]}]),new r.WhitespaceNode([[o.TokenType.Whitespace," ",-1,-1,void 0]]),new r.TokenNode([o.TokenType.Ident,n[1],-1,-1,{value:n[1]}]),new r.WhitespaceNode([[o.TokenType.Whitespace," ",-1,-1,void 0]]),new r.TokenNode([o.TokenType.Ident,n[2],-1,-1,{value:n[2]}]))}switch(s){case"rgb":case"rgba":return void convertToRelativeColor(e,t+1,"rgb",["r","g","b"]);case"hsl":case"hsla":return void convertToRelativeColor(e,t+1,"hsl",["h","s","l"]);case"hwb":return void convertToRelativeColor(e,t+1,"hwb",["h","w","b"]);case"lab":return void convertToRelativeColor(e,t+1,"lab",["l","a","b"]);case"lch":return void convertToRelativeColor(e,t+1,"lch",["l","c","h"]);case"oklab":return void convertToRelativeColor(e,t+1,"oklab",["l","a","b"]);case"oklch":return void convertToRelativeColor(e,t+1,"oklch",["l","c","h"])}}if(r.isTokenNode(a)&&(o.isTokenHash(a.value)||o.isTokenIdent(a.value))){const o=s.color(a);if(!o)return;if(!o.syntaxFlags.has(s.SyntaxFlag.Hex)&&!o.syntaxFlags.has(s.SyntaxFlag.ColorKeyword))return;return void convertToRelativeColor(e,t+1,"rgb",["r","g","b"])}}}),p=r.stringify(u);p!==c&&(i.cloneBefore({value:p}),e?.preserve||i.remove())}});function convertToRelativeColor(e,s,t,n){e.name[1]=t+"(",e.name[4].value=t,e.value.splice(s,0,new r.WhitespaceNode([[o.TokenType.Whitespace," ",-1,-1,void 0]]),new r.TokenNode([o.TokenType.Ident,n[0],-1,-1,{value:n[0]}]),new r.WhitespaceNode([[o.TokenType.Whitespace," ",-1,-1,void 0]]),new r.TokenNode([o.TokenType.Ident,n[1],-1,-1,{value:n[1]}]),new r.WhitespaceNode([[o.TokenType.Whitespace," ",-1,-1,void 0]]),new r.TokenNode([o.TokenType.Ident,n[2],-1,-1,{value:n[2]}]))}basePlugin.postcss=!0;const postcssPlugin=o=>{const s=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},o);return s.enableProgressiveCustomProperties&&s.preserve?{postcssPlugin:"postcss-color-function",plugins:[e(),basePlugin(s)]}:basePlugin(s)};postcssPlugin.postcss=!0,module.exports=postcssPlugin;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PluginCreator } from 'postcss';
|
|
2
|
+
|
|
3
|
+
/** postcss-color-function plugin options */
|
|
4
|
+
export declare type pluginOptions = {
|
|
5
|
+
/** Preserve the original notation. default: false */
|
|
6
|
+
preserve?: boolean;
|
|
7
|
+
/** Enable "@csstools/postcss-progressive-custom-properties". default: true */
|
|
8
|
+
enableProgressiveCustomProperties?: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/** Transform the color() function in CSS. */
|
|
12
|
+
declare const postcssPlugin: PluginCreator<pluginOptions>;
|
|
13
|
+
export default postcssPlugin;
|
|
14
|
+
|
|
15
|
+
export { }
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import e from"@csstools/postcss-progressive-custom-properties";import{tokenize as o,isTokenIdent as r,TokenType as s,isTokenHash as t}from"@csstools/css-tokenizer";import{color as l,SyntaxFlag as a}from"@csstools/css-color-parser";import{hasFallback as n,hasSupportsAtRuleAncestor as i}from"@csstools/utilities";import{replaceComponentValues as c,parseCommaSeparatedListOfComponentValues as v,isFunctionNode as u,isWhiteSpaceOrCommentNode as p,isTokenNode as d,WhitespaceNode as h,TokenNode as f,stringify as g}from"@csstools/css-parser-algorithms";const b=/\balpha\(/i,w=/^alpha$/i,m=new Set(["srgb","srgb-linear","display-p3","display-p3-linear","a98-rgb","prophoto-rgb","rec2020","xyz","xyz-d50","xyz-d65"]),basePlugin=e=>({postcssPlugin:"postcss-color-function",Declaration(C){const y=C.value;if(!b.test(y))return;if(n(C))return;if(i(C,b))return;const x=o({css:y}),R=c(v(x),e=>{if(u(e)&&w.test(e.getName()))for(let o=0;o<e.value.length;o++){let n=e.value[o];for(;p(n);)o++,n=e.value[o];for(d(n)&&r(n.value)&&"from"===n.value[4].value.toLowerCase()&&(o++,n=e.value[o]);p(n);)o++,n=e.value[o];const i=n;if(u(i)){const t=i.getName().toLowerCase();if("var"===t)return;if("color"===t){let t="";for(let e=0;e<i.value.length;e++){const o=i.value[e];if(d(o)&&r(o.value)&&m.has(o.value[4].value.toLowerCase())){if(t)return;t=o.value[4].value}}if(!t)return;e.name[1]="color(",e.name[4].value="color";let l=["r","g","b"];return"xyz"!==t&&"xyz-d50"!==t&&"xyz-d65"!==t||(l=["x","y","z"]),void e.value.splice(o+1,0,new h([[s.Whitespace," ",-1,-1,void 0]]),new f([s.Ident,t,-1,-1,{value:t}]),new h([[s.Whitespace," ",-1,-1,void 0]]),new f([s.Ident,l[0],-1,-1,{value:l[0]}]),new h([[s.Whitespace," ",-1,-1,void 0]]),new f([s.Ident,l[1],-1,-1,{value:l[1]}]),new h([[s.Whitespace," ",-1,-1,void 0]]),new f([s.Ident,l[2],-1,-1,{value:l[2]}]))}switch(t){case"rgb":case"rgba":return void convertToRelativeColor(e,o+1,"rgb",["r","g","b"]);case"hsl":case"hsla":return void convertToRelativeColor(e,o+1,"hsl",["h","s","l"]);case"hwb":return void convertToRelativeColor(e,o+1,"hwb",["h","w","b"]);case"lab":return void convertToRelativeColor(e,o+1,"lab",["l","a","b"]);case"lch":return void convertToRelativeColor(e,o+1,"lch",["l","c","h"]);case"oklab":return void convertToRelativeColor(e,o+1,"oklab",["l","a","b"]);case"oklch":return void convertToRelativeColor(e,o+1,"oklch",["l","c","h"])}}if(d(i)&&(t(i.value)||r(i.value))){const r=l(i);if(!r)return;if(!r.syntaxFlags.has(a.Hex)&&!r.syntaxFlags.has(a.ColorKeyword))return;return void convertToRelativeColor(e,o+1,"rgb",["r","g","b"])}}}),T=g(R);T!==y&&(C.cloneBefore({value:T}),e?.preserve||C.remove())}});function convertToRelativeColor(e,o,r,t){e.name[1]=r+"(",e.name[4].value=r,e.value.splice(o,0,new h([[s.Whitespace," ",-1,-1,void 0]]),new f([s.Ident,t[0],-1,-1,{value:t[0]}]),new h([[s.Whitespace," ",-1,-1,void 0]]),new f([s.Ident,t[1],-1,-1,{value:t[1]}]),new h([[s.Whitespace," ",-1,-1,void 0]]),new f([s.Ident,t[2],-1,-1,{value:t[2]}]))}basePlugin.postcss=!0;const postcssPlugin=o=>{const r=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},o);return r.enableProgressiveCustomProperties&&r.preserve?{postcssPlugin:"postcss-color-function",plugins:[e(),basePlugin(r)]}:basePlugin(r)};postcssPlugin.postcss=!0;export{postcssPlugin as default};
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@csstools/postcss-alpha-function",
|
|
3
|
+
"description": "Use the alpha() function in CSS",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"contributors": [
|
|
6
|
+
{
|
|
7
|
+
"name": "Romain Menke",
|
|
8
|
+
"email": "romainmenke@gmail.com"
|
|
9
|
+
}
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT-0",
|
|
12
|
+
"funding": [
|
|
13
|
+
{
|
|
14
|
+
"type": "github",
|
|
15
|
+
"url": "https://github.com/sponsors/csstools"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"type": "opencollective",
|
|
19
|
+
"url": "https://opencollective.com/csstools"
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "dist/index.cjs",
|
|
27
|
+
"module": "dist/index.mjs",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"default": "./dist/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"default": "./dist/index.cjs"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"CHANGELOG.md",
|
|
41
|
+
"LICENSE.md",
|
|
42
|
+
"README.md",
|
|
43
|
+
"dist"
|
|
44
|
+
],
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@csstools/css-color-parser": "^3.1.0",
|
|
47
|
+
"@csstools/css-parser-algorithms": "^3.0.5",
|
|
48
|
+
"@csstools/css-tokenizer": "^3.0.4",
|
|
49
|
+
"@csstools/postcss-progressive-custom-properties": "^4.2.0",
|
|
50
|
+
"@csstools/utilities": "^2.0.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"postcss": "^8.4"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {},
|
|
56
|
+
"homepage": "https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-alpha-function#readme",
|
|
57
|
+
"repository": {
|
|
58
|
+
"type": "git",
|
|
59
|
+
"url": "git+https://github.com/csstools/postcss-plugins.git",
|
|
60
|
+
"directory": "plugins/postcss-alpha-function"
|
|
61
|
+
},
|
|
62
|
+
"bugs": "https://github.com/csstools/postcss-plugins/issues",
|
|
63
|
+
"keywords": [
|
|
64
|
+
"alpha",
|
|
65
|
+
"color",
|
|
66
|
+
"css",
|
|
67
|
+
"design",
|
|
68
|
+
"postcss",
|
|
69
|
+
"postcss-plugin"
|
|
70
|
+
]
|
|
71
|
+
}
|