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:
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:
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:
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:
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 intoUIColor(red:green:blue:alpha:)without dividing by255.0. - Forgetting that
UIColorFromRGBis 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
- '
UIColorFromRGBis usually a custom helper, not a built-in Swift function.' - Its job is to convert
0to255RGB values into the normalized valuesUIColorexpects. - A helper function works well for separate red, green, and blue inputs.
- A
UIColorextension 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.

