hexstring to rgb array js
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
function hexToRgb(hex) {
var result = /^#?([a-fd]{2})([a-fd]{2})([a-fd]{2})$/i.exec(hex);
if(result){
var r= parseInt(result[1], 16);
var g= parseInt(result[2], 16);
var b= parseInt(result[3], 16);
return r+","+g+","+b;//return 23,14,45 -> reformat if needed
}
return null;
}
console.log(rgbToHex(10, 54, 120)); //#0a3678
console.log(hexToRgb("#0a3678"));//"10,54,120"function hexToRgb(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? [
parseInt(result[1], 16),
parseInt(result[2], 16),
parseInt(result[3], 16)
] : null;
}
alert(hexToRgb("#0033ff").g); // "51";
alert(hexToRgb("#03f").g); // "51";
Also in JavaScript:
- angular cdk media query
- run a code after delay js
- javascript set timeout
- send info with get request in js
- javascript matching sock problem
- how to convert char to number in js
- angular pipe for 2 decimal places
- kill all node processes
- uncaught TypeError: $.jajax is not a function
- react state scope
- editable table ajax
- jquery read dropdown in table cell
- sum all odd fibonacci numbers
- <!DOCTYPE html> <!--[if IE 9]> <html lang="" class="js ie9 ... www.tork.cz › product-list www.tork.cz › product-list Cached addEventListener(e,a,!1)})}l(c)}},{}],3:[function(e,n,t){function r(e,n){if(!i)return!1 ... typeof window.performance
- generator in classs js
- js analyse img color
- javascript create object empty
- javascript type casting int
- registeing components in vue
- javascript ajax receive multiple values
- javascript is variable a string
- cloudwatch logs sdk.
- this.setState is undefined inside a async function in js
- set node_env in windows