1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-26 19:47:20 +00:00

Add colour-contrast operator

This commit is contained in:
Jeremy Ruston 2024-11-18 18:15:20 +00:00
parent 76f2decf8b
commit aa69c3ae91
2 changed files with 45 additions and 4 deletions

View File

@ -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);
};
}
})();

View File

@ -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