added contrastYIQ util

This commit is contained in:
SrGooglo 2022-12-20 14:46:16 +00:00
parent 2399ebc1fc
commit 4a8fbdf20a
2 changed files with 33 additions and 0 deletions

View File

@ -46,6 +46,7 @@
"electron-log": "^4.4.8",
"electron-squirrel-startup": "^1.0.0",
"evite": "0.13.7",
"fast-average-color": "^9.2.0",
"faye": "1.4.0",
"feather-reactjs": "2.0.13",
"fuse.js": "6.5.3",

View 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)
}
}