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
44 changes: 44 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,50 @@ test("Readability", function () {
equal (tinycolor.mostReadable("#f00", ["#d00", "#0d0"]).toHexString(), "#00dd00", "pick most readable color");
});

test("ScanForColors", function () {
var blackHex = tinycolor.fromRatio({ r: 0, g: 0, b: 0 }, {format: "hex"}),
single_line_single_result = "background-color: #000000;",
results = tinycolor.scanForColors(single_line_single_result);
equal (results.length, 1);
equal (results[0], blackHex.toString());

var single_line_multiple_results = "{ content: '#000'; background: orange; }";
results = tinycolor.scanForColors(single_line_multiple_results);
equal (results.length, 2);

equal (results[0], blackHex.toString());
equal (results[1], "orange");

var multiple_lines_single_results = "body { \n font-size: 12px;\n\n text-color: rgba (255, 0, 0, .5) }";
results = tinycolor.scanForColors(multiple_lines_single_results);
equal (results.length, 1);
equal (results[0].toRgbString(), "rgba(255, 0, 0, 0.5)");

var multiple_lines_multiple_results = "body { \n background-color: hsl 0 1.0 0.5;\n\n text-color: rgba (255, 0, 0, .5) }";
results = tinycolor.scanForColors(multiple_lines_multiple_results);
equal (results.length, 2);
equal (results[0].toHslString(), "hsl(0, 100%, 50%)");
equal (results[1].toRgbString(), "rgba(255, 0, 0, 0.5)");

equal (tinycolor.scanForColors("body {").length, 0);

var negativeMatchThreeHex = "background-position: 100% 50%;",
results = tinycolor.scanForColors(negativeMatchThreeHex);
equal (results.length, 0);

var positiveMatchThreeHex = "background-position: #100% 50%;",
results = tinycolor.scanForColors(positiveMatchThreeHex);
equal (results.length, 1);

var negativeMatchString = "margin: 0 !important;",
results = tinycolor.scanForColors(negativeMatchString);
equal (results.length, 0);

var negativeMatchFourHex = "color: #fade;",
results = tinycolor.scanForColors(negativeMatchFourHex);
equal (results.length, 0);
});

test("Filters", function () {

equal (tinycolor("red").toFilter(), "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ffff0000,endColorstr=#ffff0000)");
Expand Down
47 changes: 47 additions & 0 deletions tinycolor.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,53 @@ tinycolor.mostReadable = function(baseColor, colorList) {
return bestColor;
};

tinycolor.constructMatcherRegExp = function() {
if (tinycolor.joinedMatcherRegExp !== undefined)
return tinycolor.joinedMatcherRegExp;

var joinedMatcherRegExp = [], name = null, match = null;

for (name in names) {
if (names.hasOwnProperty(name)) {
// "\\b" prevents words like imporTANt
joinedMatcherRegExp.push("\\b" + name + "\\b");
}
}

// construct a giant regexp of the color matchers
for (match in matchers) {
if (matchers.hasOwnProperty(match)) {
var matchAsString = String(matchers[match]);
if (/^hex/.test(match))
matchAsString = "#" + matchAsString;

joinedMatcherRegExp.push("(?:\\s*|\\b)" + matchAsString.replace(/\^|\$|\//g, '') + "\\b");
}
}

this.joinedMatcherRegExp = new RegExp(joinedMatcherRegExp.join("|"), "gi");
return this.joinedMatcherRegExp;
};

// `scanForColors`
// Given a string, determines if it has a valid color.
// Returns an array of matched objects if it does, or an empty array otherwise.
tinycolor.scanForColors = function(text) {
var joinedMatcherRegExp = this.constructMatcherRegExp(), results = [], match = null, lines = text.split("\n");

// for each line, try to find a match. if it's found, turn it into a tinycolor
// and append it to the results
for (var l = 0, size = lines.length; l < size; l++) {
match = lines[l].match(joinedMatcherRegExp);
if (match === null)
continue;
while (match.length > 0) {
results.push(tinycolor(match.shift()));
}
}

return results;
};

// Big List of Colors
// ------------------
Expand Down