Git Product home page Git Product logo

uicolor-crossfade's Introduction

UIColor+CrossFade

UIColor+CrossFade is a UIColor category adding a class method to return a UIColor created by cross fading between the two specified UIColor objects at the specified ratio (between 0.0 and 1.0).

Using UIColor+CrossFade:

Import UIColor+CrossFade.h, and use as follows:

UIColor *colorA = [UIColor redColor];
UIColor *colorB = [UIColor blueColor];
UIColor *crossFade = [UIColor colorForFadeBetweenFirstColor:colorA secondColor:colorB atRatio:0.5f];
// crossFade is purple

At a ratio of 0.0f, the result will be fully firstColor. At a ratio of 1.0f, the result will be fully secondColor.

Remember - you control the ratio and it doesn't have to be linear! ratio = powf((1 - value/valueNominal), 0.333f) is valid as well.

There is also a convenience method that returns an array of crossfaded increments between the specified firstColor and secondColor (thanks peyton!):

UIColor *colorA = [UIColor redColor];
UIColor *colorB = [UIColor blueColor];
NSUInteger steps = 5;
NSArray *steppedColors = [UIColor colorsForFadeBetweenFirstColor:firstColor lastColor:lastColor inSteps:steps];
// steppedColors is an NSArray of 5 UIColor objects, crossfaded in steps between firstColor and secondColor

You can also use a version of the convenience function that allows you to define the curve the method uses when calculating your color steps as a block. For example:

NSArray *steppedColors = [UIColor colorsForFadeBetweenFirstColor:firstColor 
                                                       lastColor:lastColor
                                               withRatioEquation:^(float input) {
                                                     return powf(input, 1/4.0f);
                                               }
                                                          inSteps:steps];

See the included UIColorCrossFadeDemo project, which uses the extremely handy InfColorPicker, for further example usage.

If you use this in your app, I'd love to hear about it!

uicolor-crossfade's People

Contributors

cbpowell avatar peyton avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.