It seems a bit rude and less elegant than @bobince's answer, but what the hell.
//setup var colours = [], num_colours = 10, skew_to = 255, skew_chance = 20; //get as many RGB vals as required for (var i=0; i<num_colours; i++) { //generate random grey var this_grey = Math.floor(Math.random() * 256); //skew it towards the @skew_to endpoint, or leave as-is? if (Math.floor(Math.random() * 100) >= skew_chance && this_grey != skew_to) { //skew by random amount (0 - difference between curr val and endpoint) var skew_amount = Math.floor(Math.random() * Math.abs(this_grey - skew_to)); this_grey += ' (skewed to '+(skew_to < this_grey ? this_grey - skew_amount : this_grey + skew_amount)+')'; } colours.push(this_grey); } console.log(colours);
Essentially, it generates random gray dots, and then decides, based on the probably specified (in percent) skew_chance , skew_chance it or not. (If you want to make it random, not permanent). If he decides to skew, a random number is then added or subtracted from the gray value (depending on whether the end skew point is lower or higher than the current value).
This random number is a number from 0 to the absolute difference between the current value and the end point, for example. if the current value is 40 and the endpoint is 100, the added number will be between 0 and 60.
Like I said, @bobince's answer is somewhat, er, more elegant!
source share