2012-03-11 95 views
0

我有問題得到這個十六進制隨機函數傳遞值(var十六進制)生成一種新的顏色。我不擅長事件處理程序,所以任何幫助都非常感謝。感謝提前:)隨機十六進制顏色發生器+ RGB滑塊jquery

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

$(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 & 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++ > 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 > defaults.levelUp || g > defaults.levelUp || b > defaults.levelUp) && 
     (r < defaults.levelDown || g < defaults.levelDown || b < 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 = 'ABCDEF'; 
    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() > 255) 
        $(this).val(255); 
       if ($(this).val() < 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); 

     }); 
    }); 
}); 
}); 

+0

那麼「有什麼問題」或「不起作用」? – 2012-03-11 18:31:03

+0

不幸的是,它不起作用。 – dgills 2012-03-11 18:35:51

+0

我將添加html – dgills 2012-03-11 18:36:08

回答

0

對於初學者您onclick="click()"是聯合國需要,你可以使用jQuery的.click()捕捉,像$('#button').click(function(){/*do stuff*/})$('#button').click(function_name)那麼你有一堆您的代碼中出現錯誤,jslintjshint可能會有幫助,以下是我看到的錯誤:

Error: Problem at line 51 character 51: Expected '{' and instead saw 'i'. if (i === undefined || typeof i !== 'number') i = 0;

Problem at line 52 character 45: Expected '{' and instead saw 'return'. else if (i++ > defaults.recursionLimit) return defaults.recursion();

Problem at line 55 character 26: Expected '{' and instead saw 'hex'. if (self.stack[hex]) hex = self.random(i);

Problem at line 63 character 19: Expected '{' and instead saw 'hex'. if (! levels) hex = self.random(i);

Problem at line 74 character 1: Expected '}' to match '{' from line 2 and instead saw 'rgb2hex'. rgb2hex: function (r,g,b) {

Problem at line 74 character 8: Missing semicolon. rgb2hex: function (r,g,b) {

Problem at line 74 character 8: Expected '}' to match '{' from line 1 and instead saw ':'. rgb2hex: function (r,g,b) {

Problem at line 74 character 10: Expected ')' and instead saw 'function'. rgb2hex: function (r,g,b) {

Problem at line 74 character 26: Missing semicolon. rgb2hex: function (r,g,b) {

Problem at line 74 character 27: Expected to see a statement and instead saw a block. rgb2hex: function (r,g,b) {

Problem at line 74 character 27: Stopping, unable to continue. (50% scanned). Implied global: $ 1, r 74, g 74, b 74 Unused variable: Color 1 "ready"

相關問題