推荐一款个人开源的图片色值处理工具 image-color-utils
image-color-utils
DESC
提供取色
、色值相似度对比
、色彩边界值计算
等能力。
demo
codesandbox
Install
npm install image-color-utils --save
Usage
es
import { ImageColorUtils } from 'image-color-utils'
commonjs
const { ImageColorUtils } = require('image-color-utils')
API
\# ImageColorUtils
import { ImageColorUtils } from 'image-color-utils'
const params = {
origin: img,
width: canvas.width,
height: canvas.height,
boundaryValue,
mockMovePx
}
const imageColorUtils = new ImageColorUtils(params)
Arguments
Name | Desc | Type | Default | required |
---|---|---|---|---|
origin | 数据源(可以是 http链接 / ImageBitmap / HTMLImageElement ) | string / HTMLImageElement / ImageBitmap / | - | true |
width | 画板宽度 | number | - | false (不传参将根据图片宽高自适应,origin 为 ImageBitmap / HTMLImageElemen,必填) |
height | 画板高度 | number | - | false (不传参将根据图片宽高自适应,origin 为 ImageBitmap / HTMLImageElemen,必填) |
mockMovePx | 边界扫描距离(最大边界扫描距离, 扫描方向由内向外) | number | 30 | false |
boundaryValue | 色彩边界阈值(作用于色值相似度对比, 阈值越高,相似条件越高) | number | 10 | false |
Returns
Desc | Type |
---|---|
ImageColorUtils实例 | Object |
\# pickColor - 提取色值
import { ImageColorUtils } from 'image-color-utils'
const imageColorUtils = new ImageColorUtils({
origin: img,
width: canvas.width,
height: canvas.height
})
const res = imageColorUtils.pickColor(x, y)
Arguments
Name | Desc | Type | Default | required |
---|---|---|---|---|
x | 目标点距离画布左上角x坐标 | number | - | true |
y | 目标点距离画布左上角y坐标 | number | - | true |
Returns
Desc | Type |
---|---|
目标点 rgb 色值 | number[] |
\# adjust - 色彩边界值计算
import { ImageColorUtils } from 'image-color-utils'
const imageColorUtils = new ImageColorUtils({
origin: img,
width: canvas.width,
height: canvas.height,
boundaryValue,
mockMovePx
})
imageColorUtils.adjust(leftTopPosition, rightBottomPosition)
Arguments
Name | Desc | Type | Default | required |
---|---|---|---|---|
leftTopPosition | 图片所选区域初始左上角坐标 | number[] | [] | false |
rightBottomPosition | 图片所选区域初始右下角坐标 | number[] | [] | false |
Returns
Desc | Type |
---|---|
边界计算后左上角坐标(x,y)及区域宽高(width,height) | Object:{x: number, y: number, width: number, height: number} |
\# compare - 色值相似度对比
import { ImageColorUtils } from 'image-color-utils'
const res = ImageColorUtils.compare(color1, color2, boundaryValue)
Arguments
Name | Desc | Type | Default | required |
---|---|---|---|---|
color1 | rgb 色值1 | number[] | - | true |
color2 | rgb 色值2 | number[] | - | true |
boundaryValue | 色彩边界阈值(作用于色值相似度对比, 阈值越高,相似条件越高) | number | 10 | false |
Returns
Desc | Type |
---|---|
是否相似 | boolean |
\# hex2rgb - HEX色值转RGB色值
import { ImageColorUtils } from 'image-color-utils'
const rgb = ImageColorUtils.hex2rgb(hex)
Arguments
Name | Desc | Type | Default | required |
---|---|---|---|---|
hex | HEX色值 | String | - | true |
Returns
Desc | Type |
---|---|
RGB色值 | number[] |
\# rgb2hex - RGB色值转HEX色值
import { ImageColorUtils } from 'image-color-utils'
const hex = ImageColorUtils.rgb2hex(rgb)
Arguments
Name | Desc | Type | Default | required |
---|---|---|---|---|
rgb | RGB色值 | number[] | - | true |
Returns
Desc | Type |
---|---|
HEX色值 | string |
Attribute
import { ImageColorUtils } from 'image-color-utils'
const imageColorUtils = new ImageColorUtils({
origin: img,
width: canvas.width,
height: canvas.height,
boundaryValue,
mockMovePx
})
console.log(imageColorUtils.canvas)
console.log(imageColorUtils.ctx)
console.log(imageColorUtils.imageData)
Name | Type |
---|---|
canvas | OffscreenCanvas |
ctx | OffscreenCanvasRenderingContext2D |
imageData | ImageData |
Devin前端技术分享
前端技术专栏
30 声望
2 粉丝
推荐阅读
route-resource-preload 拆包,尽最大努力减少首屏幕加载资源,并提供优质的用户体验
route-resource-preload🚀 专注于不影响用户最佳体验的同时,提升应用首屏加载速度,灵感来自NextJS的预加载.gif 效果图正常懒加载.gifroute-resource-preload 预加载.gif从上图可以发现,route-resource-preload ...
daivon阅读 98
手把手教你写一份优质的前端技术简历
不知不觉一年一度的秋招又来了,你收获了哪些大厂的面试邀约,又拿了多少offer呢?你身边是不是有挺多人技术比你差,但是却拿到了很多大厂的offer呢?其实,要想面试拿offer,首先要过得了简历那一关。如果一份简...
tonychen赞 152阅读 17.7k评论 5
正则表达式实例
收集在业务中经常使用的正则表达式实例,方便以后进行查找,减少工作量。常用正则表达式实例1. 校验基本日期格式 {代码...} {代码...} 2. 校验密码强度密码的强度必须是包含大小写字母和数字的组合,不能使用特殊...
寒青赞 56阅读 8.5k评论 11
JavaScript有用的代码片段和trick
平时工作过程中可以用到的实用代码集棉。判断对象否为空 {代码...} 浮点数取整 {代码...} 注意:前三种方法只适用于32个位整数,对于负数的处理上和Math.floor是不同的。 {代码...} 生成6位数字验证码 {代码...} ...
jenemy赞 48阅读 7k评论 12
从零搭建 Node.js 企业级 Web 服务器(十五):总结与展望
总结截止到本章 “从零搭建 Node.js 企业级 Web 服务器” 主题共计 16 章内容就更新完毕了,回顾第零章曾写道:搭建一个 Node.js 企业级 Web 服务器并非难事,只是必须做好几个关键事项这几件必须做好的关键事项就...
乌柏木赞 75阅读 7.1k评论 16
再也不学AJAX了!(二)使用AJAX ① XMLHttpRequest
「再也不学 AJAX 了」是一个以 AJAX 为主题的系列文章,希望读者通过阅读本系列文章,能够对 AJAX 技术有更加深入的认识和理解,从此能够再也不用专门学习 AJAX。本篇文章为该系列的第二篇,最近更新于 2023 年 1...
libinfs赞 42阅读 6.8k评论 12
从零搭建 Node.js 企业级 Web 服务器(一):接口与分层
分层规范从本章起,正式进入企业级 Web 服务器核心内容。通常,一块完整的业务逻辑是由视图层、控制层、服务层、模型层共同定义与实现的,如下图:从上至下,抽象层次逐渐加深。从下至上,业务细节逐渐清晰。视图...
乌柏木赞 45阅读 8.5k评论 6
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。