add web app & add concurrency
This commit is contained in:
25
web/src/getContrastYIQ.ts
Normal file
25
web/src/getContrastYIQ.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
export function getContrastYIQ(hexcolor: string) {
|
||||
// Remove leading # if present
|
||||
if (hexcolor.startsWith("#")) {
|
||||
hexcolor = hexcolor.slice(1);
|
||||
}
|
||||
|
||||
// Handle 3-digit hex codes (e.g., #abc → #aabbcc)
|
||||
if (hexcolor.length === 3) {
|
||||
hexcolor = hexcolor
|
||||
.split("")
|
||||
.map((c) => c + c)
|
||||
.join("");
|
||||
}
|
||||
|
||||
// Parse RGB values
|
||||
const r = parseInt(hexcolor.substr(0, 2), 16);
|
||||
const g = parseInt(hexcolor.substr(2, 2), 16);
|
||||
const b = parseInt(hexcolor.substr(4, 2), 16);
|
||||
|
||||
// Calculate YIQ brightness
|
||||
const yiq = (r * 299 + g * 587 + b * 114) / 1000;
|
||||
|
||||
// Return black or white based on contrast
|
||||
return yiq >= 128 ? "black" : "white";
|
||||
}
|
||||
Reference in New Issue
Block a user