Skip to content

Commit c038fa4

Browse files
committed
Add support for colorMode + change default to lrgb
1 parent 9dd9d1b commit c038fa4

File tree

5 files changed

+18
-26
lines changed

5 files changed

+18
-26
lines changed

README.md

+5-19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[![Build Status][ci-img]][ci]
66
[![NPM Version][npm-img]][npm]
77
[![NPM Monthly Downloads][dm-img]][npm]
8+
[![JavaScript Style Guide][std-img]][std]
89

910
⚠️ The [syntax](#syntax) has changed substantially in `^v.2.0.0` ⚠️
1011

@@ -15,7 +16,7 @@ Visual examples and online editor on [larsenwork.com/easing-gradients](https://l
1516

1617
<br>
1718

18-
## Examples
19+
## Code Examples
1920

2021
```css
2122
.cubic-bezier {
@@ -131,27 +132,12 @@ I wouldn't recommend tweaking from default values but:
131132

132133
* `precision: 0.1` is the default and creates ~17 color stops. A higher number creates a more "low poly" gradient and banding becomes very visible when using anything above 0.2.
133134
* `alphaDecimals: 5` is the default. A lower number can result in banding.
134-
135-
<br>
136-
137-
## Demo
138-
139-
Make sure that you have gulp cli installed and then `cd demo`, `npm install` and `gulp`.
140-
141-
If you want to develop then `npm link` in project root and then `npm link postcss-easing-gradients`
142-
143-
<br>
144-
145-
## Contributions
146-
147-
Are more than welcome. Code is linted using:
148-
149-
[![js-standard-style][js-img]][js]
135+
* `colorMode: 'lrgb'` is the default color space used for interpolation and is closest to what most browsers use. Other options are `'rgb', 'hsl', 'lab' and 'lch'` as per [chromajs documentation](http://gka.github.io/chroma.js/#chroma-mix)
150136

151137
[ci-img]: https://img.shields.io/travis/larsenwork/postcss-easing-gradients.svg
152138
[ci]: https://travis-ci.org/larsenwork/postcss-easing-gradients
153-
[js-img]: https://cdn.rawgit.com/feross/standard/master/badge.svg
154-
[js]: https://standardjs.com
155139
[npm-img]: https://img.shields.io/npm/v/postcss-easing-gradients.svg
156140
[npm]: https://www.npmjs.com/package/postcss-easing-gradients
157141
[dm-img]: https://img.shields.io/npm/dm/postcss-easing-gradients.svg
142+
[std-img]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
143+
[std]: https://standardjs.com

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = postcss.plugin('easing-gradient', (options = {}) => {
2929
try {
3030
const colors = [gradientParams[i - 1], gradientParams[i + 1]]
3131
const coordinates = easingCoordinates.easingCoordinates(param, options.precision)
32-
const colorStops = getColorStops(colors, coordinates, options.alphaDecimals)
32+
const colorStops = getColorStops(colors, coordinates, options.alphaDecimals, options.colorMode)
3333
// Update node
3434
node.type = 'word'
3535
// Assume if it has 4 params it's because the first one is the direction

index.test.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function run (input, output, opts) {
1515
it('create a steps gradient with direction', () => {
1616
return run(
1717
'a{ background: linear-gradient(to right, green, steps(4, skip-none), red); }',
18-
'a{ background: linear-gradient(to right, hsl(120, 100%, 25.1%), hsl(120, 100%, 25.1%) 25%, hsl(60.23, 100%, 16.73%) 25%, hsl(60.23, 100%, 16.73%) 50%, hsl(15.06, 100%, 33.33%) 50%, hsl(15.06, 100%, 33.33%) 75%, hsl(0, 100%, 50%) 75%, hsl(0, 100%, 50%)); }', // eslint-disable-line max-len
18+
'a{ background: linear-gradient(to right, hsl(120, 100%, 25.1%), hsl(120, 100%, 25.1%) 25%, hsl(42.59, 100%, 28.87%) 25%, hsl(42.59, 100%, 28.87%) 50%, hsl(21.3, 100%, 40.82%) 50%, hsl(21.3, 100%, 40.82%) 75%, hsl(0, 100%, 50%) 75%, hsl(0, 100%, 50%)); }', // eslint-disable-line max-len
1919
{ }
2020
)
2121
})
@@ -29,7 +29,7 @@ it('create a cubic bezier gradient with direction', () => {
2929
it('create an ease gradient with direction', () => {
3030
return run(
3131
'a{ background: linear-gradient(to right, green, ease, red); }',
32-
'a{ background: linear-gradient(to right, hsl(120, 100%, 25.1%), hsl(111.85, 100%, 23.5%) 7.8%, hsl(98.92, 100%, 21.34%) 13.2%, hsl(82.02, 100%, 19.05%) 17.6%, hsl(60.02, 100%, 16.71%) 21.7%, hsl(40.57, 100%, 21.3%) 25.8%, hsl(28.11, 100%, 25.86%) 30.2%, hsl(19.54, 100%, 30.32%) 35.1%, hsl(13.47, 100%, 34.55%) 40.6%, hsl(9.01, 100%, 38.49%) 46.9%, hsl(5.73, 100%, 42.01%) 54.1%, hsl(3.36, 100%, 44.98%) 62.2%, hsl(1.74, 100%, 47.28%) 71.1%, hsl(0.7, 100%, 48.86%) 80.6%, hsl(0.15, 100%, 49.74%) 90.5%, hsl(0, 100%, 50%)); }', // eslint-disable-line max-len
32+
'a{ background: linear-gradient(to right, hsl(120, 100%, 25.1%), hsl(88.79, 100%, 24.28%) 7.8%, hsl(69.81, 100%, 23.14%) 13.2%, hsl(53.43, 100%, 24.55%) 17.6%, hsl(42.52, 100%, 28.9%) 21.7%, hsl(34.96, 100%, 32.64%) 25.8%, hsl(29.1, 100%, 35.96%) 30.2%, hsl(24.26, 100%, 38.94%) 35.1%, hsl(20.14, 100%, 41.56%) 40.6%, hsl(16.47, 100%, 43.87%) 46.9%, hsl(13.13, 100%, 45.83%) 54.1%, hsl(10.07, 100%, 47.42%) 62.2%, hsl(7.23, 100%, 48.62%) 71.1%, hsl(4.6, 100%, 49.43%) 80.6%, hsl(2.16, 100%, 49.87%) 90.5%, hsl(0, 100%, 50%)); }', // eslint-disable-line max-len
3333
{ }
3434
)
3535
})
@@ -51,7 +51,13 @@ it('create a cubic-bezier gradient with .3 precision', () => {
5151
{ precision: 0.3 }
5252
)
5353
})
54-
54+
it('create an ease gradient with hsl colorMode', () => {
55+
return run(
56+
'a{ background: linear-gradient(to right, green, ease, red); }',
57+
'a{ background: linear-gradient(to right, hsl(120, 100%, 25.1%), hsl(112.5, 100%, 26.67%) 7.8%, hsl(102.04, 100%, 28.82%) 13.2%, hsl(91.32, 100%, 31.18%) 17.6%, hsl(79.76, 100%, 33.33%) 21.7%, hsl(68.9, 100%, 35.69%) 25.8%, hsl(57.84, 100%, 38.04%) 30.2%, hsl(47.12, 100%, 40.2%) 35.1%, hsl(36.94, 100%, 42.35%) 40.6%, hsl(27.61, 100%, 44.31%) 46.9%, hsl(19.15, 100%, 46.08%) 54.1%, hsl(12.15, 100%, 47.45%) 62.2%, hsl(6.53, 100%, 48.63%) 71.1%, hsl(2.86, 100%, 49.41%) 80.6%, hsl(0.71, 100%, 49.8%) 90.5%, hsl(0, 100%, 50%)); }', // eslint-disable-line max-len
58+
{ colorMode: 'hsl' }
59+
)
60+
})
5561
/**
5662
* Ignore incorrect/unsuported input
5763
*/

lib/colorStops.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ const helpers = require('./helpers.js')
44
/**
55
* Calculate the color stops based on start+stopColor in an array and easingType
66
*/
7-
module.exports = (colors, coordinates, alphaDecimals = 5) => {
7+
module.exports = (colors, coordinates, alphaDecimals = 5, colorMode = 'lrgb') => {
88
let colorStops = []
99
colors = helpers.transparentFix(colors)
1010
coordinates.forEach(coordinate => {
1111
const ammount = coordinate.y
1212
const percent = coordinate.x * 100
13-
let color = chroma.mix(colors[0], colors[1], ammount).css('hsl')
13+
let color = chroma.mix(colors[0], colors[1], ammount, colorMode).css('hsl')
1414
color = helpers.roundHslAlpha(color, alphaDecimals)
1515
if (Number(coordinate.x) !== 0 && Number(coordinate.x) !== 1) {
1616
colorStops.push(`${color} ${+percent.toFixed(2)}%`)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-easing-gradients",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "PostCSS plugin to create smooth linear-gradients that approximate easing functions.",
55
"scripts": {
66
"test": "jest && eslint *.js"

0 commit comments

Comments
 (0)