stylelint-gamut 1.3.3 → 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/README.md +8 -9
- package/package.json +16 -15
- package/src/index.js +17 -14
- package/src/utils.js +13 -27
- package/src/README.md +0 -121
package/README.md
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
# Rules
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
- [`color-no-out-gamut-range`](./src/README.md): Throw warning if color goes out of sRGB color space and is not wrapped in `@media (color-gamut: p3) {}` or `@media (color-gamut: rec2020) {}`.
|
|
10
10
|
|
|
11
|
-
# Requirements
|
|
11
|
+
# Requirements
|
|
12
12
|
|
|
13
13
|
- **node version ≥ 12.0.0**
|
|
14
14
|
- **Stylelint version ≥ 14.0.0**.
|
|
@@ -27,28 +27,27 @@ npm install stylelint stylelint-config-standard --save-dev
|
|
|
27
27
|
npm install stylelint-gamut --save-dev
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
Add `stylelint-gamut` to your Stylelint config `plugins` array, then add rules you need to the rules list. All rules from `stylelint-gamut` need to be namespaced with `gamut`.
|
|
30
|
+
3. Add `stylelint-gamut` to your Stylelint config `plugins` array, then add rules you need to the rules list. All rules from `stylelint-gamut` need to be namespaced with `gamut`.
|
|
33
31
|
|
|
34
32
|
```json
|
|
35
33
|
{
|
|
36
|
-
"plugins": [
|
|
37
|
-
"stylelint-gamut"
|
|
38
|
-
],
|
|
34
|
+
"plugins": ["stylelint-gamut"],
|
|
39
35
|
"rules": {
|
|
40
|
-
"gamut/color-no-out-gamut-range": true
|
|
36
|
+
"gamut/color-no-out-gamut-range": true
|
|
41
37
|
}
|
|
42
38
|
}
|
|
43
39
|
```
|
|
44
40
|
|
|
45
41
|
# More
|
|
42
|
+
|
|
46
43
|
It is already used by the creator of [PostCSS] [Andrey Sitnik].
|
|
47
44
|
|
|
48
45
|
Read more about color spaces in css:
|
|
46
|
+
|
|
49
47
|
- [OKLCH in CSS: why we moved from RGB and HSL](https://evilmartians.com/chronicles/oklch-in-css-why-quit-rgb-hsl)
|
|
50
48
|
- [LCH colors in CSS: what, why, and how?](https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/)
|
|
51
49
|
- [A Guide To Modern CSS Colors](https://www.smashingmagazine.com/2021/11/guide-modern-css-colors/)
|
|
50
|
+
- [Better dynamic themes in Tailwind with OKLCH color magic](https://evilmartians.com/chronicles/better-dynamic-themes-in-tailwind-with-oklch-color-magic)
|
|
52
51
|
|
|
53
52
|
[Stylelint]: https://stylelint.io/
|
|
54
53
|
[npm-version-img]: https://img.shields.io/npm/v/stylelint-gamut.svg
|
package/package.json
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stylelint-gamut",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Stylelint plugin for working with different color spaces",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "src/index.js",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"format": "prettier --write",
|
|
8
|
-
"lint": "eslint --fix",
|
|
9
|
-
"test": "jest",
|
|
8
|
+
"format": "prettier --write .",
|
|
9
|
+
"lint": "eslint --fix *.js",
|
|
10
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
|
|
10
11
|
"prepare": "husky install"
|
|
11
12
|
},
|
|
12
13
|
"lint-staged": {
|
|
13
|
-
"*.js": "
|
|
14
|
-
".": "
|
|
14
|
+
"*.js": "eslint --fix",
|
|
15
|
+
".": "prettier -write"
|
|
15
16
|
},
|
|
16
17
|
"engines": {
|
|
17
|
-
"node": ">=
|
|
18
|
+
"node": ">=20.19.0"
|
|
18
19
|
},
|
|
19
20
|
"repository": {
|
|
20
21
|
"type": "git",
|
|
@@ -40,22 +41,22 @@
|
|
|
40
41
|
},
|
|
41
42
|
"homepage": "https://github.com/fpetrakov/stylelint-gamut#readme",
|
|
42
43
|
"peerDependencies": {
|
|
43
|
-
"stylelint": "^14.0.0 || ^15.0.0"
|
|
44
|
+
"stylelint": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
|
|
44
45
|
},
|
|
45
46
|
"devDependencies": {
|
|
46
47
|
"eslint": "^8.43.0",
|
|
47
|
-
"eslint-config-prettier": "^
|
|
48
|
+
"eslint-config-prettier": "^9.0.0",
|
|
48
49
|
"eslint-config-standard": "^17.1.0",
|
|
49
50
|
"eslint-plugin-import": "^2.27.5",
|
|
50
51
|
"eslint-plugin-n": "^16.0.0",
|
|
51
52
|
"eslint-plugin-promise": "^6.1.1",
|
|
52
53
|
"husky": "^8.0.3",
|
|
53
|
-
"jest": "^
|
|
54
|
-
"jest-cli": "^
|
|
55
|
-
"jest-preset-stylelint": "^
|
|
56
|
-
"lint-staged": "^
|
|
57
|
-
"prettier": "^
|
|
58
|
-
"stylelint": "^
|
|
54
|
+
"jest": "^30.0.0",
|
|
55
|
+
"jest-cli": "^30.0.0",
|
|
56
|
+
"jest-preset-stylelint": "^9.0.0",
|
|
57
|
+
"lint-staged": "^15.0.2",
|
|
58
|
+
"prettier": "^3.0.3",
|
|
59
|
+
"stylelint": "^17.0.0"
|
|
59
60
|
},
|
|
60
61
|
"dependencies": {
|
|
61
62
|
"colorjs.io": "^0.4.5"
|
package/src/index.js
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
import stylelint from "stylelint";
|
|
2
|
+
import Color from "colorjs.io";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
const Color = require("colorjs.io").default;
|
|
5
|
-
const {
|
|
4
|
+
import {
|
|
6
5
|
isInColorGamutP3MediaQuery,
|
|
7
6
|
isStandardSyntaxProperty,
|
|
8
7
|
declarationValueIndex,
|
|
9
8
|
isInColorGamutRec2020MediaQuery,
|
|
10
9
|
startsWithNumber,
|
|
11
|
-
}
|
|
10
|
+
} from "./utils.js";
|
|
12
11
|
|
|
13
12
|
const ruleName = "gamut/color-no-out-gamut-range";
|
|
14
13
|
|
|
15
14
|
const messages = stylelint.utils.ruleMessages(ruleName, {
|
|
16
|
-
rejected: color => `Unexpected color out of gamut range "${color}"`,
|
|
15
|
+
rejected: (color) => `Unexpected color out of gamut range "${color}"`,
|
|
17
16
|
});
|
|
18
17
|
|
|
19
18
|
const meta = {
|
|
@@ -21,7 +20,7 @@ const meta = {
|
|
|
21
20
|
};
|
|
22
21
|
|
|
23
22
|
/** @type {import('stylelint').Rule} */
|
|
24
|
-
const ruleFunction = primary => {
|
|
23
|
+
const ruleFunction = (primary) => {
|
|
25
24
|
return (root, result) => {
|
|
26
25
|
const validOptions = stylelint.utils.validateOptions(result, ruleName, {
|
|
27
26
|
actual: primary,
|
|
@@ -33,19 +32,20 @@ const ruleFunction = primary => {
|
|
|
33
32
|
|
|
34
33
|
const customProperties = {};
|
|
35
34
|
|
|
36
|
-
root.walkDecls(decl => {
|
|
35
|
+
root.walkDecls((decl) => {
|
|
37
36
|
if (!isStandardSyntaxProperty(decl.prop)) return;
|
|
38
37
|
|
|
39
38
|
const values = decl.value.match(
|
|
40
39
|
/(oklch|oklab|lab|lch|var)\([^)]+\)/g,
|
|
41
40
|
);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
check(value);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
41
|
+
|
|
42
|
+
if (!values) return;
|
|
47
43
|
|
|
48
44
|
// value is e.g. lch(48% 82 283 / 67%)
|
|
45
|
+
for (const value of values) {
|
|
46
|
+
check(value);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
49
|
function check(value) {
|
|
50
50
|
const shouldBeIgnored =
|
|
51
51
|
!value.startsWith("var(--") &&
|
|
@@ -105,4 +105,7 @@ ruleFunction.ruleName = ruleName;
|
|
|
105
105
|
ruleFunction.messages = messages;
|
|
106
106
|
ruleFunction.meta = meta;
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
const plugin = stylelint.createPlugin(ruleName, ruleFunction);
|
|
109
|
+
|
|
110
|
+
export default plugin;
|
|
111
|
+
export { ruleName, messages, meta };
|
package/src/utils.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Get the index of a declaration's value
|
|
5
3
|
*
|
|
6
4
|
* @param {import('postcss').Declaration} decl
|
|
7
5
|
* @returns {number}
|
|
8
6
|
*/
|
|
9
|
-
|
|
7
|
+
export const declarationValueIndex = (decl) => {
|
|
10
8
|
const raws = decl.raws;
|
|
11
9
|
|
|
12
10
|
return [
|
|
@@ -22,7 +20,7 @@ function declarationValueIndex(decl) {
|
|
|
22
20
|
|
|
23
21
|
return count;
|
|
24
22
|
}, 0);
|
|
25
|
-
}
|
|
23
|
+
};
|
|
26
24
|
|
|
27
25
|
/**
|
|
28
26
|
* Check whether a property is standard
|
|
@@ -30,7 +28,7 @@ function declarationValueIndex(decl) {
|
|
|
30
28
|
* @param {string} property
|
|
31
29
|
* @return {boolean} If `true`, the property is standard
|
|
32
30
|
*/
|
|
33
|
-
|
|
31
|
+
export const isStandardSyntaxProperty = (property) => {
|
|
34
32
|
// SCSS var (e.g. $var: x), list (e.g. $list: (x)) or map (e.g. $map: (key:value))
|
|
35
33
|
if (property.startsWith("$")) {
|
|
36
34
|
return false;
|
|
@@ -47,13 +45,13 @@ function isStandardSyntaxProperty(property) {
|
|
|
47
45
|
}
|
|
48
46
|
|
|
49
47
|
return true;
|
|
50
|
-
}
|
|
48
|
+
};
|
|
51
49
|
|
|
52
50
|
/**
|
|
53
51
|
* @param {import('postcss').Declaration} decl
|
|
54
52
|
* @returns {boolean}
|
|
55
53
|
*/
|
|
56
|
-
|
|
54
|
+
export const isInColorGamutP3MediaQuery = (decl) => {
|
|
57
55
|
const parentMediaQuery = getMediaQueryParent(decl);
|
|
58
56
|
|
|
59
57
|
if (
|
|
@@ -64,13 +62,13 @@ function isInColorGamutP3MediaQuery(decl) {
|
|
|
64
62
|
}
|
|
65
63
|
|
|
66
64
|
return false;
|
|
67
|
-
}
|
|
65
|
+
};
|
|
68
66
|
|
|
69
67
|
/**
|
|
70
68
|
* @param {import('postcss').Declaration} decl
|
|
71
69
|
* @returns {boolean}
|
|
72
70
|
*/
|
|
73
|
-
|
|
71
|
+
export const isInColorGamutRec2020MediaQuery = (decl) => {
|
|
74
72
|
const parentMediaQuery = getMediaQueryParent(decl);
|
|
75
73
|
|
|
76
74
|
if (
|
|
@@ -81,13 +79,13 @@ function isInColorGamutRec2020MediaQuery(decl) {
|
|
|
81
79
|
}
|
|
82
80
|
|
|
83
81
|
return false;
|
|
84
|
-
}
|
|
82
|
+
};
|
|
85
83
|
|
|
86
84
|
/**
|
|
87
85
|
* @param {import('postcss').Declaration} decl
|
|
88
86
|
* @returns {boolean | decl}
|
|
89
87
|
*/
|
|
90
|
-
|
|
88
|
+
const getMediaQueryParent = (decl) => {
|
|
91
89
|
let parent = decl.parent;
|
|
92
90
|
|
|
93
91
|
if (parent && isMediaQuery(parent)) return parent;
|
|
@@ -95,24 +93,12 @@ function getMediaQueryParent(decl) {
|
|
|
95
93
|
parent = parent.parent;
|
|
96
94
|
|
|
97
95
|
return isMediaQuery(parent) && parent;
|
|
98
|
-
}
|
|
96
|
+
};
|
|
99
97
|
|
|
100
98
|
/**
|
|
101
99
|
* @param {import('postcss').Declaration} decl
|
|
102
100
|
* @returns {boolean}
|
|
103
101
|
*/
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
function startsWithNumber(str) {
|
|
109
|
-
return /^\d/.test(str);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
module.exports = {
|
|
113
|
-
isStandardSyntaxProperty,
|
|
114
|
-
declarationValueIndex,
|
|
115
|
-
isInColorGamutP3MediaQuery,
|
|
116
|
-
isInColorGamutRec2020MediaQuery,
|
|
117
|
-
startsWithNumber,
|
|
118
|
-
};
|
|
102
|
+
const isMediaQuery = (decl) => decl.type === "atrule" && decl.name === "media";
|
|
103
|
+
|
|
104
|
+
export const startsWithNumber = (str) => /^\d/.test(str);
|
package/src/README.md
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
# color-no-out-gamut-range
|
|
2
|
-
|
|
3
|
-
Disallow out of gamut range colors.
|
|
4
|
-
|
|
5
|
-
<!-- prettier-ignore -->
|
|
6
|
-
```css
|
|
7
|
-
a { color: lch(48% 82 283) } /* This color is out of sRGB gamut range */
|
|
8
|
-
```
|
|
9
|
-
|
|
10
|
-
Colors declared using lch, oklch, lab and oklab functions can move from sRGB to P3 or Rec2020 color spaces, which are not supported by most of screens right now. It can happen by mistake. For instance, by converting colors from one space to another.
|
|
11
|
-
|
|
12
|
-
This rule checks if the color is in sRGB, p3 or rec2020 space. If it's not in sRGB gamut you should wrap it in a proper media query:
|
|
13
|
-
|
|
14
|
-
```css
|
|
15
|
-
@media (color-gamut: p3) {
|
|
16
|
-
a {
|
|
17
|
-
color: lch(29.8% 42.5 109.485);
|
|
18
|
-
/* This color is in p3 gamut range */
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
```css
|
|
24
|
-
@media (color-gamut: rec2020) {
|
|
25
|
-
a {
|
|
26
|
-
color: lch(25.1% 42.5 109.485);
|
|
27
|
-
/* This color is in rec2020 gamut range */
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
In case of **css custom properties** you should wrap either the custom property declaration:
|
|
33
|
-
|
|
34
|
-
```css
|
|
35
|
-
@media (color-gamut: p3) {
|
|
36
|
-
:root {
|
|
37
|
-
--my-var: lch(48% 82 283);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
```
|
|
41
|
-
or the rule that uses the custom property:
|
|
42
|
-
|
|
43
|
-
```css
|
|
44
|
-
@media (color-gamut: p3) {
|
|
45
|
-
a {
|
|
46
|
-
color: var(--my-var);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
Custom properties inside color declaration are ignored:
|
|
52
|
-
|
|
53
|
-
```css
|
|
54
|
-
background-color: oklch(var(--oklch-primary) / var(--alpha-bg)); /* not checked */
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
Please note that conditional imports are not checked, e.g.:
|
|
58
|
-
|
|
59
|
-
```html
|
|
60
|
-
<link href="p3-custom-props.css" rel="stylesheet" media="(color-gamut: p3)" />
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
```css
|
|
64
|
-
@import url("p3-custom-props.css") (color-gamut: p3);
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
## Options
|
|
68
|
-
|
|
69
|
-
### `true`
|
|
70
|
-
|
|
71
|
-
The following patterns are considered problems:
|
|
72
|
-
|
|
73
|
-
<!-- prettier-ignore -->
|
|
74
|
-
```css
|
|
75
|
-
a { color: lch(48% 82 283) }
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
<!-- prettier-ignore -->
|
|
79
|
-
```css
|
|
80
|
-
a { color: oklch(57.35% 0.23 259.06) }
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
<!-- prettier-ignore -->
|
|
84
|
-
```css
|
|
85
|
-
a { color: lab(82.2345% 40.1645 59.9971 / .5) }
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
<!-- prettier-ignore -->
|
|
89
|
-
```css
|
|
90
|
-
a { color: oklab(85.69% 0.1007 0.1191 / .5) }
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
The following patterns are _not_ considered problems:
|
|
94
|
-
|
|
95
|
-
<!-- prettier-ignore -->
|
|
96
|
-
```css
|
|
97
|
-
@media (color-gamut: p3) {
|
|
98
|
-
a { color: lch(48% 82 283) }
|
|
99
|
-
}
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
<!-- prettier-ignore -->
|
|
103
|
-
```css
|
|
104
|
-
@media (color-gamut: p3) {
|
|
105
|
-
a { color: oklch(57.35% 0.23 259.06) }
|
|
106
|
-
}
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
<!-- prettier-ignore -->
|
|
110
|
-
```css
|
|
111
|
-
@media (color-gamut: rec2020) {
|
|
112
|
-
a { color: lab(82.2345% 40.1645 59.9971 / .5) }
|
|
113
|
-
}
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
<!-- prettier-ignore -->
|
|
117
|
-
```css
|
|
118
|
-
@media (color-gamut: rec2020) {
|
|
119
|
-
a { color: oklab(85.69% 0.1007 0.1191 / .5) }
|
|
120
|
-
}
|
|
121
|
-
```
|