diff --git a/core/modules/filters/colour-ops.js b/core/modules/filters/colour-ops.js new file mode 100644 index 000000000..3ce9d1dca --- /dev/null +++ b/core/modules/filters/colour-ops.js @@ -0,0 +1,41 @@ +/*\ +title: $:/core/modules/filters/colour-ops.js +type: application/javascript +module-type: filteroperator + +Filter operators for colour operations + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var Color = require("$:/core/modules/utils/dom/color.js").Color; + +exports["colour-lighten"] = makeColourOperator(function (c, operator, operands) { + return c.lighten($tw.utils.parseNumber(operator.operand)); +}); + +exports["colour-darken"] = makeColourOperator(function (c, operator, operands) { + return c.darken($tw.utils.parseNumber(operator.operand)); +}); + +function makeColourOperator(fn) { + return function (source, operator, options) { + var results = []; + source(function (tiddler, title) { + var c = $tw.utils.parseCSSColorObject(title); + if (c) { + c = fn(c, operator, options); + results.push(c.display().toString()); + } else { + results.push(""); + } + }); + return results; + }; +} + +})(); diff --git a/core/modules/utils/dom/color-utils.js b/core/modules/utils/dom/color-utils.js index f4e119127..74e533a37 100644 --- a/core/modules/utils/dom/color-utils.js +++ b/core/modules/utils/dom/color-utils.js @@ -12,14 +12,13 @@ Color.js related utilities /*global $tw: false */ "use strict"; +var Color = require("$:/core/modules/utils/dom/color.js").Color; + +/* +For backwards compatibility +*/ exports.parseCSSColor = function(colourString) { - var Color = require("$:/core/modules/utils/dom/color.js").Color, - c = null; - try { - c = new Color(colourString); - } catch(e) { - // Do nothing on an error - } + var c = exports.parseCSSColorObject(colourString); if(c) { var rgb = c.srgb; return [rgb[0],rgb[1],rgb[2],c.alpha]; @@ -28,4 +27,19 @@ exports.parseCSSColor = function(colourString) { } }; + +/* +Preferred way to parse a Color.js colour +*/ +exports.parseCSSColorObject = function(colourString) { + var Color = require("$:/core/modules/utils/dom/color.js").Color, + c = null; + try { + c = new Color(colourString); + } catch(e) { + // Return null if there is an error + } + return c; +}; + })(); diff --git a/editions/test/tiddlers/tests/data/operators/colour/ColourDarken.tid b/editions/test/tiddlers/tests/data/operators/colour/ColourDarken.tid new file mode 100644 index 000000000..7801d1c34 --- /dev/null +++ b/editions/test/tiddlers/tests/data/operators/colour/ColourDarken.tid @@ -0,0 +1,18 @@ +title: Operators/Colour/ColourDarken +description: Darken colour function +type: text/vnd.tiddlywiki-multiple +tags: [[$:/tags/wiki-test-spec]] + +title: Output + +\parsermode inline +\import [subfilter{$:/core/config/GlobalImportFilter}] +<$text text={{{ [subfilter{Filter}] }}}/> ++ +title: Filter + +[function[colour],[primary]colour-darken[0.5]] ++ +title: ExpectedResult + +#0f1d77 \ No newline at end of file diff --git a/editions/test/tiddlers/tests/data/operators/colour/ColourLighten.tid b/editions/test/tiddlers/tests/data/operators/colour/ColourLighten.tid new file mode 100644 index 000000000..967215e52 --- /dev/null +++ b/editions/test/tiddlers/tests/data/operators/colour/ColourLighten.tid @@ -0,0 +1,18 @@ +title: Operators/Colour/ColourLighten +description: Lighten colour function +type: text/vnd.tiddlywiki-multiple +tags: [[$:/tags/wiki-test-spec]] + +title: Output + +\parsermode inline +\import [subfilter{$:/core/config/GlobalImportFilter}] +<$text text={{{ [subfilter{Filter}] }}}/> ++ +title: Filter + +[function[colour],[primary]colour-lighten[0.5]] ++ +title: ExpectedResult + +#c6dbff \ No newline at end of file