Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
Last active January 9, 2026 21:16
Show Gist options
  • Select an option

  • Save drewolbrich/89b4fcbf38620f29bce02c99f7e49221 to your computer and use it in GitHub Desktop.

Select an option

Save drewolbrich/89b4fcbf38620f29bce02c99f7e49221 to your computer and use it in GitHub Desktop.
Blends together two UIColors, returning a dynamic color
//
// UIColor+Blended.swift
//
// Created by Drew Olbrich on 5/16/21.
// Copyright © 2021 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//
import UIKit
extension UIColor {
/// Creates a dynamic color from the receiver blended with another color.
///
/// If `weight` is 0, the color of `self` is returned. If `weight` is 1, the color
/// of `otherColor` is returned.
///
/// This method works correctly with light and dark mode. A dynamic color is
/// returned, so if the user switches to dark mode, the dark mode versions of `self`
/// and `otherColor` will be blended together and displayed.
func blended(with otherColor: UIColor, weight: CGFloat) -> UIColor {
func blend(_ baseColor: UIColor, with otherColor: UIColor, weight: CGFloat) -> UIColor {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
baseColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
var otherRed: CGFloat = 0
var otherGreen: CGFloat = 0
var otherBlue: CGFloat = 0
var otherAlpha: CGFloat = 0
otherColor.getRed(&otherRed, green: &otherGreen, blue: &otherBlue, alpha: &otherAlpha)
func mix(_ x: CGFloat, _ y: CGFloat, _ a: CGFloat) -> CGFloat {
return (1 - a)*x + a*y
}
return UIColor(
red: mix(red, otherRed, weight),
green: mix(green, otherGreen, weight),
blue: mix(blue, otherBlue, weight),
alpha: mix(alpha, otherAlpha, weight)
)
}
return UIColor(dynamicProvider: { traitCollection in
var result: UIColor = .white
traitCollection.performAsCurrent {
result = blend(self, with: otherColor, weight: weight)
}
return result
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment