MCPcopy Create free account
hub / github.com/codesandbox/sandpack / isDarkColor

Function isDarkColor

sandpack-react/src/utils/stringUtils.ts:81–110  ·  view source on GitHub ↗
(color: string)

Source from the content-addressed store, hash-verified

79
80// Checks both rgb and hex colors for contrast and returns true if the color is in the dark spectrum
81export const isDarkColor = (color: string): boolean => {
82 let r = 0;
83 let g = 0;
84 let b = 0;
85 if (color.startsWith("#")) {
86 if (color.length < 7) {
87 return true;
88 }
89
90 r = parseInt(color.substr(1, 2), 16);
91 g = parseInt(color.substr(3, 2), 16);
92 b = parseInt(color.substr(5, 2), 16);
93 } else {
94 const rgbValues = color
95 .replace("rgb(", "")
96 .replace("rgba(", "")
97 .replace(")", "")
98 .split(",");
99 if (rgbValues.length < 3) {
100 return true;
101 }
102
103 r = parseInt(rgbValues[0], 10);
104 g = parseInt(rgbValues[1], 10);
105 b = parseInt(rgbValues[2], 10);
106 }
107
108 const yiq = (r * 299 + g * 587 + b * 114) / 1000;
109 return yiq < 128;
110};
111
112// Minimal cuid-like id
113let lastCount = 0;

Callers 1

standardizeThemeFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected