UIColor
extension UIColor
-
Contrast ratio between two colors according to W3’s WCAG 2.0: https://www.w3.org/TR/WCAG20/#contrast-ratiodef
Declaration
Swift
public func contrastRatio(to otherColor: UIColor) -> CGFloat
Parameters
otherColor
the color to calculate contrast ratio with
Return Value
the contrast ratio between the two colors ranging from 1.0 (identical) to 21.0 (the contrast between pure white and pure black)
-
Determines whether the contrast between this
UIColor
and the providedUIColor
is sufficient to meet the recommendations of W3’s WCAG 2.0.The recommendation is that the contrast ratio between text and its background should be at least 4.5 : 1 for small text and at least 3.0 : 1 for larger text.
Warning
Combining context ==.uiComponent
and level ==.AAA
will returnfalse
for all color pairings because there is no AAA contrast threshold level defined for UI Components.Declaration
Swift
public func isSufficientContrast( to otherColor: UIColor, context: WCAGContext = .normalText, level: WCAGLevel = .AA ) -> Bool
Parameters
otherColor
the other color to compute contrast with
context
the context of the color comparison (default =
.normalText
)level
the WCAG standard to test against (default =
.AA
)Return Value
true
if the two colors meet the specified WCAG minimum contrast ratio, otherwisefalse
-
Lightens the receiving color by blending it with white.
Warning
The receiving color must be addressible in the rgba color space.Declaration
Swift
func lightened(by amount: CGFloat = 0.5) -> UIColor
Parameters
amount
the amount of white to blend in, ranging from 0 (0% white) to 1 (100% white). The value is constrained to a minimum of 0 and a maximum of 1. Default = 0.5 (50% white).
Return Value
The lightened color
-
Darkens the receiving color by blending it with black.
Warning
The receiving color must be addressible in the rgba color space.Declaration
Swift
func darkened(by amount: CGFloat = 0.5) -> UIColor
Parameters
amount
the amount of black to blend in, ranging from 0 (0% black) to 1 (100% black). The value is constrained to a minimum of 0 and a maximum of 1. Default = 0.5 (50% black).
Return Value
The darkened color
-
Blends the receiving color with another color.
Warning
Both colors must be addressible in the rgba color space.Declaration
Swift
func blended(by amount: CGFloat = 0.5, with otherColor: UIColor) -> UIColor
Parameters
amount
the amount of the other color to blend in, ranging from 0 (0% other color) to 1 (100% other color). The value is constrained to a minimum of 0 and a maximum of 1. Default = 0.5 (50/50 blend of each color).
otherColor
the other color to blend with the receiving color
Return Value
The blended color
-
Create a color from a RGB hex value
Declaration
Swift
public convenience init(rgb: UInt, alpha: CGFloat = 1.0)
Parameters
rgb
RGB hex value (anything about 0xFFFFFF will be masked out and ignored)
alpha
alpha value
-
Returns the RGBA color channel components for this color. Values range from 0.0 to 1.0 in the sRGB color space.
Declaration
Swift
var rgbaComponents: RGBAComponents { get }
-
Formats a color as an RGB hexadecimal string
Declaration
Swift
func rgbDisplayString(prefix: String? = nil, isUppercase: Bool = true) -> String
Parameters
prefix
optional prefix to precede the hexadecimal value such as
0x
or#
(default = nil)isUppercase
whether the hex values should be upper or lower case
Return Value
the formatted hexadecimal string
-
Formats a color as an RGB hexadecimal string. Appropriate for debug printing.
Declaration
Swift
func rgbDebugDisplayString(prefix: String? = nil, isUppercase: Bool = true) -> String
Parameters
prefix
optional prefix to precede the hexadecimal value such as
0x
or#
(default = nil)isUppercase
whether the hex values should be upper or lower case
Return Value
the formatted hexadecimal string (with an
⚠️
for colors that fall outside of the sRGB color space)