saltcat-gradient-parser
v1.1.4
Published
Parse CSS3 gradient definitions and return an AST.
Maintainers
Readme
Saltcat Gradient Parser
About
Saltcat Gradient Parser is a fork of the original gradient-parser by Rafael Caricio, designed to support custom CSS easing extensions and enhanced CSS features.
Parse CSS3 gradient definitions and return AST object with extended support for:
- CSS variables (custom properties) in colors
- Enhanced calc() expressions
- Improved legacy gradient syntax parsing
Examples
var gradient = require('gradient-parser');
var obj = gradient.parse('linear-gradient(30deg, #000, transparent)');
console.log(JSON.stringify(obj, null, 2));Results in:
[
{
"type": "linear-gradient",
"orientation": {
"type": "angular",
"value": "30"
},
"colorStops": [
{
"type": "hex",
"value": "000"
},
{
"type": "literal",
"value": "transparent"
}
]
}
]Installation
Install via npm:
npm install saltcat-gradient-parserImport in Node.js:
const gradient = require('saltcat-gradient-parser');For browser usage:
<script src="node_modules/saltcat-gradient-parser/build/web.js"></script>Fork Enhancements
This fork extends the original gradient-parser with the following custom CSS easing extensions:
CSS Easing Functions Support
Parse gradients with CSS easing functions for custom interpolation:
var gradient = require('saltcat-gradient-parser');
var obj = gradient.parse('linear-gradient(ease, red, blue)');
var obj2 = gradient.parse('linear-gradient(cubic-bezier(0.25, 0.1, 0.25, 1), to right, red, blue)');Supported easing functions:
lineareaseease-inease-outease-in-outcubic-bezier(x1, y1, x2, y2)
CSS Variables Support
var gradient = require('saltcat-gradient-parser');
var obj = gradient.parse('linear-gradient(45deg, var(--primary-color), #fff)');Enhanced Calc() Expressions
var obj = gradient.parse('linear-gradient(to right, red calc(50% - 10px), blue)');Improved Legacy Syntax
Better parsing of legacy gradient syntax including directional keywords without the to prefix:
var obj = gradient.parse('linear-gradient(bottom, #000, #fff)');Development
Project Status
Gradient-parser has been modernized (as of v1.1.0):
- Removed Bower support in favor of npm exclusively
- Replaced Grunt with a modern build system using esbuild
- Added minification support
- Updated dependencies to specific versions
- Improved npm scripts for better developer experience
Build
Gradient-parser uses a modern build system with esbuild for building and minification.
# Build both Node.js and web bundles
npm run build
# Build only Node.js bundle
npm run build:node
# Build only web bundle
npm run build:web
# Build minified bundles
npm run build:minifyTesting
Run the test suite with:
npm testStarting a local server
You can run a simple HTTP server for development:
npm startAPI
gradient.parse
Accepts the gradient definitions as it is declared in background-image and returns an AST object.
AST
Common properties
All nodes have the following properties.
type
String. The possible values are the ones listed in the Types section bellow.
Types
The available values of node.type are listed below, as well as the available properties of each node (other than the common properties listed above).
linear-gradient
- easing:
Objectorundefined. Easing function of typeeasing. - orientation:
Objectpossible typesdirectionalorangular. - colorStops:
Arrayof color stops of typeliteral,hex,rgb, orrgba.
repeating-linear-gradient
- easing:
Objectorundefined. Easing function of typeeasing. - orientation:
Objectpossible typesdirectionalorangular. - colorStops:
Arrayof color stops of typeliteral,hex,rgb, orrgba.
radial-gradient
- easing:
Objectorundefined. Easing function of typeeasing. - orientation:
Arrayorundefined.Arrayof possible typesshape,default-radial. - colorStops:
Arrayof color stops of typeliteral,hex,rgb, orrgba.
repeating-radial-gradient
- easing:
Objectorundefined. Easing function of typeeasing. - orientation:
Arrayorundefined.Arrayof possible typesshape,default-radial. - colorStops:
Arrayof color stops of typeliteral,hex,rgb, orrgba.
directional
- value:
Stringpossible valuesleft,top,bottom, orright.
angular
- value:
Numberinteger number.
literal
- value:
Stringliteral name of the color.
hex
- value:
Stringhex value.
rgb
- value:
Arrayof length 3 ofNumber's.
rgba
- value:
Arrayof length 4 orNumber's.
shape
- style:
Objectorundefinedpossible typesextent-keyword,px,em,%, orpositioning-keyword. - value:
Stringpossible valuesellipseorcircle. - at:
Objectof attributes:- x:
Objectpossible typesextent-keyword,px,em,%, orpositioning-keyword. - y:
Objectpossible typesextent-keyword,px,em,%, orpositioning-keyword.
- x:
default-radial
- at:
Objectof attributes:- x:
Objectpossible typesextent-keyword,px,em,%, orpositioning-keyword. - y:
Objectpossible typesextent-keyword,px,em,%, orpositioning-keyword.
- x:
positioning-keyword
- value:
Stringpossible valuescenter,left,top,bottom, orright.
extent-keyword
- value:
Stringpossible valuesclosest-side,closest-corner,farthest-side,farthest-corner,contain, orcover.
easing
- value:
StringCSS easing function value (e.g., "ease", "cubic-bezier(0.25, 0.1, 0.25, 1)").
License
(The MIT License)
Copyright (c) 2014-2025 Rafael Caricio [email protected]
Saltcat extensions Copyright (c) 2025 Durable Programming Team
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
