Skip to content

Commit

Permalink
Do not limit the user
Browse files Browse the repository at this point in the history
* Do not require ECMA6.
* Do not round channel values.
* Do not compute small and obivious gradients.
  • Loading branch information
aurium committed Nov 5, 2019
1 parent d4c4e61 commit e4183f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
26 changes: 20 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@ Color.prototype = {
if (steps < 2) {
throw new Error('Argument "steps" must to be bigger or equal to 2.');
}
if (steps === 2) {
return [this.rgb(), toColor.rgb()];
}
if (steps === 3) {
return [this.rgb(), this.mix(toColor, 0.5).rgb(), toColor.rgb()];
}
toColor = toColor.rgb();
var fromColor = this.rgb();
var [fR, fG, fB] = fromColor.color;
Expand Down Expand Up @@ -422,6 +428,9 @@ Color.prototype = {
if (steps < 2) {
throw new Error('Argument "steps" must to be bigger or equal to 2.');
}
if (steps === 2) {
return [this.hsl(), toColor.hsl()];
}
if (!rotationWay) {
rotationWay = 1;
}
Expand All @@ -430,8 +439,12 @@ Color.prototype = {
}
toColor = toColor.hsl();
var fromColor = this.hsl();
var [fH, fS, fL] = fromColor.color;
var [tH, tS, tL] = toColor.color;
var fH = fromColor.color[0];
var fS = fromColor.color[1];
var fL = fromColor.color[2];
var tH = toColor.color[0];
var tS = toColor.color[1];
var tL = toColor.color[2];
var fA = fromColor.alpha();
var tA = toColor.alpha();
if (rotationWay > 0 && fH > tH) {
Expand All @@ -445,14 +458,15 @@ Color.prototype = {
var incL = (tL - fL) / (steps - 1);
var incA = (tA - fA) / (steps - 1);
var gradient = [fromColor];
for (var i = 1; i < steps; i++) {
for (var i = 1; i < (steps - 1); i++) {
gradient.push(Color({
h: Math.round(fH + incH * i),
s: Math.round(fS + incS * i),
l: Math.round(fL + incL * i),
h: fH + incH * i,
s: fS + incS * i,
l: fL + incL * i,
alpha: fA + incA * i
}));
}
gradient.push(toColor);
return gradient;
}

Expand Down
18 changes: 9 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,27 +680,27 @@ it('Gradient in RGBA', function () {
it('Gradient in HSL', function () {
deepEqual(Color('#f00').gradientHSL(Color('#080'), 4).map(c => c.string()), [
'hsl(0, 100%, 50%)',
'hsl(40, 100%, 42%)',
'hsl(80, 100%, 34%)',
'hsl(120, 100%, 27%)'
'hsl(40, 100%, 42.2%)',
'hsl(80, 100%, 34.4%)',
'hsl(120, 100%, 26.7%)'
]);
});

it('Gradient in HSLA', function () {
deepEqual(Color('#f00').gradientHSL(Color('#080').alpha(0.85), 4).map(c => c.string()), [
'hsl(0, 100%, 50%)',
'hsla(40, 100%, 42%, 0.95)',
'hsla(80, 100%, 34%, 0.9)',
'hsla(120, 100%, 27%, 0.85)'
'hsla(40, 100%, 42.2%, 0.95)',
'hsla(80, 100%, 34.4%, 0.9)',
'hsla(120, 100%, 26.7%, 0.85)'
]);
});

it('Gradient in HSL counterclokwise', function () {
deepEqual(Color('#f00').gradientHSL(Color('#080'), 4, -1).map(c => c.string()), [
'hsl(0, 100%, 50%)',
'hsl(280, 100%, 42%)',
'hsl(200, 100%, 34%)',
'hsl(120, 100%, 27%)'
'hsl(280, 100%, 42.2%)',
'hsl(200, 100%, 34.4%)',
'hsl(120, 100%, 26.7%)'
]);
});

Expand Down

0 comments on commit e4183f1

Please sign in to comment.