mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
added contrastYIQ
util
This commit is contained in:
parent
4f53ec4d54
commit
6df0ad7938
@ -46,6 +46,7 @@
|
|||||||
"electron-log": "^4.4.8",
|
"electron-log": "^4.4.8",
|
||||||
"electron-squirrel-startup": "^1.0.0",
|
"electron-squirrel-startup": "^1.0.0",
|
||||||
"evite": "0.13.7",
|
"evite": "0.13.7",
|
||||||
|
"fast-average-color": "^9.2.0",
|
||||||
"faye": "1.4.0",
|
"faye": "1.4.0",
|
||||||
"feather-reactjs": "2.0.13",
|
"feather-reactjs": "2.0.13",
|
||||||
"fuse.js": "6.5.3",
|
"fuse.js": "6.5.3",
|
||||||
|
32
packages/app/src/utils/contrastYIQ/index.js
Normal file
32
packages/app/src/utils/contrastYIQ/index.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { FastAverageColor } from "fast-average-color"
|
||||||
|
|
||||||
|
export default class ContrastYIQ {
|
||||||
|
static get facInstance() {
|
||||||
|
return new FastAverageColor()
|
||||||
|
}
|
||||||
|
|
||||||
|
static async averageColor(image) {
|
||||||
|
return await ContrastYIQ.facInstance.getColorAsync(image)
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromHex(hexcolor) {
|
||||||
|
const r = parseInt(hexcolor.substring(1, 3), 16)
|
||||||
|
const g = parseInt(hexcolor.substring(3, 5), 16)
|
||||||
|
const b = parseInt(hexcolor.substring(5, 7), 16)
|
||||||
|
|
||||||
|
const yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000
|
||||||
|
|
||||||
|
return (yiq >= 128) ? "black" : "white"
|
||||||
|
}
|
||||||
|
|
||||||
|
static async fromUrl(url) {
|
||||||
|
const image = new Image()
|
||||||
|
|
||||||
|
image.src = url + "?" + new Date().getTime()
|
||||||
|
image.setAttribute("crossOrigin", "")
|
||||||
|
|
||||||
|
const results = await ContrastYIQ.averageColor(image)
|
||||||
|
|
||||||
|
return ContrastYIQ.fromHex(results.hex)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user