10 examples of 'convert hex to rgba' in JavaScript

Every line of 'convert hex to rgba' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
60export function hexToRgb(hex: string): Rgba {
61 // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
62 const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
63 hex = hex.replace(shorthandRegex, (m, r, g, b) => {
64 return r + r + g + g + b + b;
65 });
66
67 const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
68 return result ? new Rgba(
69 parseInt(result[1], 16),
70 parseInt(result[2], 16),
71 parseInt(result[3], 16)) : null;
72}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

47export function rgba2hex(rgba) {
48 const regex = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d+)\s*)?\)/;
49 const parsed = regex.exec(rgba);
50 if (!parsed) {
51 return false;
52 }
53 const red = parsed[1];
54 const green = parsed[2];
55 const blue = parsed[3];
56 const alpha = parsed[4];
57 const elems = [hex(red), hex(green), hex(blue)];
58 if (alpha) {
59 elems.push(hex(alpha));
60 }
61 return `#${elems.join('')}`;
62}
57static hex3ToRgb (color) {
58 return new RGB((color >> 8 & 0xf) | (color >> 4 & 0x0f0),
59 (color >> 4 & 0xf) | (color & 0xf),
60 ((color & 0xf) << 4) | (color & 0xf),
61 1);
62}
282export function hexToRgb(hexColor: string) {
283 const rgb = parseInt(hexColor.substring(1), 16);
284 const r = (rgb >> 16) & 0xff;
285 const g = (rgb >> 8) & 0xff;
286 const b = (rgb >> 0) & 0xff;
287 return [r, g, b];
288}
128function hexToRgb (hex) {
129
130 if(hex == undefined){
131 return "";
132 }
133
134 var valueWithoutSymbol = hex.slice(1, hex.length);
135 var bigint = parseInt(valueWithoutSymbol, 16);
136 var r = (bigint >> 16) & 255;
137 var g = (bigint >> 8) & 255;
138 var b = bigint & 255;
139
140 return "rgb(" + r + ", " + g + ", " + b + ")";
141 }
4static hex2rgb(hex) {
5 // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
6 let shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
7 hex = hex.replace(shorthandRegex, function(m, r, g, b) {
8 return r + r + g + g + b + b;
9 });
10
11 let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
12 return result ? {
13 r: parseInt(result[1], 16),
14 g: parseInt(result[2], 16),
15 b: parseInt(result[3], 16)
16 } : null;
17}
1679function convertToHex(red, green, blue) {
1680 return reduceHexValue('#' + colorToHex(red) + colorToHex(green) + colorToHex(blue));
1681}
52export function hexToColor (hex) {
53 let color = hexToRgb(hex)
54 return MSColor.colorWithRed_green_blue_alpha(color.r / 255, color.g / 255, color.b / 255, 1.0)
55}
183function hexToRgb(hex){
184 hex = hex.replace('#','');
185 var myTempRGBColor = {
186 r: parseInt(hex.substring(0,2), 16),
187 g: parseInt(hex.substring(2,4), 16),
188 b: parseInt(hex.substring(4,6), 16)
189 }
190
191 return myTempRGBColor
192}
365function hexToRgb (hex) {
366 var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
367 return [
368 parseInt(result[1], 16) / 255,
369 parseInt(result[2], 16) / 255,
370 parseInt(result[3], 16) / 255
371 ]
372}

Related snippets