Random Hex Color Generator + RGB Sliders jquery
<p>I'm having issues getting this hex randomizer function to pass the value (var hex) to generate a new color. I'm not good with event handlers yet so any help is really appreciated. Thanks in advance :)</p>

<pre><code>&lt;body&gt;
 &lt;div id="backgroundColor" style="background:#EEE8CD;"&gt;
   &lt;p&gt;R:
     &lt;span class="wrap_red"&gt;&lt;input class="r ch" type="range" min="0" max="255" /&gt;&lt;/span&gt;
     &lt;input type="number" min="0" max="255" /&gt;
   &lt;/p&gt;
   &lt;p&gt;G:
     &lt;span class="wrap_green"&gt;&lt;input class="g ch" type="range" min="0" max="255" /&gt;&lt;/span&gt;
     &lt;input type="number" min="0" max="255" /&gt;
   &lt;/p&gt;
   &lt;p&gt;B:   
     &lt;span class="wrap_blue"&gt;&lt;input class="b ch" type="range" min="0" max="255" /&gt;&lt;/span&gt;
     &lt;input type="number" min="0" max="255" /&gt;
   &lt;/p&gt;
   &lt;table id="tableRGB"&gt;
     &lt;tr&gt;
         &lt;td&gt;
           &lt;strong&gt;RGB: &lt;/strong&gt;#&lt;span class="result"&gt;567890&lt;/span&gt;
         &lt;/td&gt;
         &lt;td&gt;
           &lt;strong&gt; Hexadecimal: &lt;/strong&gt;#&lt;input type="number"&gt;
         &lt;/td&gt;
    &lt;/tr&gt;  
    &lt;/table&gt; 
   &lt;button type="button" id="button" onclick="click();"&gt;Generate&lt;/button&gt;
</code></pre>

<p></p>

<pre><code>$(document).ready(function () {  
    var Color = {
    defaults: {
    // Predefined hex codes that cant be used as random colors
    // All must be prefixed with the '#' indicator
    predef: [],

    // Maximum &amp; Minimum random range values
    rangeMax: 255,
    rangeMin: 0,

    // Upper and lower level values that must be 
    // passed for random color acceptance
    //
    // By setting levelUp: 200, levelDown: 100; Neutral
    // colors like White, Gray, and Black can be somewhat weeded
    // out and your random colors will be full spectrum based.
    // Note*: Doing so increases likely hood of recursion
    levelUp: -1,
    levelDown: 256,

    // Recursion handlers
    recursionLimit: 15,
    recursion: function () {
        throw 'Recursion Error in Random Color Generator, ' +
            'too many tries on finding random color, ' +
            '[Limit ' + this.recursionLimit + ']';
    }
},

// Caching of random colors
stack: {},

// Returns a random color in hex code form, and caches
// find in the stack.

rand: function () {
    var defaults = this.defaults;
    return defaults.rangeMin + Math.floor(Math.random()*(defaults.rangeMax+1));
},
random: function (i) {
    var self = this,
        defaults = self.defaults,
        r = self.rand(),
        g = self.rand(),
        b = self.rand(),
        hex = self.rgb2hex(r, g, b),
        levels = true;

    // Check for recursion
    if (i === undefined || typeof i !== 'number') i = 0;
    else if (i++ &gt; defaults.recursionLimit) return defaults.recursion();

    // Color already used, try another one
    if (self.stack[hex]) hex = self.random(i);

    // Ensure one of the vals is above levelUp and another is below levelDown
    // Check defaults comments for better understanding
    levels = !!(
        (r &gt; defaults.levelUp || g &gt; defaults.levelUp || b &gt; defaults.levelUp) &amp;&amp;
        (r &lt; defaults.levelDown || g &lt; defaults.levelDown || b &lt; defaults.levelDown)
    );
    if (! levels) hex = self.random(i);

    // Store on stack to help prevent repeat
    self.stack[hex] = [r,g,b];

    // Return hex code in #
    return hex;
}


// Returns hex code
rgb2hex: function (r,g,b) {
    var str = '0123456789ABCDEF';
    return '#' + [
        str.charAt( (r - r % 16) / 16) + str.charAt(r % 16),
        str.charAt( (g - g % 16) / 16) + str.charAt(g % 16),
        str.charAt( (b - b % 16) / 16) + str.charAt(b % 16)
    ].join('');
},

// Returns in array form [red, green, blue]
hex2rgb: function (hex) {
    if (hex.substr(0,1) === '#')
        hex = hex.substr(1);

    // Use the stack if possible to reduce processing
    return this.stack['#' + hex] ? this.stack['#' + hex] : 
        hex.length === 6 ? [
            parseInt(hex.substr(0, 2), 16),
            parseInt(hex.substr(2, 2), 16),
            parseInt(hex.substr(4, 2), 16)
        ] : hex.length === 3 ? [
            parseInt(hex.substr(0, 1), 16),
            parseInt(hex.substr(1, 1), 16),
            parseInt(hex.substr(2, 1), 16)
        ] : [];
}
   };
//Random color generator button
$('#generate').click( function () {
    $('div').each(function() {
        var th = $(this);
        hex = th.css('backgroundColor');
        rgb = hex.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
        var red = rgb[1];
        var green = rgb[2];
        var blue = rgb[3];

        th.find('.r').val(red);
        th.find('.r').parent('span').siblings('input').val(red);
        th.find('.g').val(green);
        th.find('.g').parent('span').siblings('input').val(green);
        th.find('.b').val(blue);
        th.find('.b').parent('span').siblings('input').val(blue);

        $('input').bind('change keyup click', function() {
            if ($(this).hasClass('ch')) {
                $(this).parent('span').siblings('input').val($(this).val());
            }
            else {
                if ($(this).val() &gt; 255) 
                    $(this).val(255);
                if ($(this).val() &lt; 0) 
                    $(this).val(0);
                    $(this).siblings('span').find('input').val($(this).val());
            }

            r = parseInt(th.find('.r').val()).toString(16);
            if (r.length == 1) 
                r = '0' + r;

            g = parseInt(th.find('.g').val()).toString(16);
            if (g.length == 1) 
                g = '0' + g;

            b = parseInt(th.find('.b').val()).toString(16);
            if (b.length == 1) 
                b = '0' + b;

            x = r + g + b;

            th.find('.result').html(x);
            th.css('backgroundColor', '#' + x);

        });
    });
});
});
</code></pre>

<p>
  </p>
