color-combos
Version:
Get accessibility information about colour combinations
128 lines (126 loc) • 3.98 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
default: () => src_default
});
module.exports = __toCommonJS(src_exports);
var import_lodash = __toESM(require("lodash.uniq"));
var import_color = __toESM(require("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) => (0, import_color.default)(colors[key]));
if (combinedOptions.uniq) {
arr = (0, import_lodash.default)(arr);
}
} else {
console.error("Must provide an array or object");
return false;
}
} else {
let uniqueColors = colors;
if (combinedOptions.uniq) {
uniqueColors = (0, import_lodash.default)(colors);
}
if (uniqueColors !== void 0) {
arr = uniqueColors.map((color) => (0, import_color.default)(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;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});