Using Xcode: Creating Reusable and Shareable Color Palettes
Having a full range of colors that can be shared and reused in different Xcode projects improves efficiency and workflow because it removes the time wasted in looking for colors that will work together for your projects. Additionally, it ensures consistency of color usage within your apps and projects.
Historically, developers and designers create color palettes using design tools such as Adobe Illustrator, Sketch, and Figma. It turns out that Swift developers can build these color collections and manage them directly inside Xcode using the built-in color picker. In this article, you will discover how to create a set of colors called DuoChroma to use later to customize the look and feel of a sample Stream Chat SwiftUI app. The techniques, tips, and tricks you learn in this article will assist you in building coherent color schemes for your future Xcode projects.
Getting started
To follow along and get the best out of this tutorial, install Xcode on your Mac. If you do not have Xcode yet, you can download and install it from the Mac App Store. Download the completed color scheme from GitHub or create a blank SwiftUI project with Xcode and let’s begin.
Launch Xcode, create a new SwiftUI project, and name it duoChromaPalette. Next, rename the Swift file ContentView in your project navigator to duoColorSet. You will use this file soon to access the color picker.
The color picker
The color picker provides color drawers/wells and controls that allow you to select system colors or build your own color sets. It is available in other Mac applications such as Final Cut Pro and Apple Motion. You can find it in most third-party Mac apps such as Kite Compositor and ScreenFlow. However, this article focuses mainly on the color picker in Xcode.
During the research phase of writing this article, several Swift developers were asked how they use the color picker in Xcode. All the developers knew how to use it to set colors in their projects. However, none of them knew its obscured features. As a result, we designed the tutorial to give you the key to unlocking the hidden gems and capabilities of the color picker. You will master the craft of building shareable and reusable color palettes that can be shared with your team or used in future Xcode projects.
How to access the color picker in Xcode
To use the color picker in Xcode, you can access it in two ways:
// duoColorSet.swift
// duoChromaPalette
import SwiftUI
struct duoColorSet: View {
// Color literal definition
let deviceBg = colorLiteral(red: 0, green:
0.3725490196, blue: 1, alpha: 1)
var body: some View {
ZStack {
Color(deviceBg)
.ignoresSafeArea()
VStack {
Image("streamLogoMark")
.resizable()
.frame(width: 64, height: 32)
Text("Hello, **Stream**!")
}
}
}
}
struct duoColorSet_Previews: PreviewProvider {
static var previews: some View {
duoColorSet()
.preferredColorScheme(.dark)
}
}
Touring the color categories
Click the color chip from the literal’s definition to open the color wells. Then, click Other at the bottom center of the window to present the color categories.
The colors window has three sections:
Using the default colors
Click the color button on the top to switch between the pickers. There are five categories of pickers and these are:
Create a custom palette using the Color Palettes category
In this section, you will learn how to build custom-made color palettes by sampling colors from interface elements. To create your color scheme in Xcode, you should use the categories of the Color Palettes and Image Palettes.
In the Swift file duoColorSet:
// duoColorSet.swif
// duoChromaPalette
import SwiftUI
struct duoColorSet: View {
// Color literal definition
let deviceBg = colorLiteral(red: 0, green:
0.3725490196, blue: 1,
alpha: 1)
let addColor = colorLiteral(red: 0.5409796238, green:
0.776439786,
blue: 0.2311589718, alpha: 1)
var body: some View {
ZStack {
Color(deviceBg)
.ignoresSafeArea()
VStack {
Image("streamLogoMark")
.resizable()
.frame(width: 64, height: 32)
Text("Hello, **Stream**!")
}
}
}
}
struct duoColorSet_Previews: PreviewProvider {
static var previews: some View {
duoColorSet()
.preferredColorScheme(.dark)
}
}t
When you choose Apple from the color options, a window similar to the image above will be presented with default system colors from Apple. The bottom part of the window has the color preview, eyedropper, and color wells.
To create a sample palette:
You now have a custom-made Xcode color palette that can be used in future projects or shared with other team members to use. You may ask, how can the palette be exported from Xcode and shared? Keep reading to find out.
Create a custom palette using the Image Palettes category
In this section, you will build a color palette called DuoChroma using an image of the Duolingo character cloned in Figma. You can follow along with your image to complete the following steps.
## Sharing, installing and reusing color palettes
In the previous section, we created a color palette called DuoChroma. This palette will be automatically available in other Xcode projects you create using your Mac. However, to share the color palette file with others to reuse, you need to find out where it is located on your system. You can jump to Spotlight or the macOS finder to look for the palette you just created. When you try to search it using the Spotlight shortcut cmd + spacebar, the result will show related files instead of the actual color palette file, as shown in the image below. That is strange, huh!!.
Don’t worry, the file is still on your Mac but hidden. Whenever you create a custom color palette in Xcode, it will be stored as an Apple Color List (CLR) with the extension .clr in the system’s library. So, the file name of the palette becomes DuoChroma.clr. It is a binary property list (Plist) file. So it will be hidden by default.
You can find it with the command line Terminal or Hyper using the standard search path `~/Library/Colors`.
Once you locate the file in `~/Library/Colors`, you can copy and paste it to any location on your system. For example, you can use the command `cp DuoChroma.clr ~/Desktop` to copy it to the Desktop as shown in the image below.
Now that you have successfully saved the file DuoChroma.clr to a location, you can send it to anyone to reuse in other Xcode projects. To do that, you should follow the steps below:
Where do I go next?
Congratulations! You now know how to create color palettes in Xcode. You have learned to share and reuse the palettes you build in other Xcode projects. Start building awesome palettes in Xcode to ensure harmonious color usage in your apps and projects. If you are new to Stream Chat SwiftUI, check out the [getting started tutorial](https://getstream.io/tutorials/swiftui-chat/). To learn more about Xcode and SwiftUI, subscribe to the Stream Developers YouTube channel.
Originally published at https://getstream.io.
iOS Developer | Swift | SwiftUI | Objective-C | Python | Data Science | BT Group
3yAmazing. Thank you for this
Sr Software Engineer | React Native | iOS
3yLove this 🤩