From cfabc92945589b50c87b127d1bc688b3cd1af43d Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sat, 16 Nov 2024 14:37:14 +0000 Subject: [PATCH] Update contrastcolour macro to use color.js --- core/modules/macros/contrastcolour.js | 23 ++++++++++--------- core/modules/utils/dom/color-utils.js | 3 +-- .../contrastcolour/ContrastColourBasic.tid | 12 ++++++++++ 3 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 editions/test/tiddlers/tests/data/macros/contrastcolour/ContrastColourBasic.tid diff --git a/core/modules/macros/contrastcolour.js b/core/modules/macros/contrastcolour.js index eab6f0108..6d38993f0 100644 --- a/core/modules/macros/contrastcolour.js +++ b/core/modules/macros/contrastcolour.js @@ -29,27 +29,28 @@ exports.params = [ Run the macro */ exports.run = function(target,fallbackTarget,colourA,colourB) { - var rgbTarget = $tw.utils.parseCSSColor(target) || $tw.utils.parseCSSColor(fallbackTarget); + var rgbTarget = $tw.utils.parseCSSColorObject(target) || $tw.utils.parseCSSColorObject(fallbackTarget); if(!rgbTarget) { return colourA; } - var rgbColourA = $tw.utils.parseCSSColor(colourA), - rgbColourB = $tw.utils.parseCSSColor(colourB); + var rgbColourA = $tw.utils.parseCSSColorObject(colourA), + rgbColourB = $tw.utils.parseCSSColorObject(colourB); if(rgbColourA && !rgbColourB) { - return rgbColourA; + return colourA; } if(rgbColourB && !rgbColourA) { - return rgbColourB; + return colourB; } if(!rgbColourA && !rgbColourB) { // If neither colour is readable, return a crude inverse of the target - return [255 - rgbTarget[0],255 - rgbTarget[1],255 - rgbTarget[2],rgbTarget[3]]; + rgbTarget.srgb.r = 1 - rgbTarget.srgb.r; + rgbTarget.srgb.g = 1 - rgbTarget.srgb.g; + rgbTarget.srgb.b = 1 - rgbTarget.srgb.b; + return rgbTarget.display(); } - // Colour brightness formula derived from http://www.w3.org/WAI/ER/WD-AERT/#color-contrast - var brightnessTarget = rgbTarget[0] * 0.299 + rgbTarget[1] * 0.587 + rgbTarget[2] * 0.114, - brightnessA = rgbColourA[0] * 0.299 + rgbColourA[1] * 0.587 + rgbColourA[2] * 0.114, - brightnessB = rgbColourB[0] * 0.299 + rgbColourB[1] * 0.587 + rgbColourB[2] * 0.114; - return Math.abs(brightnessTarget - brightnessA) > Math.abs(brightnessTarget - brightnessB) ? colourA : colourB; + var aContrast = rgbColourA.contrast(rgbTarget,"DeltaPhi"), + bContrast = rgbColourB.contrast(rgbTarget,"DeltaPhi"); + return aContrast > bContrast ? colourA : colourB; }; })(); diff --git a/core/modules/utils/dom/color-utils.js b/core/modules/utils/dom/color-utils.js index 74e533a37..475632418 100644 --- a/core/modules/utils/dom/color-utils.js +++ b/core/modules/utils/dom/color-utils.js @@ -32,8 +32,7 @@ exports.parseCSSColor = function(colourString) { Preferred way to parse a Color.js colour */ exports.parseCSSColorObject = function(colourString) { - var Color = require("$:/core/modules/utils/dom/color.js").Color, - c = null; + var c = null; try { c = new Color(colourString); } catch(e) { diff --git a/editions/test/tiddlers/tests/data/macros/contrastcolour/ContrastColourBasic.tid b/editions/test/tiddlers/tests/data/macros/contrastcolour/ContrastColourBasic.tid new file mode 100644 index 000000000..13940f3ed --- /dev/null +++ b/editions/test/tiddlers/tests/data/macros/contrastcolour/ContrastColourBasic.tid @@ -0,0 +1,12 @@ +title: Macros/ContrastColour/ContrastColourBasic +description: Basic usage of contrastcolour macro +type: text/vnd.tiddlywiki-multiple +tags: [[$:/tags/wiki-test-spec]] + +title: Output + +Colour: <> ++ +title: ExpectedResult + +

Colour: #ffffff

\ No newline at end of file