diff --git a/src/nearest.js b/src/nearest.js index 76874fcd..6532b513 100644 --- a/src/nearest.js +++ b/src/nearest.js @@ -12,7 +12,7 @@ const nearest = (colors, metric = differenceEuclidean(), accessor = d => d) => { let arr = colors.map((c, idx) => ({ color: accessor(c), i: idx })); return (color, n = 1, τ = Infinity) => { if (isFinite(n)) { - n = Math.max(1, Math.min(n, arr.length - 1)); + n = Math.max(1, Math.min(n, arr.length)); } arr.forEach(c => { diff --git a/test/nearest.test.js b/test/nearest.test.js index cd061bda..2cc2243f 100644 --- a/test/nearest.test.js +++ b/test/nearest.test.js @@ -40,3 +40,13 @@ test('nearest() with accessor', t => { let nearestColors = nearest(names, undefined, name => palette[name]); assert.deepEqual(nearestColors('red', 1), ['Bright Red']); }); + +test('nearest() returns up to all colors', t => { + let palette = ['red', 'green', 'blue', 'yellow', 'purple']; + let nearestColors = nearest(palette); + assert.equal( + nearestColors('orange', palette.length).length, + palette.length, + 'n equal to the number of colors returns all of them' + ); +});