@wordpress/components
Version:
UI components for WordPress.
102 lines (101 loc) • 3.43 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
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 __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, 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));
let colord = require("colord");
let colord_plugins_names = require("colord/plugins/names");
colord_plugins_names = __toESM(colord_plugins_names);
// packages/components/src/duotone-picker/utils.ts
(0, colord.extend)([colord_plugins_names.default]);
/**
* Object representation for a color.
*
* @typedef {Object} RGBColor
* @property {number} r Red component of the color in the range [0,1].
* @property {number} g Green component of the color in the range [0,1].
* @property {number} b Blue component of the color in the range [0,1].
*/
/**
* Calculate the brightest and darkest values from a color palette.
*
* @param palette Color palette for the theme.
*
* @return Tuple of the darkest color and brightest color.
*/
function getDefaultColors(palette) {
if (!palette || palette.length < 2) return ["#000", "#fff"];
return palette.map(({ color }) => ({
color,
brightness: (0, colord.colord)(color).brightness()
})).reduce(([min, max], current) => {
return [current.brightness <= min.brightness ? current : min, current.brightness >= max.brightness ? current : max];
}, [{
brightness: 1,
color: ""
}, {
brightness: 0,
color: ""
}]).map(({ color }) => color);
}
/**
* Generate a duotone gradient from a list of colors.
*
* @param colors CSS color strings.
* @param angle CSS gradient angle.
*
* @return CSS gradient string for the duotone swatch.
*/
function getGradientFromCSSColors(colors = [], angle = "90deg") {
const l = 100 / colors.length;
return `linear-gradient( ${angle}, ${colors.map((c, i) => `${c} ${i * l}%, ${c} ${(i + 1) * l}%`).join(", ")} )`;
}
/**
* Convert a color array to an array of color stops.
*
* @param colors CSS colors array
*
* @return Color stop information.
*/
function getColorStopsFromColors(colors) {
return colors.map((color, i) => ({
position: i * 100 / (colors.length - 1),
color
}));
}
/**
* Convert a color stop array to an array colors.
*
* @param colorStops Color stop information.
*
* @return CSS colors array.
*/
function getColorsFromColorStops(colorStops = []) {
return colorStops.map(({ color }) => color);
}
exports.getColorStopsFromColors = getColorStopsFromColors;
exports.getColorsFromColorStops = getColorsFromColorStops;
exports.getDefaultColors = getDefaultColors;
exports.getGradientFromCSSColors = getGradientFromCSSColors;
//# sourceMappingURL=utils.cjs.map