color-combos
Version:
Get accessibility information about colour combinations
101 lines (99 loc) • 2.61 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/index.ts
import uniq from "lodash.uniq";
import Color from "color";
var ColorCombos = /* @__PURE__ */ __name((colors, options = {}) => {
let arr = [];
let results = [];
const MINIMUMS = {
aa: 4.5,
aaLarge: 3,
aaa: 7,
aaaLarge: 4.5
};
const DEFAULT_OPTIONS = {
threshold: 0,
compact: false,
uniq: true
};
const combinedOptions = Object.assign(DEFAULT_OPTIONS, options);
if (!Array.isArray(colors)) {
if (typeof colors === "object") {
arr = Object.keys(colors).map((key) => Color(colors[key]));
if (combinedOptions.uniq) {
arr = uniq(arr);
}
} else {
console.error("Must provide an array or object");
return false;
}
} else {
let uniqueColors = colors;
if (combinedOptions.uniq) {
uniqueColors = uniq(colors);
}
if (uniqueColors !== void 0) {
arr = uniqueColors.map((color) => Color(color));
}
}
results = arr.map((color) => {
const result = combinedOptions.compact ? {
hex: "",
combinations: []
} : {
color: color.color,
model: color.model,
valpha: color.valpha,
hex: "",
combinations: []
};
result.hex = color.hex();
result.combinations = arr.filter((bg) => color !== bg).filter((bg) => {
if (combinedOptions.threshold !== void 0) {
return color.contrast(bg) > combinedOptions.threshold;
}
return true;
}).map((bg) => {
let combination = combinedOptions.compact ? {
accessibility: {
aa: false,
aaLarge: false,
aaa: false,
aaaLarge: false
},
hex: "",
contrast: 0
} : {
accessibility: {
aa: false,
aaLarge: false,
aaa: false,
aaaLarge: false
},
hex: "",
contrast: 0,
color: bg.color,
model: bg.model,
valpha: bg.valpha
};
combination = Object.assign(combination, {
hex: bg.hex(),
contrast: color.contrast(bg)
});
combination.accessibility = {
aa: combination.contrast >= MINIMUMS.aa,
aaLarge: combination.contrast >= MINIMUMS.aaLarge,
aaa: combination.contrast >= MINIMUMS.aaa,
aaaLarge: combination.contrast >= MINIMUMS.aaaLarge
};
return combination;
});
return result;
});
return results;
}, "ColorCombos");
var src_default = ColorCombos;
export {
src_default as default
};