MCPcopy
hub / github.com/GitbookIO/gitbook / hexToRgbArray

Function hexToRgbArray

packages/colors/src/transformations.ts:262–283  ·  view source on GitHub ↗
(hex: string)

Source from the content-addressed store, hash-verified

260 * Convert a hex color to an RGB color set.
261 */
262export function hexToRgbArray(hex: string): RGBColor {
263 const originalHex = hex;
264
265 let value = hex.replace('#', '');
266 if (hex.length === 3) value = value + value;
267
268 const r = value.substring(0, 2);
269 const g = value.substring(2, 4);
270 const b = value.substring(4, 6);
271
272 const rgb = [r, g, b].map((channel) => {
273 try {
274 const channelInt = Number.parseInt(channel, 16);
275 if (channelInt < 0 || channelInt > 255) throw new Error();
276 return channelInt;
277 } catch {
278 throw new Error(`Invalid hex color provided: ${originalHex}`);
279 }
280 });
281
282 return rgb as RGBColor;
283}
284
285/**
286 * Convert a RGB color set to a hex color.

Callers 5

hexToRgbFunction · 0.85
hexToRgbaFunction · 0.85
shadesOfColorFunction · 0.85
colorScaleFunction · 0.85
colorContrastFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected