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

Add colour-lighten and colour-darken operators

This commit is contained in:
Jeremy Ruston 2024-10-24 12:01:50 +01:00
parent 96b85edfa2
commit d372729ed0
4 changed files with 98 additions and 7 deletions

View File

@ -0,0 +1,41 @@
/*\
title: $:/core/modules/filters/colour-ops.js
type: application/javascript
module-type: filteroperator
Filter operators for colour operations
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var Color = require("$:/core/modules/utils/dom/color.js").Color;
exports["colour-lighten"] = makeColourOperator(function (c, operator, operands) {
return c.lighten($tw.utils.parseNumber(operator.operand));
});
exports["colour-darken"] = makeColourOperator(function (c, operator, operands) {
return c.darken($tw.utils.parseNumber(operator.operand));
});
function makeColourOperator(fn) {
return function (source, operator, options) {
var results = [];
source(function (tiddler, title) {
var c = $tw.utils.parseCSSColorObject(title);
if (c) {
c = fn(c, operator, options);
results.push(c.display().toString());
} else {
results.push("");
}
});
return results;
};
}
})();

View File

@ -12,14 +12,13 @@ Color.js related utilities
/*global $tw: false */
"use strict";
var Color = require("$:/core/modules/utils/dom/color.js").Color;
/*
For backwards compatibility
*/
exports.parseCSSColor = function(colourString) {
var Color = require("$:/core/modules/utils/dom/color.js").Color,
c = null;
try {
c = new Color(colourString);
} catch(e) {
// Do nothing on an error
}
var c = exports.parseCSSColorObject(colourString);
if(c) {
var rgb = c.srgb;
return [rgb[0],rgb[1],rgb[2],c.alpha];
@ -28,4 +27,19 @@ 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;
try {
c = new Color(colourString);
} catch(e) {
// Return null if there is an error
}
return c;
};
})();

View File

@ -0,0 +1,18 @@
title: Operators/Colour/ColourDarken
description: Darken colour function
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\parsermode inline
\import [subfilter{$:/core/config/GlobalImportFilter}]
<$text text={{{ [subfilter{Filter}] }}}/>
+
title: Filter
[function[colour],[primary]colour-darken[0.5]]
+
title: ExpectedResult
#0f1d77

View File

@ -0,0 +1,18 @@
title: Operators/Colour/ColourLighten
description: Lighten colour function
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\parsermode inline
\import [subfilter{$:/core/config/GlobalImportFilter}]
<$text text={{{ [subfilter{Filter}] }}}/>
+
title: Filter
[function[colour],[primary]colour-lighten[0.5]]
+
title: ExpectedResult
#c6dbff