"use strict";
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 __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(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-library/src/cover/edit/color-utils.js
var color_utils_exports = {};
__export(color_utils_exports, {
  DEFAULT_BACKGROUND_COLOR: () => DEFAULT_BACKGROUND_COLOR,
  DEFAULT_OVERLAY_COLOR: () => DEFAULT_OVERLAY_COLOR,
  compositeIsDark: () => compositeIsDark,
  compositeSourceOver: () => compositeSourceOver,
  getMediaColor: () => getMediaColor,
  retrieveFastAverageColor: () => retrieveFastAverageColor
});
module.exports = __toCommonJS(color_utils_exports);
var import_colord = require("colord");
var import_names = __toESM(require("colord/plugins/names"));
var import_fast_average_color = require("fast-average-color");
var import_memize = __toESM(require("memize"));
var import_hooks = require("@wordpress/hooks");
(0, import_colord.extend)([import_names.default]);
var DEFAULT_BACKGROUND_COLOR = "#FFF";
var DEFAULT_OVERLAY_COLOR = "#000";
function compositeSourceOver(source, dest) {
  return {
    r: source.r * source.a + dest.r * dest.a * (1 - source.a),
    g: source.g * source.a + dest.g * dest.a * (1 - source.a),
    b: source.b * source.a + dest.b * dest.a * (1 - source.a),
    a: source.a + dest.a * (1 - source.a)
  };
}
function retrieveFastAverageColor() {
  if (!retrieveFastAverageColor.fastAverageColor) {
    retrieveFastAverageColor.fastAverageColor = new import_fast_average_color.FastAverageColor();
  }
  return retrieveFastAverageColor.fastAverageColor;
}
var getMediaColor = (0, import_memize.default)(async (url) => {
  if (!url) {
    return DEFAULT_BACKGROUND_COLOR;
  }
  const { r, g, b, a } = (0, import_colord.colord)(DEFAULT_BACKGROUND_COLOR).toRgb();
  try {
    const imgCrossOrigin = (0, import_hooks.applyFilters)(
      "media.crossOrigin",
      void 0,
      url
    );
    const color = await retrieveFastAverageColor().getColorAsync(url, {
      // The default color is white, which is the color
      // that is returned if there's an error.
      // colord returns alpga 0-1, FAC needs 0-255
      defaultColor: [r, g, b, a * 255],
      // Errors that come up don't reject the promise,
      // so error logging has to be silenced
      // with this option.
      silent: process.env.NODE_ENV === "production",
      crossOrigin: imgCrossOrigin
    });
    return color.hex;
  } catch {
    return DEFAULT_BACKGROUND_COLOR;
  }
});
function compositeIsDark(dimRatio, overlayColor, backgroundColor) {
  if (overlayColor === backgroundColor || dimRatio === 100) {
    return (0, import_colord.colord)(overlayColor).isDark();
  }
  const overlay = (0, import_colord.colord)(overlayColor).alpha(dimRatio / 100).toRgb();
  const background = (0, import_colord.colord)(backgroundColor).toRgb();
  const composite = compositeSourceOver(overlay, background);
  return (0, import_colord.colord)(composite).isDark();
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DEFAULT_BACKGROUND_COLOR,
  DEFAULT_OVERLAY_COLOR,
  compositeIsDark,
  compositeSourceOver,
  getMediaColor,
  retrieveFastAverageColor
});
//# sourceMappingURL=color-utils.cjs.map
