Modify colors using the color-mod() function in CSS
Installations
npm install postcss-color-mod-function
Releases
Unable to fetch releases
Developer
jonathantneal
Developer Guide
Module System
CommonJS
Min. Node Version
>= 18
Typescript Support
No
Node Version
20.17.0
NPM Version
10.8.3
Statistics
96 Stars
35 Commits
14 Forks
5 Watching
1 Branches
11 Contributors
Updated on 08 Nov 2024
Bundle Size
33.49 kB
Minified
9.92 kB
Minified + Gzipped
Languages
JavaScript (86.77%)
CSS (13.23%)
Total Downloads
Cumulative downloads
Total Downloads
846,080,006
Last day
-5.8%
290,913
Compared to previous day
Last week
1.7%
1,571,219
Compared to previous week
Last month
4.7%
6,706,423
Compared to previous month
Last year
-35.9%
88,610,404
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Peer Dependencies
1
Dev Dependencies
3
PostCSS color-mod() Function
PostCSS color-mod() Function lets you modify colors using the color-mod()
function in CSS, following the outdated version of CSS Color Module Level 4 specification (05 July 2016).
⚠️ color-mod()
has been removed from Color Module Level 4 specification. (Here's why)
1:root { 2 --brand-red: color-mod(yellow blend(red 50%)); 3 --brand-red-hsl: color-mod(yellow blend(red 50% hsl)); 4 --brand-red-hwb: color-mod(yellow blend(red 50% hwb)); 5 --brand-red-dark: color-mod(red blackness(20%)); 6} 7 8/* becomes */ 9 10:root { 11 --brand-red: rgb(255, 127.5, 0); 12 --brand-red-hsl: rgb(255, 127.5, 255); 13 --brand-red-hwb: rgb(255, 127.5, 0); 14 --brand-red-dark: rgb(204, 0, 0); 15} 16 17/* or, using stringifier(color) { return color.toString() } */ 18 19:root { 20 --brand-red: rgb(100% 50% 0% / 100%); 21 --brand-red-hsl: hsl(30 100% 50% / 100%); 22 --brand-red-hwb: hwb(30 0% 0% / 100%); 23 --brand-red-dark: hwb(0 0% 20% / 100%); 24}
Supported Colors
The color-mod()
function accepts rgb()
, legacy comma-separated rgb()
,
rgba()
, hsl()
, legacy comma-separated hsl()
, hsla()
, hwb()
, and
color-mod()
colors, as well as 3, 4, 6, and 8 digit hex colors, and named
colors without the need for additional plugins.
Implemention details are available in the specification.
Supported Color Adjusters
The color-mod()
function accepts red()
, green()
, blue()
, a()
/
alpha()
, rgb()
, h()
/ hue()
, s()
/ saturation()
, l()
/
lightness()
, w()
/ whiteness()
, b()
/ blackness()
, tint()
,
shade()
, blend()
, blenda()
, and contrast()
color adjusters.
Implemention details are available in the specification.
Supported Variables
By default, var()
variables will be used if their corresponding Custom
Properties are found in a :root
rule, or if a fallback value is specified.
Usage
Add PostCSS color-mod() Function to your project:
1npm install postcss postcss-color-mod-function --save-dev
Use PostCSS color-mod() Function to process your CSS:
1const postcssColorMod = require('postcss-color-mod-function'); 2 3postcssColorMod.process(YOUR_CSS /*, processOptions, pluginOptions */);
Or use it as a PostCSS plugin:
1const postcss = require('postcss'); 2const postcssColorMod = require('postcss-color-mod-function'); 3 4postcss([ 5 postcssColorMod(/* pluginOptions */) 6]).process(YOUR_CSS /*, processOptions */);
PostCSS color-mod() Function runs in all Node environments, with special instructions for:
Node | PostCSS CLI | Webpack | Create React App | Gulp | Grunt |
---|
Options
stringifier
The stringifier
option defines how transformed colors will be produced in CSS.
By default, legacy rgb()
and rgba()
colors are produced, but this can be
easily updated to support [CSS Color Module Level 4 colors] colors.
1import postcssColorMod from 'postcss-color-mod-function';
2
3postcssColorMod({
4 stringifier(color) {
5 return color.toString(); // use CSS Color Module Level 4 colors (rgb, hsl, hwb)
6 }
7});
Future major releases of PostCSS color-mod() Function may reverse this functionality so that CSS Color Module Level 4 colors are produced by default.
unresolved
The unresolved
option defines how unresolved functions and arguments should
be handled. The available options are throw
, warn
, and ignore
. The
default option is to throw
.
If ignore
is used, the color-mod()
function will remain unchanged.
1import postcssColorMod from 'postcss-color-mod-function';
2
3postcssColorMod({
4 unresolved: 'ignore' // ignore unresolved color-mod() functions
5});
transformVars
The transformVars
option defines whether var()
variables used within
color-mod()
should be transformed into their corresponding Custom Properties
available in :root
, or their fallback value if it is specified. By default,
var()
variables will be transformed.
However, because these transformations occur at build time, they cannot be considered accurate. Accurately resolving cascading variables relies on knowledge of the living DOM tree.
importFrom
The importFrom
option allows you to import variables from other sources,
which might be CSS, JS, and JSON files, and directly passed objects.
1postcssColorMod({
2 importFrom: 'path/to/file.css' // :root { --brand-dark: blue; --brand-main: var(--brand-dark); }
3});
1.brand-faded { 2 color: color-mod(var(--brand-main) a(50%)); 3} 4 5/* becomes */ 6 7.brand-faded { 8 color: rgba(0, 0, 255, .5); 9}
Multiple files can be passed into this option, and they will be parsed in the
order they were received. JavaScript files, JSON files, and objects will need
to namespace custom properties under a customProperties
or
custom-properties
key.
1postcssColorMod({ 2 importFrom: [ 3 'path/to/file.css', // :root { --brand-dark: blue; --brand-main: var(--brand-dark); } 4 'and/then/this.js', // module.exports = { customProperties: { '--brand-dark': 'blue', '--brand-main': 'var(--brand-dark)' } } 5 'and/then/that.json', // { "custom-properties": { "--brand-dark": "blue", "--brand-main": "var(--brand-dark)" } } 6 { 7 customProperties: { 8 '--brand-dark': 'blue', 9 '--brand-main': 'var(--brand-dark)' 10 } 11 } 12 ] 13});
Variables may reference other variables, and this plugin will attempt to
resolve them. If transformVars
is set to false
then importFrom
will not
be used.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: Creative Commons Zero v1.0 Universal: LICENSE.md:0
Reason
0 existing vulnerabilities detected
Reason
6 commit(s) and 3 issue activity found in the last 90 days -- score normalized to 7
Reason
Found 7/28 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/csstools/postcss-color-mod-function/test.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/csstools/postcss-color-mod-function/test.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:26
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 14 are checked with a SAST tool
Score
4.9
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to postcss-color-mod-function
postcss-color-function
PostCSS plugin to transform W3C CSS color function to more compatible CSS.
@csstools/postcss-color-function
Use the color() function in CSS
postcss-lab-function
Use lab() and lch() color functions in CSS
@csstools/postcss-oklab-function
Use oklab() and oklch() color functions in CSS