views:

135

answers:

3

I am using this Math for a bg color animation on hover:

var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';

It produces a random rgb color. Very nice indeed but I look for something different.

Does anybody know a good Math that I can use to have that random color only out of a certain color range, like out of the red color range or out of the greens?

Any help is appreciated.

@Avinash, here is my complete code as I use it right now. How can I include your solution?

$(document).ready(function(){ //bg color animation original = $('.item,.main-menu-button').css('background-color'); $('.item,.main-menu-button').hover(function() { //mouseover var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';//random hover color $(this).stop().animate({'backgroundColor': col}, 1000); },function() { //mouseout $(this).stop().animate({'backgroundColor': '#111'},500);//original color as in css });

I doesn´t work. I better leave it as it is. Thank to all of you for your help.

+2  A: 

Generates a random color out of the black-to-red range:

var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',0,0)';
cherouvim
Actually that snippet has made me understand the whole thing much better. I am playing a little bit around with it. Thank you for your time.
Hardy
+1  A: 

You should research on converting values to and from RGB to HSB (sometimes called HSI). Arithmetic on this color model makes very much sense. E.g. To play with shades of red you can start with HSB values (0, 100, 100) for "pure" red. Change S=50% gives you a "grayer" shade of red. Changing B=50% gives you "darker" shade of red.

Salman A
+3  A: 

To generate a random number in a range, we need to do some thing like

minValue + random Number * (maxValue - minValue)

i.e., if you want create a random number between the range of 100 to 200, we should do

var rand = 100 + Math.floor(Math.random() * (200 - 100));

which gives a random number in between the range 100 to 200

using this basic rule we can generate a random color from a given range

var range = [{0 : 150,1 : 200}, {0 : 200,1 : 230},{0 : 10,1 : 20}];
function rgb() {
  var color  ='rgb(';
  for(var i = 0; i< 3; i++) {
    color += rand(range[i]['0'], range[i]['1']) + ',';
  }
  return color.replace(/\,$/,')')
}

function rand(min, max) {
    return min + Math.floor(Math.random() * (max - min));
}

alert(rgb());

Try this code http://jsbin.com/awahe3

EDIT :

$(function() {
    var cache = $('#hover').css('background-color');
    $('#hover').hover(function() {
        $(this).css({'background-color' : rgb() });
    },function() {
        $(this).css({'background-color' : cache });
    });
});

Example : http://jsbin.com/ejuvo4

Ninja Dude
Hi, that makes sense to me, somehow but I am not very good with javascript and right now I can not figure out how to place that in my existing script without destroying the rest of it. I would like to paste the script I have in here but it will not display well, I am afraid. So how do you guys paste in those script snippets anyway? Ready to reveal that trick?
Hardy
@hardy what's that ?
Ninja Dude
@Avinash that was just an emty mistake because I have realised that I can not just paste snippets into the textfield.
Hardy
@hardy if you're willing to add a code snippet or some thing else.., you can edit your question. There will be an `edit` link for every question posted in SO.
Ninja Dude
@Avinash - Yes, I at least know that but that doesn´t mean that I can copy and paste code snippets without taking care before that the snippet will be shown just the way I have copied it before. If I paste whatsoever, it is displayed without any formatting here and this is what I ment. You guys include your code with that grey background and that seems to make the trick but I don´t know how to do that. Also I think this is not possible at all if using the "add comment" field.
Hardy
@hardy it's quit natural for a newbie. we aren't a harsh people, we help each other in solving the problems. Only thing an OP should provide is proper resources and example his post(question)... This makes the people to under the question. It's pretty simple once you get used to this and you'll love it =). for "grey background" in comments, just wrap your word or line... with a " ` "(tild) symbol.
Ninja Dude
one more thing, you can post JS examples, Demo's at http://jsfiddle.net and http://jsbin.com. I prefer jsbin.com because of it visibility of code is rich when compared to jsfiddle.net :)'
Ninja Dude
$(document).ready(function(){ //bg color animation original = $('.item,.main-menu-button').css('background-color'); $('.item,.main-menu-button').hover(function() { //mouseover var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';//random hover color $(this).stop().animate({'backgroundColor': col}, 1000); },function() { //mouseout $(this).stop().animate({'backgroundColor': '#111'},500);//original color as in css });
Hardy
I knew it will not work. I have tried to paste the code just I could do if I post a question, starting with 4 spaces but in the add comment section it does not seem to work. I can not see any other way to post my current code than asking a new question and post it. All I wanted was to paste my code to ask @Avinash how I would to include his solution to my code without destroying the rest. maybe I will post another question. Oh no.....I can edit my question! I will do so and @Avinash...if you have an idea how I would have to edit my code, please let me know.
Hardy
@hardy sorry for the delay answer. I haven't seen your comments. I've updated my answer and provided an example too. please try it!
Ninja Dude
@Avinash - Thank you so much for your help, very cool. I was wondering about such an amazing amount of help when I finally saw where you come from - from India. No wonder, you people are wonderful. I have a close friend in Pune for some years now. So I am in contact with your country. Thanks again.
Hardy
@hardy you're welcome hardy. Have a Nice Day !!
Ninja Dude