autohue.js
Version:
一个自动提取图片主题色让图片和背景融为一体的工具
41 lines (40 loc) • 1.42 kB
TypeScript
export interface AutoHueResult {
primaryColor: string;
secondaryColor: string;
backgroundColor: {
top: string;
right: string;
bottom: string;
left: string;
};
}
type thresholdObj = {
primary?: number;
left?: number;
right?: number;
top?: number;
bottom?: number;
};
interface autoColorPickerOptions {
/**
* - 降采样后的最大尺寸(默认 100px)
* - 降采样后的图片尺寸不会超过该值,可根据需求调整
* - 降采样后的图片尺寸越小,处理速度越快,但可能会影响颜色提取的准确性
**/
maxSize?: number;
/**
* - Lab 距离阈值(默认 10)
* - 低于此值的颜色归为同一簇,建议 8~12
* - 值越大,颜色越容易被合并,提取的颜色越少
* - 值越小,颜色越容易被区分,提取的颜色越多
* - 传入 number | { primary?: number; left?: number; right?: number; top?: number; bottom?: number }
**/
threshold?: number | thresholdObj;
}
/**
* 主函数:根据图片自动提取颜色
* @param imageSource 图片 URL 或 HTMLImageElement
* @returns 返回包含主要颜色、次要颜色和背景色对象(上、右、下、左)的结果
*/
export default function colorPicker(imageSource: HTMLImageElement | string, options?: autoColorPickerOptions): Promise<AutoHueResult>;
export {};