rgb(a) to hex
by schrodingers
HTML
Convert RGB/RGBA to hex:
<br> (opacity of rgba is ignored)
<br>
<input type="text" value="rgba(34, 34, 34, 1)">
<button>Convert</button>
<br>
<br> Result <span class="result"></span>
CSS
body {
padding: 20px;
background: #222;
color: #ddd;
}
JavaScript
//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);
});