2014-11-06 13:35:48 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/macros/contrastcolour.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: macro
|
|
|
|
|
|
|
|
Macro to choose which of two colours has the highest contrast with a base colour
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Information about this macro
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.name = "contrastcolour";
|
|
|
|
|
|
|
|
exports.params = [
|
|
|
|
{name: "target"},
|
|
|
|
{name: "fallbackTarget"},
|
|
|
|
{name: "colourA"},
|
|
|
|
{name: "colourB"}
|
|
|
|
];
|
|
|
|
|
|
|
|
/*
|
|
|
|
Run the macro
|
|
|
|
*/
|
|
|
|
exports.run = function(target,fallbackTarget,colourA,colourB) {
|
2024-11-16 14:37:14 +00:00
|
|
|
var rgbTarget = $tw.utils.parseCSSColorObject(target) || $tw.utils.parseCSSColorObject(fallbackTarget);
|
2014-11-06 13:35:48 +00:00
|
|
|
if(!rgbTarget) {
|
|
|
|
return colourA;
|
|
|
|
}
|
2024-11-16 14:37:14 +00:00
|
|
|
var rgbColourA = $tw.utils.parseCSSColorObject(colourA),
|
|
|
|
rgbColourB = $tw.utils.parseCSSColorObject(colourB);
|
2015-04-21 21:07:16 +00:00
|
|
|
if(rgbColourA && !rgbColourB) {
|
2024-11-16 14:37:14 +00:00
|
|
|
return colourA;
|
2015-04-21 21:07:16 +00:00
|
|
|
}
|
2015-04-24 10:13:06 +00:00
|
|
|
if(rgbColourB && !rgbColourA) {
|
2024-11-16 14:37:14 +00:00
|
|
|
return colourB;
|
2015-04-21 21:07:16 +00:00
|
|
|
}
|
|
|
|
if(!rgbColourA && !rgbColourB) {
|
|
|
|
// If neither colour is readable, return a crude inverse of the target
|
2024-11-16 14:37:14 +00:00
|
|
|
rgbTarget.srgb.r = 1 - rgbTarget.srgb.r;
|
|
|
|
rgbTarget.srgb.g = 1 - rgbTarget.srgb.g;
|
|
|
|
rgbTarget.srgb.b = 1 - rgbTarget.srgb.b;
|
|
|
|
return rgbTarget.display();
|
2015-04-21 21:07:16 +00:00
|
|
|
}
|
2024-11-16 14:37:14 +00:00
|
|
|
var aContrast = rgbColourA.contrast(rgbTarget,"DeltaPhi"),
|
|
|
|
bContrast = rgbColourB.contrast(rgbTarget,"DeltaPhi");
|
|
|
|
return aContrast > bContrast ? colourA : colourB;
|
2014-11-06 13:35:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|