diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a882442 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,7 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 4 diff --git a/lib/main.js b/lib/main.js index 9613420..07ff7e0 100644 --- a/lib/main.js +++ b/lib/main.js @@ -26,33 +26,41 @@ Color.prototype.similarTo = function(other) { return this.difference(other) < 2.3; }; -var functions = { - identity: function(color, target) { - return { - color: color, - difference: target.difference(color), - format: "@input" - }; - }, - lighten: function(color, target) { - var lighten = registry.get("lighten"); +function suggestParameterFunction(functionName) { + return function(color, target) { + var func = registry.get(functionName); var amount = { value: 1 }; var result = []; - for (var val = 1; val <= 50; val++) { + for (var val = 1; val <= 100; val++) { amount.value = val; - var newColor = lighten(color, amount); + var newColor = func(color, amount); + var difference = target.difference(newColor); result.push({ color: newColor, - difference: target.difference(newColor), - format: "lighten(@input, " + amount.value + "%)" + difference: difference, + format: functionName + "(@input, " + amount.value + "%)" }); + + if (difference < 0.05) { + break; + } } return best(result, target)[0]; + }; +} + +var functions = { + identity: function(color, target) { + return { + color: color, + difference: target.difference(color), + format: "@input" + }; }, contrast: function(color, target) { var contrast = registry.get("contrast"); @@ -62,7 +70,11 @@ var functions = { difference: target.difference(result), format: "contrast(@input)" }; - } + }, + lighten: suggestParameterFunction("lighten"), + darken: suggestParameterFunction("darken"), + desaturate: suggestParameterFunction("desaturate"), + saturate: suggestParameterFunction("saturate") }; function best(results, target) { diff --git a/test/main.js b/test/main.js index aca50ce..938ec0d 100644 --- a/test/main.js +++ b/test/main.js @@ -5,7 +5,13 @@ describe("whichcolorfunction", function(){ describe("#suggest()", function(){ var suggestion; function firstFormat(from, to) { - return suggest(from, to)[0].format; + var suggestions = suggest(from, to); + + if (!suggestions.length) { + throw new Error("Nothing suggested!"); + } + + return suggestions[0].format; } assert.contains = function(haystack, needle, message) { assert(haystack.indexOf(needle) > -1, message || @@ -29,5 +35,20 @@ describe("whichcolorfunction", function(){ suggestion = firstFormat("000000", "050505"); assert.contains(suggestion, "lighten(@input, 2%)"); }); + it("should suggest darken", function() { + suggestion = firstFormat("cccccc", "c9c9c9"); + assert.contains(suggestion, "darken(@input, 1%)"); + + suggestion = firstFormat("cccccc", "c4c4c4"); + assert.contains(suggestion, "darken(@input, 3%)"); + }); + it("should suggest desaturate", function() { + suggestion = firstFormat("80e619", "80cc33"); + assert.contains(suggestion, "desaturate(@input, 20%)"); + }); + it("should suggest saturate", function() { + suggestion = firstFormat("80e619", "80ff00"); + assert.contains(suggestion, "saturate(@input, 20%)"); + }); }); });