Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,10 @@ function analogous(color, results, slices) {
results = results || 6;
slices = slices || 30;

if (isNaN(results) || results <= 0 || results % 1 !== 0) {
throw new Error("Argument to analogous must be a positive integer");
}

var hsl = tinycolor(color).toHsl();
var part = 360 / slices;
var ret = [tinycolor(color)];
Expand All @@ -758,6 +762,10 @@ function analogous(color, results, slices) {

function monochromatic(color, results) {
results = results || 6;

if (isNaN(results) || results <= 0 || results % 1 !== 0) {
throw new Error("Argument to monochromatic must be a positive integer");
}
var hsv = tinycolor(color).toHsv();
var h = hsv.h,
s = hsv.s,
Expand Down
8 changes: 8 additions & 0 deletions npm/cjs/tinycolor.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,10 @@
function _analogous(color, results, slices) {
results = results || 6;
slices = slices || 30;

if (isNaN(results) || results <= 0 || results % 1 !== 0) {
throw new Error("Argument to analogous must be a positive integer");
}
var hsl = tinycolor(color).toHsl();
var part = 360 / slices;
var ret = [tinycolor(color)];
Expand All @@ -664,6 +668,10 @@
}
function _monochromatic(color, results) {
results = results || 6;

if (isNaN(results) || results <= 0 || results % 1 !== 0) {
throw new Error("Argument to monochromatic must be a positive integer");
}
var hsv = tinycolor(color).toHsv();
var h = hsv.h,
s = hsv.s,
Expand Down
8 changes: 8 additions & 0 deletions npm/esm/tinycolor.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,10 @@ function _splitcomplement(color) {
function _analogous(color, results, slices) {
results = results || 6;
slices = slices || 30;

if (isNaN(results) || results <= 0 || results % 1 !== 0) {
throw new Error("Argument to analogous must be a positive integer");
}
var hsl = tinycolor(color).toHsl();
var part = 360 / slices;
var ret = [tinycolor(color)];
Expand All @@ -658,6 +662,10 @@ function _analogous(color, results, slices) {
}
function _monochromatic(color, results) {
results = results || 6;

if (isNaN(results) || results <= 0 || results % 1 !== 0) {
throw new Error("Argument to monochromatic must be a positive integer");
}
var hsv = tinycolor(color).toHsv();
var h = hsv.h,
s = hsv.s,
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2138,6 +2138,26 @@ Deno.test("tetrad", function () {
);
});

Deno.test("analogous and monochromatic reject counts that never terminate", function () {
// `--results` / `results--` test for truthiness, so a negative or fractional
// count never lands on 0 and the loop allocates until the heap is gone.
// `polyad` already guards the same shape of input.
for (const bad of [-1, 1.5, 0.5, -0.5]) {
assertThrows(() => {
tinycolor("red").analogous(bad);
});
assertThrows(() => {
tinycolor("red").monochromatic(bad);
});
}

// Falsy values still fall through to the default of 6, unchanged.
assertEquals(tinycolor("red").analogous().length, 6);
assertEquals(tinycolor("red").analogous(0).length, 6);
assertEquals(tinycolor("red").monochromatic(null).length, 6);
assertEquals(tinycolor("red").analogous(3).length, 3);
});

Deno.test({
name: "polyad",
// Disabled until https://github.com/bgrins/TinyColor/issues/254
Expand Down
8 changes: 8 additions & 0 deletions tinycolor.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@
function _analogous(color, results, slices) {
results = results || 6;
slices = slices || 30;

if (isNaN(results) || results <= 0 || results % 1 !== 0) {
throw new Error("Argument to analogous must be a positive integer");
}
var hsl = tinycolor(color).toHsl();
var part = 360 / slices;
var ret = [tinycolor(color)];
Expand All @@ -663,6 +667,10 @@
}
function _monochromatic(color, results) {
results = results || 6;

if (isNaN(results) || results <= 0 || results % 1 !== 0) {
throw new Error("Argument to monochromatic must be a positive integer");
}
var hsv = tinycolor(color).toHsv();
var h = hsv.h,
s = hsv.s,
Expand Down