@dominate-color.js/core

中文文档
1.1.1发布于 2024/10/14MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 3989

A library that can extract the primary color and its hue in RGBA format and convert it.

原始使用文档

@dominate-color.js/core

core-npm-badge Size code-badge

A robust TypeScript library for color detection in images. The @dominate-color.js/core package provides essential functionality to identify and extract dominant colors from an image source. The library is built to cater to various use cases, from creating color-based aesthetic layouts to implementing dynamic theming based on image colors in your applications.

colorDetection()

The colorDetection function is a cornerstone of the @dominate-color.js/core package, enabling developers to easily detect colors in an image. It accepts an image source and optional configuration parameters, returning a promise that resolves with an array of the most dominant RGBA colors found in the image.

Features
  • Supports various image sources, including URLs, file paths, and buffers.
  • Offers configurable settings to fine-tune color detection.
  • Utilizes efficient algorithms for fast and accurate color extraction.
Getting Started

Before integrating colorDetection into your project, it is advisable to go through the provided documentation to understand how to use the function effectively. This function is designed to be straightforward and flexible, allowing for easy adaptation to your specific needs.

Installation

First, install the @dominate-color.js/core package using npm, pnpm or yarn:

npm install @dominate-color.js/core
# or
yarn add @dominate-color.js/core
Usage

To use colorDetection() in your project, follow these steps:

  1. Import the colorDetection function from the package:
import { colorDetection } from '@dominate-color.js/core';
  1. Prepare your image source. This can be a URL, a local file path, or an ArrayBuffer.
  2. (Optional) Set up your configuration options. You can specify the distance method, the number k of colors to detect, and AccessControlAllowOrigin for CORS settings.
  3. Call colorDetection() with your image source and optional configuration:
    import { colorDetection, type Config } from '@dominate-color.js/core';
     
    const config: Config = {
        distance: 'euclidean', // Choose the 'euclidean' algorithm for color distance calculation
        k: 5, // Detect the top 5 dominant colors
        AccessControlAllowOrigin: '*' // Useful for CORS when fetching images from external URLs
    };

    // Replace `source.url` with the path to your image or any other type supported by the `colorDetection` function when calling it.
    colorDetection(source.url, config)
        .then(colors => {
            console.log('Detected Colors with custom config:', colors);
            updateUIWithColors(colors);
        })
        .catch(error => {
            console.error('Error detecting colors with custom config:', error);
            warnUserSomethingWentWrong(error)
        });

版本与发布记录

@dominate-color.js/core 共发布 3 个版本,首次发布于 2023/12/04;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.1latest2024/10/142 年前
1.1.02023/12/172 年前
1.0.02023/12/042 年前

关于 @dominate-color.js/core 的常见问题

@dominate-color.js/core 是什么?

A library that can extract the primary color and its hue in RGBA format and convert it.

如何安装 @dominate-color.js/core?

在项目目录运行 npm install @dominate-color.js/core,或使用 pnpm add / yarn add / bun add。

@dominate-color.js/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dominate-color.js/core 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dominate-color.js/core 可以商用吗?

@dominate-color.js/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dominate-color.js/core 的下载量有多少?

根据 npm 官方下载统计,@dominate-color.js/core 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,989 次,日均约 133 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dominate-color.js/core 还在维护吗?

最新版本 1.1.1 发布于 2024/10/14(2 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/12/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。