Contrast

object Contrast

Color science for contrast utilities.

Utility methods for calculating contrast given two colors, or calculating a color given one color and a contrast ratio.

Contrast ratio is calculated using XYZ's Y. When linearized to match human perception, Y becomes HCT's tone and Lab*'s' L*.

Functions

Link copied to clipboard
fun darker(tone: Double, ratio: Double): Double
fun darker(tone: Double, ratio: Float): Float

Returns T in HCT, L* in Lab* <= tone parameter that ensures ratio with input T/L*. Returns -1 if ratio cannot be achieved.

Link copied to clipboard
fun darkerUnsafe(tone: Double, ratio: Double): Double
fun darkerUnsafe(tone: Double, ratio: Float): Float

Tone <= tone parameter that ensures ratio. 0 if ratio cannot be achieved.

Link copied to clipboard
fun lighter(tone: Double, ratio: Double): Double
fun lighter(tone: Double, ratio: Float): Float

Returns T in HCT, L* in Lab* >= tone parameter that ensures ratio with input T/L*. Returns -1 if ratio cannot be achieved.

Link copied to clipboard
fun lighterUnsafe(tone: Double, ratio: Double): Double
fun lighterUnsafe(tone: Double, ratio: Float): Float

Tone >= tone parameter that ensures ratio. 100 if ratio cannot be achieved.

Link copied to clipboard
fun ratioOfTones(tone1: Double, tone2: Double): Double

Contrast ratio of two tones. T in HCT, L* in Lab*. Also known as luminance or perpetual luminance.

Link copied to clipboard
fun ratioOfYs(y1: Double, y2: Double): Double

Contrast ratio is a measure of legibility, its used to compare the lightness of two colors. This method is used commonly in industry due to its use by WCAG.