Swift implementation of rainbow gradient

Source: Internet
Author: User

Thank you for your support and attention. I was flattered by the rapid increase in access volume as soon as Web color's swfit implementation was published.

To express gratitude, the Rainbow gradient was also achieved this morning.


Latest code & Related Materials: https://github.com/duzixi/RainbowColors-with-Swift (continuous maintenance)

Generate a function prototype:

  • Func rainbowcolor (X: Float)-> uicolor
Value range:

X: 0 ~ 256*5-1


Example of generating gradient of all rainbow colors:

Class viewcontroller: uiviewcontroller {override func viewdidload () {super. viewdidload () // do any additional setup after loading the view, typically from a nib. let viewheight: Float = float (self. view. frame. size. height) Let viewwidth: Float = float (self. view. frame. size. width) // create all rainbow colors let Inc: Float = 256*5/viewheight; For (var I: Float = 0.0; I <256*5; I ++ = Inc) {Let view = uiview (); view. frame = cgrectmake (0, 0 + I/INC, viewwidth, 1); view. backgroundcolor = rainbowcolor (I); // <---- call the rainbow gradient function. The parameter must be float self. view. addsubview (View) ;}} override func didreceivememorywarning () {super. didreceivememorywarning () // dispose of any resources that can be recreated .}}

Program execution:



Source code reference:

(Note: The following Source Code may not be up-to-date. For the latest source code, click the link above this article .)

<Span style = "font-family: Arial; font-size: 14px;"> // rainbowcolor. swift /// created by Du Zi Jun on 14-6-29. // copyright (c) 2014 lanou. all rights reserved. // import uikit // X: 0 ~ 256*5-1 func rainbowcolor (X: Float)-> uicolor {var unit: Float = 256 var R: Float = 0, G: Float = 0, B: float = 0 if (0 <= x & x <Unit) {// red-> orange-> yellow R = unit-1g = x} else if (x <Unit * 2) {// yellow-> green r = unit-1-x % Unit G = unit-1} else if (x <Unit * 3) {// green-> Green G = unit-1-x % unit/2 B = x % unit} else if (x <Unit * 4) {// blue-> Blue G = unit/2-1-x % unit/2 B = unit-1} else if (x <Unit * 5) {// blue-> purple r = x % Unit B = unit-1} return uicolor (RED: R/(unit-1), Green: g/(unit-1), blue: B/(unit-1), alpha: 1.0)} </span>


Program BUG:

Debugging environment: xcode6 Beta

There is no problem with the 4s and 5 simulators. The operator error will be prompted during the 5S test.

This is probably a data type issue. However, if debugging is not successful for any reason, please kindly advise.


Related Article

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.