$(document).ready(function(){ function colorize($target){ var card = $($target), color_panel = card.find('header'), range = $('input[type*="range"]'), range_red = $('input[type*="range"][class*="-red"]'), range_blue = $('input[type*="range"][class*="-blue"]'), range_green = $('input[type*="range"][class*="-green"]'), r_color = $('input[type*="range"][class*="-red"]').attr('value'), g_color = $('input[type*="range"][class*="-green"]').attr('value'), b_color = $('input[type*="range"][class*="-blue"]').attr('value'); color_panel.css('background', 'rgb('+Math.floor(r_color)+','+Math.floor(g_color)+','+Math.floor(b_color)+')'); var rgbVal = 'rgb('+Math.floor(r_color)+','+Math.floor(g_color)+','+Math.floor(b_color)+')'; range.on('change', function(){ var $this = $(this), vMax = $this.attr('max'), mVal = 100/vMax, r_color = $('input[type*="range"][class*="-red"]').attr('value'), g_color = $('input[type*="range"][class*="-green"]').attr('value'), b_color = $('input[type*="range"][class*="-blue"]').attr('value'); color_panel.css('background', 'rgb('+Math.floor(r_color)+','+Math.floor(g_color)+','+Math.floor(b_color)+')'); var rgbVal = 'rgb('+Math.floor(r_color)+','+Math.floor(g_color)+','+Math.floor(b_color)+')'; var hex = rgb2hex( rgbVal ); $('.result').html( hex ); }); //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) : ''; } var hex = rgb2hex( rgbVal ); $('.result').html( hex ); } colorize('.m-card') })