Swift
UIColorFromRGB
iOS Development
Swift Programming
Mobile App Development

How can I use UIColorFromRGB in Swift?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

UIColorFromRGB is not a built-in Swift API. It is usually just a helper function or extension that converts integer RGB values into the normalized CGFloat values that UIColor expects.

That is the entire idea: UIColor wants numbers between 0.0 and 1.0, while designers and style guides often hand you colors as 0 to 255 RGB values or as a hex integer. A small utility function makes that conversion consistent.

A Basic UIColorFromRGB Function

Here is the classic helper:

swift
1import UIKit
2
3func UIColorFromRGB(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat = 1.0) -> UIColor {
4    return UIColor(
5        red: red / 255.0,
6        green: green / 255.0,
7        blue: blue / 255.0,
8        alpha: alpha
9    )
10}
11
12let accent = UIColorFromRGB(red: 52, green: 152, blue: 219)

This works because UIColor(red:green:blue:alpha:) expects normalized values. Dividing by 255.0 converts the more human-friendly RGB numbers into that range.

A More Swifty Approach: Hex Extension

In modern Swift code, many teams prefer an extension that takes one hex value:

swift
1import UIKit
2
3extension UIColor {
4    convenience init(rgb: UInt32, alpha: CGFloat = 1.0) {
5        let red = CGFloat((rgb >> 16) & 0xFF) / 255.0
6        let green = CGFloat((rgb >> 8) & 0xFF) / 255.0
7        let blue = CGFloat(rgb & 0xFF) / 255.0
8
9        self.init(red: red, green: green, blue: blue, alpha: alpha)
10    }
11}
12
13let brandBlue = UIColor(rgb: 0x3498DB)

This is often easier to use when your design system publishes hex colors such as #3498DB.

Using the Color in UIKit

Once you have the helper, you use the resulting UIColor like any other UIKit color:

swift
1let titleLabel = UILabel()
2titleLabel.textColor = UIColorFromRGB(red: 231, green: 76, blue: 60)
3
4let button = UIButton(type: .system)
5button.backgroundColor = UIColor(rgb: 0x2ECC71)
6button.setTitleColor(.white, for: .normal)

The helper does not create a new color type. It simply makes UIColor construction less repetitive and less error-prone.

Why People Write This Helper

Without a helper, you keep repeating code like this:

swift
UIColor(red: 52/255.0, green: 152/255.0, blue: 219/255.0, alpha: 1.0)

That is not terrible, but it clutters the call site and invites copy-paste mistakes. A dedicated helper centralizes the conversion rule and makes color creation more readable across the codebase.

It also gives you one place to add validation, hex parsing, or support for alpha defaults later.

When to Use a Helper Versus Asset Catalog Colors

For app-wide brand colors, asset catalogs are often a better long-term choice because they integrate with dark mode, high-contrast variants, and design-system maintenance. A helper such as UIColorFromRGB is still very useful when colors come from code, configuration files, API responses, or quick one-off UI work.

In other words, the helper is best for conversion logic, while named assets are best for stable semantic colors such as primaryBackground or accent.

Common Pitfalls

  • Passing raw 255-style numbers directly into UIColor(red:green:blue:alpha:) without dividing by 255.0.
  • Forgetting that UIColorFromRGB is a custom helper name, not an Apple framework function.
  • Mixing up hex order when extracting red, green, and blue bytes.
  • Using integer division accidentally in older code patterns, which can collapse values to zero.
  • Defining multiple color helpers with different conventions and making the codebase inconsistent.

Summary

  • 'UIColorFromRGB is usually a custom helper, not a built-in Swift function.'
  • Its job is to convert 0 to 255 RGB values into the normalized values UIColor expects.
  • A helper function works well for separate red, green, and blue inputs.
  • A UIColor extension is often cleaner when your colors are expressed as hex values.
  • The main thing to remember is that UIKit color components are normalized, not raw byte values.

Course illustration
Course illustration

All Rights Reserved.