MCPcopy Create free account
hub / github.com/Linen-dev/linen.dev / pickTextColorBasedOnBgColor

Function pickTextColorBasedOnBgColor

packages/utilities/src/colors.ts:1–19  ·  view source on GitHub ↗
(
  bgColor: string,
  lightColor: string,
  darkColor: string
)

Source from the content-addressed store, hash-verified

1export const pickTextColorBasedOnBgColor = (
2 bgColor: string,
3 lightColor: string,
4 darkColor: string
5) => {
6 var color = bgColor.charAt(0) === '#' ? bgColor.substring(1, 7) : bgColor;
7 var r = parseInt(color.substring(0, 2), 16); // hexToR
8 var g = parseInt(color.substring(2, 4), 16); // hexToG
9 var b = parseInt(color.substring(4, 6), 16); // hexToB
10 var uicolors = [r / 255, g / 255, b / 255];
11 var c = uicolors.map((col) => {
12 if (col <= 0.03928) {
13 return col / 12.92;
14 }
15 return Math.pow((col + 0.055) / 1.055, 2.4);
16 });
17 var L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
18 return L > 0.179 ? darkColor : lightColor;
19};
20
21// We have only 12 color schemes available right now
22// please note that this only includes letters of the English alphabet

Callers 3

CommunityIconFunction · 0.90
fontColorFunction · 0.90
HeaderFunction · 0.90

Calls 1

mapMethod · 0.80

Tested by

no test coverage detected