Color
public typealias Color = NSColor
Extension that adds functionality for calculating WCAG compliant high contrast colors.
-
Calculates the color ratio for a text color on a background color.
Note
Semi-transparent text colors will be blended with the background color. However, for background colors, the alpha component is ignored.
Warning
This function will also return
nil
if any input color is not convertable to the sRGB color space.Declaration
Parameters
textColor
The text color.
backgroundColor
The background color.
Return Value
The contrast ratio for a given pair of colors.
-
Returns the text color with the highest contrast (black or white) for a given background color.
Note
Semi-transparent text colors will be blended with the background color. However, for background colors, the alpha component is ignored.
Warning
This function will also return
nil
if any input color is not convertable to the sRGB color space.Declaration
Parameters
backgroundColor
The background color.
Return Value
A color that has the highest contrast with the given background color.
-
Calculates the contrast ratio of a given list of text colors and a background color. The first color that conforms to the conformance level defined gets returned. The default conformance level is .AA.
Note
Semi-transparent text colors will be blended with the background color. However, for background colors, the alpha component is ignored.
Warning
This function will also return
nil
if any input color is not convertable to the sRGB color space.Declaration
Swift
public class func getTextColor(fromColors colors: [Color], withFont font: Font, onBackgroundColor backgroundColor: Color, conformanceLevel: ConformanceLevel = .AA) -> Color?
Parameters
colors
A list of possible text colors.
font
The font used for the text.
backgroundColor
The background color that the text should be displayed on.
conformanceLevel
The conformance level that needs to be passed when calculating the contrast ratio. The default conformance level is .AA.
Return Value
The first color that conforms to the conformance level defined or
nil
if non of the colors provided passed. -
Returns the background color with the highest contrast (black or white) for a given text color.
Note
Semi-transparent text colors will be blended with the background color. However, for background colors, the alpha component is ignored.
Warning
This function will also return
nil
if any input color is not convertable to the sRGB color space.Parameters
textColor
The textColor color.
Return Value
A color that has the highest contrast with the given text color.
-
Calculates the contrast ratio of a given list of background colors and a text color. The first color that conforms to the conformance level defined gets returned. The default conformance level is .AA.
Note
Semi-transparent text colors will be blended with the background color. However, for background colors, the alpha component is ignored.
Warning
This function will also return
nil
if any input color is not convertable to the sRGB color space.Declaration
Swift
public class func getBackgroundColor(fromColors colors: [Color], forTextColor textColor: Color, withFont font: Font, conformanceLevel: ConformanceLevel = .AA) -> Color?
Parameters
colors
A list of possible background colors.
textColor
The text color that should be used.
font
The font used for the text.
conformanceLevel
The conformance level that needs to be passed when calculating the contrast ratio. The default conformance level is .AA.
Return Value
The first color that conforms to the conformance level defined or
nil
if non of the colors provided passed.