MCPcopy Index your code
hub / github.com/codeceptjs/CodeceptJS / convertHexColorToRgba

Function convertHexColorToRgba

lib/colorUtils.js:161–180  ·  view source on GitHub ↗

* Convert a CSS hex color code (#00EE00) to rgba() format * @param {string} hex hex color code (#00EE00). Short form is also accepted i.e. #03F for #0033FF)

(hex)

Source from the content-addressed store, hash-verified

159 * @param {string} hex hex color code (#00EE00). Short form is also accepted i.e. #03F for #0033FF)
160 */
161function convertHexColorToRgba(hex) {
162 // Expand shorthand form (e.g. "#03F") to full form (e.g. "#0033FF")
163 const shorthandRegex = /^#([a-f\d])([a-f\d])([a-f\d])$/i
164 const hexFull = `${hex}`.replace(shorthandRegex, (m, r, g, b) => {
165 return r + r + g + g + b + b
166 })
167
168 const result = /^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexFull)
169 if (!result) {
170 // Return untouched if not a hex code
171 return hex
172 }
173
174 const r = parseInt(result[1], 16)
175 const g = parseInt(result[2], 16)
176 const b = parseInt(result[3], 16)
177 const a = 1
178
179 return `rgba(${r}, ${g}, ${b}, ${a})`
180}
181
182/**
183 * Convert a colour to a normalised RGBA format

Callers 1

convertColorToRGBAFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected