1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-25 00:16:52 +00:00

Add best contrast operator

This commit is contained in:
Jeremy Ruston 2025-01-07 17:40:23 +00:00
parent c02c82557b
commit 5d1cf251b9
2 changed files with 45 additions and 2 deletions

View File

@ -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<colours.length; t++) {
var colour = colours[t];
if(colour) {
var contrast = colour.contrast(targetColour,"DeltaPhi");
if(contrast > 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);
};
}

View File

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