Color Converter for Swift
Free online color converter with Swift code examples
Working with color converter in Swift? Our free online color converter helps Swift developers format, validate, and process data instantly. Below you will find Swift code examples using UIKit / AppKit (built-in) so you can achieve the same result programmatically in your own projects.
Try the Color Converter Online
Use our free Color Converter directly in your browser — no setup required.
Open Color ConverterSwift Code Example
import Foundation
#if canImport(UIKit)
import UIKit
typealias PlatformColor = UIColor
#elseif canImport(AppKit)
import AppKit
typealias PlatformColor = NSColor
#endif
// HEX to RGB
func hexToRGB(_ hex: String) -> (r: Int, g: Int, b: Int) {
let hex = hex.trimmingCharacters(in: CharacterSet(charactersIn: "#"))
var rgb: UInt64 = 0
Scanner(string: hex).scanHexInt64(&rgb)
return (Int((rgb >> 16) & 0xFF), Int((rgb >> 8) & 0xFF), Int(rgb & 0xFF))
}
let (r, g, b) = hexToRGB("#3498db")
print("RGB: (\(r), \(g), \(b))")
// RGB to HSB
var h: CGFloat = 0, s: CGFloat = 0, br: CGFloat = 0
let color = PlatformColor(red: CGFloat(r)/255, green: CGFloat(g)/255,
blue: CGFloat(b)/255, alpha: 1)
color.getHue(&h, saturation: &s, brightness: &br, alpha: nil)
print("HSB: (\(Int(h*360)), \(Int(s*100))%, \(Int(br*100))%)")Quick Setup
Library: UIKit / AppKit (built-in)
// Built-in framework — no installation neededSwift Tips & Best Practices
- UIColor (iOS) and NSColor (macOS) provide getHue method
- Use CGColor for cross-platform color handling
- Swift has no built-in HSL conversion — only HSB (HSV)