# linear-gradient-parser

> Parses a SVG linear gradient string / parsed JSON into css background image property

Latest version **1.1.8** (published 2021-07-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install linear-gradient-parser
pnpm add linear-gradient-parser
yarn add linear-gradient-parser
bun add linear-gradient-parser
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.8 |
| Published | 2021-07-23 |
| First published | 2019-11-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 62.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Oded Goldglas |
| Maintainers | pdedg |
| Keywords | svg, linear-greadient, background-image |

## Links

- npm: https://www.npmjs.com/package/linear-gradient-parser
- Repository: https://github.com/odedglas/linear-gradient-parser
- Issues: https://github.com/odedglas/linear-gradient-parser/issues
- npm.io page: https://npm.io/package/linear-gradient-parser

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 1.1.8 (latest) — 2021-07-23
- 1.1.7 — 2019-12-21
- 1.1.6 — 2019-12-13
- 1.1.5 — 2019-11-30
- 1.1.4 — 2019-11-26
- 1.1.3 — 2019-11-25
- 1.1.2 — 2019-11-24
- 1.1.1 — 2019-11-22
- 1.1.0 — 2019-11-22
- 1.0.9 — 2019-11-22
- 1.0.8 — 2019-11-22
- 1.0.7 — 2019-11-22
- 1.0.6 — 2019-11-22
- 1.0.5 — 2019-11-21
- 1.0.4 — 2019-11-21
- … 3 more at https://npm.io/package/linear-gradient-parser/versions

## README

# linear-gradient-parser
Converts a linear gradient SVG String / Json rep into CSS background property

## Installation

```
npm i linear-gradient-parser
```

[Live Demo](https://stackblitz.com/edit/linear-gradient-parser?file=index.js)

## Usage
Linear gradient parser comes in handy when you need get a svg linear gradient as background.
It can accepts both SVG linearGradient string / Json as input in order to produce it's background.

```js
import parser from 'linear-gradient-parser';

// SVG String input
const linearGradientString = `
<linearGradient xmlns="http://www.w3.org/2000/svg" gradientUnits="userSpaceOnUse" x1="17.5001" y1="32" x2="17.5001" y2="2.9711">
    <stop offset="0" style="stop-color:#FCB3A4"/>
    <stop offset="1" style="stop-color:#DA5899"/>
</linearGradient>
`

const stringResult = parser.getBackground(linearGradientString);

console.log(stringResult) 
//outputs : {background: "linear-gradient(0deg, rgb(252, 179, 164) 0%, rgb(218, 88, 153) 100%)", angle: 0}

// JSON input
const linearGradient = {
    x1: '17.5001',
    y1: '32',
    x2: '17.5001',
    y2: '2.9711',
    stops: [
        { color: '#FCC3A4', offset: '0', opacity: 0.5 },
        { color: '#AAA899', offset: '1' }
    ]
};

const jsonResult = parser.getBackground(linearGradient);

console.log(jsonResult) 
//outputs : {background: "linear-gradient(0deg, rgb(252, 179, 164) 0%, rgb(218, 88, 153) 100%)", angle: 0}

// Using angle generator
const gradientCords = parser.getGradientCords(90);

//outputs : { x1: 0, x2: 1, y1: 0, y2: 0 }
```

### Methods

#### getBackground

Converts a linear gradient into a background representation.

| Name | Type | Default Value | Required? | Description
|-|-|-|-|-
| `linearGradient` | `String|LinearGradient` | `undefined` | Yes | The linear gradient to parse

Returns: { angle: Number , background: String } 

#### getGradientCords

Builds a linear gradient position attributes for a given angle.

| Name | Type | Default Value | Required? | Description
|-|-|-|-|-
| `angle` | `Number` | `0` | Yes | The angle to create the x1 / x2 / y1 / y2 cords from.

Returns: { x1: Number, x2: Number, y1: Number, y2: Number }

---
_Source: https://npm.io/package/linear-gradient-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
