Guard colour operators against achromatic hues and unparseable target colours

This commit is contained in:
Jeremy Ruston
2026-07-19 15:57:28 +01:00
parent 81d7283990
commit 1103b733fe
3 changed files with 44 additions and 0 deletions
+7
View File
@@ -25,6 +25,10 @@ exports["colour-get-oklch"] = makeSerialColourOperator(function (colour, operato
var prop = ((operator.suffixes || [])[0] || ["l"])[0];
if(["l","c","h"].indexOf(prop) !== -1) {
colour = colour.oklch[prop];
// Hue is null for achromatic colours
if(colour === null || colour === undefined || Number.isNaN(colour)) {
return "";
}
}
return colour.toString();
});
@@ -63,6 +67,9 @@ exports["colour-best-contrast"] = makeParallelColourOperator(function (colours,
return [];
}
var targetColour = colours[colours.length - 1];
if(!targetColour) {
return [];
}
for(var t=0; t<colours.length; t++) {
var colour = colours[t];
if(colour) {
@@ -0,0 +1,17 @@
title: Operators/Colour/ColourBestContrastInvalidTarget
description: colour-best-contrast with an unparseable target colour returns nothing instead of crashing
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\parsermode inline
<$text text={{{ [subfilter{Filter}] +[count[]] }}}/>
+
title: Filter
[[red]] [[blue]] [[notacolour]] +[colour-best-contrast[]]
+
title: ExpectedResult
0
@@ -0,0 +1,20 @@
title: Operators/Colour/ColourGetOklchAchromatic
description: colour-get-oklch hue of achromatic colours returns empty instead of crashing
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\parsermode inline
<$text text={{{ [subfilter{Filter}] }}}/>
+
title: Filter
=[[white]colour-get-oklch:h[]]
=[[#808080]colour-get-oklch:h[]]
=[[red]colour-get-oklch:h[]fixed[3]]
+[join[,]]
+
title: ExpectedResult
,,29.234