From aa69c3ae915b720f20e88dbf37b08366ba7a01e9 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Mon, 18 Nov 2024 18:15:20 +0000 Subject: [PATCH] Add colour-contrast operator --- core/modules/filters/colour-ops.js | 32 ++++++++++++++++--- .../data/operators/colour/ColourContrast.tid | 17 ++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 editions/test/tiddlers/tests/data/operators/colour/ColourContrast.tid diff --git a/core/modules/filters/colour-ops.js b/core/modules/filters/colour-ops.js index 6cf0f32ee..a004a1a1f 100644 --- a/core/modules/filters/colour-ops.js +++ b/core/modules/filters/colour-ops.js @@ -14,15 +14,15 @@ Filter operators for colour operations var Color = require("$:/core/modules/utils/dom/color.js").Color; -exports["colour-lighten"] = makeColourOperator(function (c, operator, options) { +exports["colour-lighten"] = makeSerialColourOperator(function (c, operator, options) { return c.lighten($tw.utils.parseNumber(operator.operand)); }); -exports["colour-darken"] = makeColourOperator(function (c, operator, options) { +exports["colour-darken"] = makeSerialColourOperator(function (c, operator, options) { return c.darken($tw.utils.parseNumber(operator.operand)); }); -exports["colour-oklch"] = makeColourOperator(function (c, operator, options) { +exports["colour-oklch"] = makeSerialColourOperator(function (c, operator, options) { var prop = ((operator.suffixes || [])[0] || ["l"])[0]; if(["l","c","h"].indexOf(prop) !== -1) { c.oklch[prop] = $tw.utils.parseNumber(operator.operand); @@ -30,7 +30,21 @@ exports["colour-oklch"] = makeColourOperator(function (c, operator, options) { return c; }); -function makeColourOperator(fn) { +exports["colour-contrast"] = makeParallelColourOperator(function (colours, operator, options) { + var colourContrasts = []; + $tw.utils.each(colours,function(colour,index) { + if(!colour) { + colour = $tw.utils.parseCSSColorObject("white"); + colours[index] = colour; + } + if(index > 0) { + colourContrasts.push(colour.contrast(colours[index - 1],"DeltaPhi").toString()); + } + }); + return colourContrasts; +}); + +function makeSerialColourOperator(fn) { return function (source, operator, options) { var results = []; source(function (tiddler, title) { @@ -46,4 +60,14 @@ function makeColourOperator(fn) { }; } +function makeParallelColourOperator(fn) { + return function (source, operator, options) { + var colours = []; + source(function (tiddler, title) { + colours.push($tw.utils.parseCSSColorObject(title)); + }); + return fn(colours, operator, options); + }; +} + })(); diff --git a/editions/test/tiddlers/tests/data/operators/colour/ColourContrast.tid b/editions/test/tiddlers/tests/data/operators/colour/ColourContrast.tid new file mode 100644 index 000000000..5aa5281b3 --- /dev/null +++ b/editions/test/tiddlers/tests/data/operators/colour/ColourContrast.tid @@ -0,0 +1,17 @@ +title: Operators/Colour/ColourContrast +description: 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]] +[colour-contrast:DeltaPhi[]trunc[]] ++ +title: ExpectedResult + +35 \ No newline at end of file