* Convert a colour to a normalised RGBA format * Required due to the different color formats * returned by different web drivers. * * Examples: * rgb(85,0,0) => rgba(85, 0, 0, 1) * * @param {string} color Color as a string, i.e. rgb(85,0,0)
(color)
| 190 | * @param {string} color Color as a string, i.e. rgb(85,0,0) |
| 191 | */ |
| 192 | function convertColorToRGBA(color) { |
| 193 | const cstr = `${color}`.toLowerCase().trim() || '' |
| 194 | |
| 195 | if (!/^rgba?\(.+?\)$/.test(cstr)) { |
| 196 | // Convert both color names and hex colors to rgba |
| 197 | const hexColor = convertColorNameToHex(color) |
| 198 | return convertHexColorToRgba(hexColor) |
| 199 | } |
| 200 | |
| 201 | // Convert rgb to rgba |
| 202 | const channels = cstr.match(/([\-0-9.]+)/g) || [] |
| 203 | |
| 204 | switch (channels.length) { |
| 205 | case 3: |
| 206 | // Convert rgb to rgba |
| 207 | return `rgba(${channels.join(', ')}, 1)` |
| 208 | |
| 209 | case 4: |
| 210 | // Format rgba |
| 211 | return `rgba(${channels.join(', ')})` |
| 212 | |
| 213 | default: |
| 214 | // Unexpected color format. Leave it untouched (let the user handle it) |
| 215 | return color |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Test if the given css property name is one that holds color information |
no test coverage detected