@macrostrat/column-components
Version:
React rendering primitives for stratigraphic columns
31 lines (30 loc) • 1.2 kB
JavaScript
;
const Color = require("../Color.cjs");
const mix = require("../generator/mix.cjs");
const W_offset = 0.027;
const P_in = 5e-4;
const P_out = 0.1;
const R_scale = 1.14;
const B_threshold = 0.022;
const B_exp = 1.414;
const contrastAPCA = (text, bg) => {
text = new Color(text);
bg = new Color(bg);
if (text.alpha() < 1) {
text = mix(bg, text, text.alpha(), "rgb");
}
const l_text = lum(...text.rgb());
const l_bg = lum(...bg.rgb());
const Y_text = l_text >= B_threshold ? l_text : l_text + Math.pow(B_threshold - l_text, B_exp);
const Y_bg = l_bg >= B_threshold ? l_bg : l_bg + Math.pow(B_threshold - l_bg, B_exp);
const S_norm = Math.pow(Y_bg, 0.56) - Math.pow(Y_text, 0.57);
const S_rev = Math.pow(Y_bg, 0.65) - Math.pow(Y_text, 0.62);
const C = Math.abs(Y_bg - Y_text) < P_in ? 0 : Y_text < Y_bg ? S_norm * R_scale : S_rev * R_scale;
const S_apc = Math.abs(C) < P_out ? 0 : C > 0 ? C - W_offset : C + W_offset;
return S_apc * 100;
};
function lum(r, g, b) {
return 0.2126729 * Math.pow(r / 255, 2.4) + 0.7151522 * Math.pow(g / 255, 2.4) + 0.072175 * Math.pow(b / 255, 2.4);
}
module.exports = contrastAPCA;
//# sourceMappingURL=contrastAPCA.cjs.map