IOS random color to achieve the principle of detailed analysis

IOS random color to achieve the principle of detailed analysis

Welcome friends to visit the latest blog: http://boaihome.com

1, frontier:

The recent barrage encountered awful things, not to use the font color random color sixteen hex, looking for a long time there is no relevant information, do it yourself, after small children who don’t have to worry about random color!

2, the realization of the principle

Apple provides us with a custom RGB color value method:

(UIColor *) colorWithRed: (CGFloat) red green: (CGFloat) green blue: (CGFloat) blue alpha: (CGFloat) alpha;

It can be seen from the method name that
uses this method to set the color we need three (CGFloat) parameters: (CGFloat) red (CGFloat) green (CGFloat) blue and a (CGFloat) set transparency. We often say
RGB three color: 0-255; and the three parameters parameters need to be normalized, which is in the range of 0-1
; so we are in the process of using, need to first RGB: R/255.0 G/255.0 B/255.0; normalized

3, take a random number

Random number method of OC: arc4random_uniform (10), arc4random_uniform () in brackets behind the random number is what we are going to take the maximum value of
. For example: RGB color values are between 0~255, so we should take the random value of random color words have to be taken to each color value:
arc4random_uniform (255)

4, iOS random color – macro definition

#pragma mark - random color (RGB) with a macro fix: #define BARandomColor [UIColor colorWithRed:arc4random_uniform (255) /255.0 green:arc4random_uniform (255) /255.0 blue:arc4random_uniform (255) /255.0 alpha:1.0f];

5, iOS random color – sixteen decimal method

  • 5.1 sixteen [source: Baidu encyclopedia]
    (English Name: Hexadecimal), is a computer data representation. Different from the expression in our daily life. It is composed of 0-9, A-F, the letter is not case sensitive. And the corresponding relationship between the 10 is: 0-9 corresponds to 0-9; A-F corresponds to 10-15; N hexadecimal number can be expressed with 0~ (N-1), more than 9 with the letter A-F.
  • 5.2 iOS in the development of practical application of
    we put all the sixteen hexadecimal number to create a sixteen hexadecimal array:
    NSArray *colorArray @[@ = “0”, “1”, “@ @ @ 2”, “3”, “4”, “@ @ @ 5”, “6”, “@ 7”, “8”, “@ @ @ 9”, “a”, “B”, “@ @ C, @ d, @ @” e “,” F “];
    (NSString) and then define a type of character variable value:
    `NSString colorString; standby
    , then we have to traverse the array of colors, random values, and then assigned to the above statement variables in colorString:
For (NSInteger i= 0; I 6; < i++) {int r = arc4random (%colorArr.count); NSString *string = colorArr[r]; colorString = [NSString stringWithFormat:@ "%@%@", colorString, string]; NSLog (@ "random color 3:%@", colorString);}
  • 5.3 finally, we also need to optimize the details, such as:
    sixteen hexadecimal color value we have only six digits, so we take the top six on the line:
    NSString *colorStr = [colorString substringFromIndex:6];
  • 5.4 can also be more careful, if we take the value is not 6, it is necessary to deal with the special: if (colorStr.length < 6) {colorStr = @ 00EBFF @};}
  • 5.5 said so much, look at the final source bar:
#pragma mark - random color (sixteen m) - (NSString * ba_randomColor) {NSString = *colorString; *colorArray = "@ NSArray @ @[@" 0 "," 1 "," 2 "," @ @ @ 3 "," 4 "," 5 "," @ @ @ 6 "," 7 "," 8 "," @ @ @ 9 "," a "," B "," @ @ C, @ d, @ @ "e", "F"]; for (NSInteger i= 0; I 6; < i++) {int r = arc4random (%colorArray.count) NSString; *string = colorArray[r]; colorString = "[NSString stringWithFormat:@%@%@", colorString, string]; NSLog (@ "random color 3:%@", colorString);} NSLog (@ random color:% @ ", colorString); return colorString;}

For details, see “BABaseProject”!