in ios, mobile

Managing Color in iOS application

Choosing right color in application make it elegant, but achieving this is not an easy task. For this, we need a support from best designers. In the same time even designer cannot achieve the best layout and design in one shot. He needs to keep trying with the different color theme and figure out the best one. This is the normal process of application design, but reflecting this via coding is even tough task especially when color theme keeps changing.time even designer cannot achieve the best layout and design in one shot. He needs to keep trying with the different color theme and figure out the best one. This is the normal process of application design, but reflecting this via coding is even tough task especially when color theme keeps changing.

I came to this tutorial A Smart Way to Manage Colour Schemes for iOS Application Development by Sauvik Dolui which inspire me to write this post. This post will add more simplicity and reusability in managing color theme.

Derived Color

Derived color is a color that dynamically depends on another color. When the parent color changes derived color updated automatically. Derived color can be in the following form.

  • Highlight Color: color that will get highlighted over the parent or base color
    func highlight(withLevel highlight: CGFloat) -> UIColor {
        var red: CGFloat = 1, green: CGFloat = 1
        var blue: CGFloat = 1, alpha: CGFloat = 1
        self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        return UIColor(red: red * (1-highlight) + highlight,
                       green: green * (1-highlight) + highlight,
                       blue: blue * (1-highlight) + highlight,
                       alpha: alpha * (1-highlight) + highlight)
    }

  • Shadow Color: Shadow does not necessarily be always black. In a colorful world, a shadow will contain black + parent color.
    func shadow(withLevel shadow: CGFloat) -> UIColor {
        var red: CGFloat = 1, green: CGFloat = 1
        var blue: CGFloat = 1, alpha: CGFloat = 1
        self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        return UIColor(red: red * (1-shadow),
                       green: green * (1-shadow),
                       blue: blue * (1-shadow),
                       alpha: alpha * (1-shadow) + shadow)
    }
    

  • Opaque Color: parent color with opacity
    func withAlpha(_ newAlpha: CGFloat) -> UIColor {
        var hue: CGFloat = 1
        var saturation: CGFloat = 1
        var brightness: CGFloat = 1
        
        self.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: nil)
        return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: newAlpha)
    }
    

If we consider this scenario then the following design can be easily achieved by just changing the parent color or base color.

Color Library for your application

Color library is a collection of all possible colors that your app is going to use. And the main reason of creating this color library is to centralize color holder in one place and encourage uniformity of using color in various part of your application. The traditional approach may be creating a color constant from the hex string. Just looking at the text string our mind cannot imagine what color would it be. It would be great if we could do it visually. For this, sketch along with paint code plugin provides an extremely easy way out in creating a color library and generating codes to your favorite language (objective C of swift). If this topic is new for you then take a look at this tutorial PaintCode for Sketch. In our previous image, there are just 3 primary color i.e. red, green, blue. All other color derived from these. But in the appliation only color is valid at a time So let’s create a primary color library using the sketch.appliation only color is valid at a time So let’s create a primary color library using the sketch.

  • First, create an artboard in the sketch and name it Library.

Create one rectangle and choose red as their fill color and give a shape appropriate name themeColor

  • Now click on from menu option and select paint code. Give appropriate file name ColorLibrary and finally, click on Export. You can export in your favorite language. But for this, you need to install paint code plugin for sketch first. Here’s the code generated.
import UIKit

class ColorLibrary: NSObject {
    
    
    //MARK: - Colors
    
    static let greenColor = UIColor(hue: 0.252, saturation: 1, brightness: 0.655, alpha: 1)
    static let blueColor = UIColor(hue: 0.639, saturation: 0.845, brightness: 0.58, alpha: 1)
    static let redColor = UIColor(hue: 0.999, saturation: 0.665, brightness: 0.69, alpha: 1)
    
    
}

Time to implement Philosophy

  • Create new project in Xcode and name it ManageColor
  • In the storyboard, create the following design. For now, color is not appropriate but we will fix it later in the code.
  • Create
  • Add two files, ColorLibrary.Swift and DeriveColor.swift into your project which you can get from here.

Now add following code in viewDidLoad method

  self.topview.backgroundColor = ColorLibrary.themeColor
         self.topview.layer.shadowColor = ColorLibrary.themeColor.shadow(withLevel: 0.6).cgColor
         self.topview.layer.shadowOpacity = 1
         self.topview.layer.shadowOffset = CGSize.zero
         self.topview.layer.shadowRadius = 10
        
        self.titleLabel.textColor  = ColorLibrary.themeColor.highlight(withLevel: 0.7)
        self.bottomView.backgroundColor = ColorLibrary.themeColor.withAlpha(0.2)
    }

All you now see is this.

Now onwards, if we need to change the theme color, we never need to change the code. We just need to choose the new color in a sketch and generate the new code. That’s it.

Complete sample can be downloaded from GitRepo

* more post at http://mobile.rajantwanabashu.com.np

Write a Comment

Comment

Webmentions

  • гдз по геометрии 11 класс

    northwest pharmacies http://cteniztan.webcindario.com

    Thanks! Awesome information!

  • гдз по русскому тростенцова

    canadian pharmacies http://fiepulcli.webcindario.com

    With thanks, Ample advice.

  • гдз мордкович 10

    canadian discount pharmacies http://exworwebp.webcindario.com

    Wow lots of superb advice!

  • гдз по русскому языку 8

    canadian pharmaceuticals http://tiopansa.webcindario.com

    Good tips. Thanks a lot!

  • гдз макарычев 9

    northwest pharmacies http://esecdi.webcindario.com

    Point nicely used!.

  • гдз 2010

    canada drugs online http://mastlina.webcindario.com

    Reliable advice. Kudos!

  • гдз 7 класс по английскому

    northwest pharmacies http://fizzbenree.webcindario.com

    Really tons of valuable info!

  • купить виагру без рецепта

    canadian pharmacies https://viagravonline.com/

    Wonderful material. Thank you!

  • sildenafil 100mg

    canada pharmacies online prescriptions http://www.viagraessale.com/

    Wow tons of good info.