diff --git a/core/modules/filters/colour-ops.js b/core/modules/filters/colour-ops.js index 341fb13f6..995b898f6 100644 --- a/core/modules/filters/colour-ops.js +++ b/core/modules/filters/colour-ops.js @@ -52,6 +52,30 @@ exports["colour-contrast"] = makeParallelColourOperator(function (colours, opera return colourContrasts; }); +exports["colour-best-contrast"] = makeParallelColourOperator(function (colours, operator, options, originalColours) { + var bestContrast = 0, + bestColour = null; + if(colours.length < 2) { + return []; + } + var targetColour = colours[colours.length - 1]; + for(var t=0; t bestContrast) { + bestContrast = contrast; + bestColour = originalColours[t]; + } + } + } + if(bestColour) { + return [bestColour]; + } else { + return []; + } +}); + exports["colour-interpolate"] = makeParallelColourOperator(function (colours, operator, options) { // Special case for less than two colours if(colours.length < 2) { @@ -89,11 +113,13 @@ function makeSerialColourOperator(fn) { function makeParallelColourOperator(fn) { return function (source, operator, options) { - var colours = []; + var originalColours = [], + colours = []; source(function (tiddler, title) { + originalColours.push(title); colours.push($tw.utils.parseCSSColorObject(title)); }); - return fn(colours, operator, options); + return fn(colours, operator, options, originalColours); }; } diff --git a/editions/test/tiddlers/tests/data/operators/colour/ColourBestContrast.tid b/editions/test/tiddlers/tests/data/operators/colour/ColourBestContrast.tid new file mode 100644 index 000000000..d52ca4213 --- /dev/null +++ b/editions/test/tiddlers/tests/data/operators/colour/ColourBestContrast.tid @@ -0,0 +1,17 @@ +title: Operators/Colour/ColourBestContrast +description: Best contrast colour function +type: text/vnd.tiddlywiki-multiple +tags: [[$:/tags/wiki-test-spec]] + +title: Output + +\parsermode inline +<$text text={{{ [subfilter{Filter}] }}}/> ++ +title: Filter + +[[black]] [[gray]] [[green]] +[colour-best-contrast:DeltaPhi[]] ++ +title: ExpectedResult + +black \ No newline at end of file