Convert RGBA to HEX

/* Convert RGB/RGBA to hex:<br> <input type="text" value="rgba(180, 193, 200, 0.55)"> <button>Convert</button><br> <br> Result <span class="result"></span> */ //Function to convert hex format to a rgb color function rgb2hex(rgb){ rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i); return (rgb && rgb.length === 4) ? "#" + ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) + ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) + ("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : ''; } $('button').click(function(){ var hex = rgb2hex( $('input').val() ); $('.result').html( hex ); });
Simple Js snippet to convert RGBA to HEX format

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.