Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4a5033
commit 24e515e
Showing
3 changed files
with
152 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
define(function(require, exports, module) { | ||
// use the apf one, when apf is avaialble. Otherwise use some mocked one | ||
var namedColors = typeof apf !== "undefined" ? apf.color.colorshex : { "black":0, "white":16777215 }; | ||
var namedPart = Object.keys(namedColors).join("|"); | ||
|
||
var patterns = { | ||
rgb: "rgba?\\(\\s*\\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\b\\s*,\\s*\\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\b\\s*,\\s*\\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\b\\s*(:?\\s*,\\s*(?:1|0|0?\\.[0-9]{1,2})\\s*)?\\)", | ||
rgb_alt: "rgba?\\(\\s*\\b(\\d{1,2}|100)%\\s*,\\s*\\b(\\d{1,2}|100)%\\s*,\\s*\\b(\\d{1,2}|100)%\\s*(:?\\s*,\\s*(?:1|0|0?\\.[0-9]{1,2})\\s*)?\\)", | ||
hsl: "hsla?\\(\\s*\\b([1-2][0-9][0-9]|360|3[0-5][0-9]|[1-9][0-9]|[0-9])\\b\\s*,\\s*\\b(\\d{1,2}|100)%\\s*,\\s*\\b(\\d{1,2}|100)%\\s*(:?\\s*,\\s*(?:1|0|0?\\.[0-9]{1,2})\\s*)?\\)" | ||
}; | ||
|
||
var isColor = new RegExp("(#([0-9A-Fa-f]{3,6})\\b)" | ||
+ "|\\b(" + namedPart + ")\\b" | ||
+ "|(" + patterns.rgb + ")" | ||
+ "|(" + patterns.rgb_alt + ")" | ||
+ "|(" + patterns.hsl + ")", "gi"); | ||
|
||
var isRgb = new RegExp("(?:" + patterns.rgb + ")" | ||
+ "|(?:" + patterns.rgb_alt + ")"); | ||
|
||
var isHsl = new RegExp(patterns.hsl); | ||
|
||
exports = module.exports = { | ||
isColor: isColor, | ||
isRgb: isRgb, | ||
isHsl: isHsl | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
if (typeof process !== "undefined") { | ||
require("amd-loader"); | ||
} | ||
|
||
|
||
if (typeof process !== "undefined") { | ||
require("../../../support/paths"); | ||
require.paths.unshift(__dirname + "/../.."); | ||
} | ||
|
||
var regex = require("./colorpicker_regex"); | ||
|
||
define(function(require, exports, module) { | ||
var assert = require("assert"); | ||
|
||
module.exports = { | ||
"test simple rgb": function(next) { | ||
var res = regex.isRgb("rgb(14, 235, 19)"); | ||
|
||
assert.equal(!!res, true); | ||
assert.equal(res[1], "14"); | ||
assert.equal(res[2], "235"); | ||
assert.equal(res[3], "19"); | ||
assert.equal(res[4], undefined); | ||
|
||
next(); | ||
}, | ||
"test simple rgba": function(next) { | ||
var res = regex.isRgb("rgba(14, 235, 19, .9)"); | ||
|
||
assert.equal(!!res, true); | ||
assert.equal(res[1], "14"); | ||
assert.equal(res[2], "235"); | ||
assert.equal(res[3], "19"); | ||
assert.equal(res[4], ", .9"); | ||
|
||
next(); | ||
}, | ||
"test rgba with transparancy over 1": function(next) { | ||
var res = regex.isRgb("rgba(14, 235, 19, 1.9)"); | ||
|
||
assert.equal(!!res, false); | ||
|
||
next(); | ||
}, | ||
"test rgb with values over 255": function(next) { | ||
var res = regex.isRgb("rgb(256, 19, 32)"); | ||
|
||
assert.equal(!!res, false); | ||
|
||
next(); | ||
}, | ||
"test hsl": function(next) { | ||
var res = regex.isHsl("hsl(14, 99%, 33%)"); | ||
|
||
assert.equal(!!res, true); | ||
assert.equal(res[1], "14"); | ||
assert.equal(res[2], "99"); | ||
assert.equal(res[3], "33"); | ||
assert.equal(res[4], undefined); | ||
|
||
next(); | ||
}, | ||
"test hsla": function(next) { | ||
var res = regex.isHsl("hsla(14, 99%, 33%, .7)"); | ||
|
||
assert.equal(!!res, true); | ||
assert.equal(res[1], "14"); | ||
assert.equal(res[2], "99"); | ||
assert.equal(res[3], "33"); | ||
assert.equal(res[4], ", .7"); | ||
|
||
next(); | ||
}, | ||
"test hsla positive transparancy": function(next) { | ||
var res = regex.isHsl("hsla(14, 99%, 33%, 3.7)"); | ||
|
||
assert.equal(!!res, false); | ||
|
||
next(); | ||
}, | ||
"test hsla values too high": function(next) { | ||
var res = regex.isHsl("hsla(201, 101%, 00000411%, 0.7)"); | ||
|
||
assert.equal(!!res, false); | ||
|
||
next(); | ||
}, | ||
"test hsla bullocks values": function(next) { | ||
var res = regex.isHsl("hsla(abc, def, geh, .9)"); | ||
|
||
assert.equal(!!res, false); | ||
|
||
next(); | ||
}, | ||
"test text colors": function(next) { | ||
var res = regex.isColor("white"); | ||
|
||
assert.equal(!!res, true); | ||
|
||
next(); | ||
}, | ||
"test text any string": function(next) { | ||
var res = regex.isColor("jan is cool"); | ||
|
||
assert.equal(!!res, false); | ||
|
||
next(); | ||
}, | ||
}; | ||
}); | ||
|
||
if (typeof module !== "undefined" && module === require.main) { | ||
require("asyncjs").test.testcase(module.exports).exec(); | ||
} |