(hex: string)
| 260 | * Convert a hex color to an RGB color set. |
| 261 | */ |
| 262 | export 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. |
no outgoing calls
no test coverage detected