Skip to content

Commit

Permalink
Add darken/saturate/desaturate suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
gyoshev committed Jul 23, 2015
1 parent 0b5daf9 commit 45ce85d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
42 changes: 27 additions & 15 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) {
Expand Down
23 changes: 22 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand All @@ -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%)");
});
});
});

0 comments on commit 45ce85d

Please sign in to comment.