10 examples of 'javascript hex to rgb' in JavaScript

Every line of 'javascript hex to rgb' 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
377function hexAsRgb(hex) {
378 return "rgb(" + [parseInt(hex.substring(1, 3), 16), parseInt(hex.substring(3, 5), 16), parseInt(hex.substring(5, 7), 16)] + ")";
379}
153function rgb_to_hex(r, g, b) {
154 r = Math.round(r * 255).toString(16);
155 g = Math.round(g * 255).toString(16);
156 b = Math.round(b * 255).toString(16);
157 return '#' + pad(r) + pad(g) + pad(b);
158}
139function rgb2hex(rgb) {
140 return (rgb[0] * 255 << 16) + (rgb[1] * 255 << 8) + (rgb[2] * 255 | 0);
141}
63function rgb2hex(rgb) {
64 return ((rgb[0]*255 << 16) + (rgb[1]*255 << 8) + rgb[2]*255);
65};
37function rgb2hex(rgb) { //http://stackoverflow.com/a/3971432/3809029
38 rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/);
39
40 function hex(x) {
41 return ('0' + parseInt(x).toString(16)).slice(-2);
42 }
43
44 return '#' + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
45}
204function rgb2hex(rgb) {
205 // jsfiddle /Mottie/xcqpF/1/light/
206 rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
207 return (rgb && rgb.length === 4) ? "#" +
208 ("0" + parseInt(rgb[1], 10).toString(16)).slice(-2) +
209 ("0" + parseInt(rgb[2], 10).toString(16)).slice(-2) +
210 ("0" + parseInt(rgb[3], 10).toString(16)).slice(-2) : '';
211}
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}
887function rgb2hex(r, g, b) {
888 return int2hex(r * 65536 + g * 256 + b);
889 //return int2hex(r + g * 256 + b * 65536);
890}
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}
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 }

Related snippets