What is postcss-color-hex-alpha?
The postcss-color-hex-alpha package is a PostCSS plugin that allows you to use 4- and 8-digit hex color notation in your CSS. This notation includes an alpha channel, which specifies the opacity of the color. The plugin converts these hex colors into their equivalent rgba() or hsla() functions, making it easier to work with colors that include transparency.
What are postcss-color-hex-alpha's main functionalities?
Convert 4-digit hex colors to rgba()
This feature allows you to use 4-digit hex colors (e.g., #0f08) in your CSS. The plugin converts these colors into their rgba() equivalents, making it easier to work with colors that include transparency.
const postcss = require('postcss');
const colorHexAlpha = require('postcss-color-hex-alpha');
const css = 'a { color: #0f08; }';
postcss([colorHexAlpha]).process(css).then(result => {
console.log(result.css);
// Output: 'a { color: rgba(0, 255, 0, 0.53); }'
});
Convert 8-digit hex colors to rgba()
This feature allows you to use 8-digit hex colors (e.g., #0000ff80) in your CSS. The plugin converts these colors into their rgba() equivalents, making it easier to work with colors that include transparency.
const postcss = require('postcss');
const colorHexAlpha = require('postcss-color-hex-alpha');
const css = 'a { color: #0000ff80; }';
postcss([colorHexAlpha]).process(css).then(result => {
console.log(result.css);
// Output: 'a { color: rgba(0, 0, 255, 0.5); }'
});
Other packages similar to postcss-color-hex-alpha
postcss-color-function
The postcss-color-function package allows you to use color-modifying functions in your CSS, such as color(), which can adjust the lightness, saturation, and alpha of a color. Unlike postcss-color-hex-alpha, which focuses on converting hex colors with alpha channels, postcss-color-function provides a broader range of color manipulation capabilities.
postcss-color-rgba-fallback
The postcss-color-rgba-fallback package generates hexadecimal color fallbacks for rgba() colors in your CSS. This is useful for ensuring compatibility with older browsers that do not support rgba(). While postcss-color-hex-alpha converts hex colors with alpha channels to rgba(), postcss-color-rgba-fallback works in the opposite direction by providing hex fallbacks for rgba() colors.
postcss-color-hex-alpha
PostCSS plugin to transform W3C RGBA hexadecimal notations (#RRGGBBAA or #RGBA) to more compatible CSS (rgba()).
Installation
$ npm install postcss-color-hex-alpha
Usage
var fs = require("fs")
var postcss = require("postcss")
var colorHexAlpha = require("postcss-color-hex-alpha")
var css = fs.readFileSync("input.css", "utf8")
var output = postcss()
.use(colorHexAlpha())
.process(css)
.css
Using this input.css
:
body {
background: #9d9c
}
you will get:
body {
background: rgba(153, 221, 153, 0.8)
}
Checkout tests for more examples.
Contributing
Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
$ git clone https://github.com/postcss/postcss-color-hex-alpha.git
$ git checkout -b patch-1
$ npm install
$ npm test