JS generates random Colors

Source: Internet
Author: User

Method One:

var function () {  return  ' # ' +    (function(color) {    return (color + =  ' 0123456789abcdef ' [Math.floor (Math.random () *16)]      )&& (color.length = = 6)?   Color:arguments.callee (color);  }) (');}

Randomly generated 6 characters and then strung together, the closure calls itself with the ternary operator to make the program become introverted.

Method Two:

var function () {  return (function(m,s,c) {    return (c? Arguments.callee (m,s , c-1): ' # ') +      *)]  (Math,' 0123456789abcdef ', 5)}

Extracts the Math object, the string used to generate the hex color value, and uses the third argument to determine if it continues to invoke itself.

Method Three:

Array.prototype.map =function(FN, thisobj) {varScope = Thisobj | |window; varA = [];  for(varI=0, j= This. length; I < J; ++i) {A.push (Fn.call (Scope, This[i], I, This)); }  returnA;};varGetrandomcolor =function(){  return' # ' + ' 0123456789abcdef '. Split ('). Map (function(v,i,a) {returnI>5?NULL: A[math.floor (Math.random () *16)]}). Join (');}

This requires us to do some expansion of the array, and the map will return a list, and then we'll use join to string its elements into characters.

Method Four:

var function () {  return ' # ' +math.floor (Math.random () *16777215). ToString (+);}

This implementation is very counter-day, although a little bit small bug. We know that hex color value is from #000000 to #ffffff, the back of which six digits is 16 binary, equivalent to "0x000000" to "0XFFFFFF". The idea is to convert the maximum hex value FFFFFF first to 10, and then convert it back to 16 after random. Let's take a look at how to get 16777215 of this value.

Method Five:

var function () {  return ' # ' + (Math.random () *0xffffff<<0). ToString (+);}

The basic implementation of the 4 improvement, using the left shift operator to convert 0XFFFFFF to an integer type. So you don't have to remember 16777215. Since the priority of the left-shift operator is less than multiplication sign, it is randomly shifted to the left and even Math.floor is not used.

Method Six:

var function () {  return ' # ' + (function(h) {    returnnew Array (7- h.length). Join ("0") +H  } ((Math.random ()*0x1000000<<0). toString ())}

Fixed the bug in the previous version (unable to generate pure white and hex digits not enough problem). 0x1000000 is quite 0xffffff+1 and ensures that it will be selected to 0XFFFFFF. In the closure we deal with the hex value of less than 6 bits of the problem, directly in the non-fill 0.

Method Seven:

var function () {  return ' # ' + (' 00000 ' + (Math.random () *0x1000000<<0). ToString ()). Slice ( -6);}

This time in front of 0, even recursive detection also saved.

The above version of the generated color range is chatty, but the problem is that the color is not good-looking, so the implementation of 8 out. It produces a fairly vivid color.

Method Eight:

var function () {    return "HSB (" + math.random ()  + ", 1, 1)";}

JS generates random Colors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.