-
Notifications
You must be signed in to change notification settings - Fork 1.3k
UIColor Methods
● Flat Colors
● Random Colors
● Complementary Colors
● Contrasting Colors
● Flattening Non-Flat Colors
● Gradient Colors
● Hex Colors
● Lighter & Darker Colors
Using a flat color is as easy as adding any other color in your app (if not easier). For example, to set a view's background property to a flat color with a dark shade, you simply have to do the following:
self.view.backgroundColor = [UIColor flatGreenColorDark];
view.backgroundColor = UIColor.flatGreenDark
self.view.backgroundColor = FlatGreenDark;
view.backgroundColor = FlatGreenDark()
Setting the color for a light shade is the same, except without adding the Dark suffix. (By default, all colors without a Dark suffix are light shades). For example:
self.view.backgroundColor = [UIColor flatGreenColor];
view.backgroundColor = UIColor.flatGreen
self.view.backgroundColor = FlatGreen;
view.backgroundColor = FlatGreen()
There are four ways to generate a random flat color. If you have no preference as to whether you want a light shade or a dark shade, you can do the following:
self.view.backgroundColor = [UIColor randomFlatColor];
view.backgroundColor = UIColor.randomFlat
self.view.backgroundColor = RandomFlatColor;
view.backgroundColor = RandomFlatColor()
Otherwise, you can perform the following method call to specify whether it should return either a light or dark shade:
[UIColor colorWithRandomFlatColorOfShadeStyle:UIShadeStyleLight];
UIColor(randomFlatColorOfShadeStyle:.Light)
RandomFlatColorWithShade(UIShadeStyleLight);
RandomFlatColorWithShade(.Light)
UIShadeStyles:
-
UIShadeStyleLight
(UIShadeStyle.Light
in Swift) -
UIShadeStyleDark
(UIShadeStyle.Dark
in Swift)
If you need to be a bit more selective and only display a random color from a set list of colors, you can use the following method:
[UIColor colorWithRandomColorInArray:@[FlatWhite, FlatRed, FlatBlue]];
TBA
RandomFlatColorInArray(@[FlatWhite, FlatRed, FlatBlue])
TBA
Last but certainly not least, you can also choose form the list of random colors and exclude the ones you don't want. For example say you want to randomly select a flat color for a user's profile, but don't want to use any blacks, grays, or whites. You can simply do:
[UIColor colorWithRandomFlatColorExcludingColorsInArray:@[FlatBlack, FlatBlackDark, FlatGray, FlatGrayDark, FlatWhite, FlatWhiteDark]];
TBA
RandomFlatColorExcluding(@[FlatBlack, FlatBlackDark, FlatGray, FlatGrayDark, FlatWhite, FlatWhiteDark])
TBA
To generate a complementary color, perform the following method call, remembering to specify the color whose complement you want:
[UIColor colorWithComplementaryFlatColorOf:(UIColor *)someUIColor];
UIColor(complementaryFlatColorOf:someUIColor)
ComplementaryFlatColorOf(color);
ComplementaryFlatColorOf(color)
The contrasting color feature returns either a dark color a light color depending on what the Chameleon algorithm believes is a better choice. You can specify whether the dark or light colors are flat: ([UIColor flatWhiteColor]
& [UIColor flatBlackColorDark]
) or non-flat ([UIColor whiteColor]
& [UIColor blackColor]
).
If you're trying to set a UILabel's textColor
property, make sure you provide the UILabel's backgroundColor
. If your label has a clear backgroundColor
, just provide the backgroundColor
property of the object directly behind the UILabel
.
Here's an example:
[UIColor colorWithContrastingBlackOrWhiteColorOn:(UIColor *)backgroundColor isFlat:(BOOL)flat];
UIColor(contrastingBlackOrWhiteColorOn:UIColor!, isFlat:Bool)
ContrastColor(backgroundColor, isFlat);
ContrastColor(backgroundColor, isFlat)
As mentioned previously, this feature is unique to Chameleon. While this feature is in its early stages of operation and can be improved, it is accurate in finding the nearest flat version of any color in the spectrum, and very simple to use:
[(UIColor *)color flatten];
UIColor.pink.flatten()
Using a gradient to color an object usually requires a couple of lines of code plus many more lines to superimpose smart contrasting text. Thankfully, Chameleon takes care of that for you. We've introduced a new way to have multicolored objects, and that's with gradients!
Chameleon provides three simple gradient styles. Gradients can be created from any number of colors you desire as long as at least two colors are provided. Don't forget that the contrasting text feature is also compatible with gradient colors!
UIGradientStyles:
-
UIGradientStyleLeftToRight
(UIGradientStyle.LeftToRight in Swift) -
UIGradientStyleTopToBottom
(UIGradientStyle.TopToBottom in Swift) -
UIGradientStyleRadial
(UIGradientStyle.Radial in Swift)
[UIColor colorWithGradientStyle:(UIGradientStyle)gradientStyle withFrame:(CGRect)frame andColors:(NSArray<UIColor *> *)colors];
UIColor(gradientStyle:UIGradientStyle, withFrame:CGRect, andColors:[UIColor])
GradientColor(gradientStyle, frame, colors);
GradientColor(gradientStyle, frame, colors)
Objective-C Note: If you use the Chameleon Shorthand, and use the NSArray
literal @[]
to set the array of colors, make sure you add parenthesis around it, or else you'll get an error.
Note: UIGradientStyleRadial
only uses a maximum of 2 colors at the moment. So if more colors are provided, they will not show.
One of the most requested features, hex colors, is now available. You can simply provide a hex string with or without a # sign:
[UIColor colorWithHexString:(NSString *)string];
UIColor(hexString:string)
HexColor(hexString)
HexColor(hexString)
Retrieving the hexValue
of a UIColor is just as easy.
[FlatGreen hexValue]; //Returns @"2ecc71"
FlatGreen.hexValue //Returns @"2ecc71"
Sometimes all you need is a color a shade lighter or a shade darker. Well for those rare, but crucial moments, Chameleon's got you covered. You can now lighten any color the following way:
[color lightenByPercentage:(CGFloat)percentage];
color.lightenByPercentage(percentage: CGFloat)
You can also generate a darker version of a color:
[color darkenByPercentage:(CGFloat)percentage];
color.darkenByPercentage(percentage: CGFloat)
Chameleon now supports the extraction of colors from images. You can either generate both flat and non-flat color schemes from an image, or easily extract the average color.
To generate a color scheme simply do the following:
[NSArray arrayOfColorsFromImage:(UIImage *)image withFlatScheme:(BOOL)flatScheme];
NSArray(ofColorsFromImage: UIImage, withFlatScheme: Bool)
ColorsFromImage(image, isFlatScheme)
ColorsFromImage(image, isFlatScheme)
To extract the average color from an image, you can also do:
[UIColor colorWithAverageColorFromImage:(UIImage *)image];
UIColor(averageColorFromImage: UIImage)
AverageColorFromImage(image)
AverageColorFromImage(image)
Many apps on the market, even the most popular ones, overlook this aspect of a beautiful app: the status bar style. Chameleon has done something no other framework has... it has created a new status bar style: UIStatusBarStyleContrast
. Whether you have a ViewController
embedded in a NavigationController
, or not, you can do the following:
[self setStatusBarStyle:UIStatusBarStyleContrast];
self.setStatusBarStyle(UIStatusBarStyleContrast)