Skip to content

Commit

Permalink
Move contrast ratio calculation to standalone fn
Browse files Browse the repository at this point in the history
Refactor implementations to use standalone function
  • Loading branch information
okaneco committed Jan 30, 2020
1 parent ce6cebb commit fda8324
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 77 deletions.
14 changes: 5 additions & 9 deletions palette/src/hsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::ops::{Add, AddAssign, Sub, SubAssign};
use encoding::pixel::RawPixel;
use encoding::{Linear, Srgb};
use rgb::{Rgb, RgbSpace};
use {clamp, from_f64};
use {clamp, contrast_ratio, from_f64};
use {Alpha, Hsv, RgbHue, Xyz};
use {
Component, FloatComponent, FromColor, FromF64, GetHue, Hue, IntoColor, Limited, Mix, Pixel,
Expand Down Expand Up @@ -631,15 +631,11 @@ where
{
type Scalar = T;

fn get_contrast_ratio(&self, color: &Self) -> T {
let this = self.into_luma();
let other = color.into_luma();
fn get_contrast_ratio(&self, other: &Self) -> T {
let luma1 = self.into_luma();
let luma2 = other.into_luma();

if this.luma > other.luma {
(this.luma + from_f64(0.05)) / (other.luma + from_f64(0.05))
} else {
(other.luma + from_f64(0.05)) / (this.luma + from_f64(0.05))
}
contrast_ratio(luma1.luma, luma2.luma)
}
}

Expand Down
14 changes: 5 additions & 9 deletions palette/src/hsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::ops::{Add, AddAssign, Sub, SubAssign};
use encoding::pixel::RawPixel;
use encoding::{Linear, Srgb};
use rgb::{Rgb, RgbSpace};
use {clamp, from_f64};
use {clamp, contrast_ratio, from_f64};
use {Alpha, Hsl, Hwb, RgbHue, Xyz};
use {
Component, FloatComponent, FromColor, FromF64, GetHue, Hue, IntoColor, Limited, Mix, Pixel,
Expand Down Expand Up @@ -644,15 +644,11 @@ where
{
type Scalar = T;

fn get_contrast_ratio(&self, color: &Self) -> T {
let this = self.into_luma();
let other = color.into_luma();
fn get_contrast_ratio(&self, other: &Self) -> T {
let luma1 = self.into_luma();
let luma2 = other.into_luma();

if this.luma > other.luma {
(this.luma + from_f64(0.05)) / (other.luma + from_f64(0.05))
} else {
(other.luma + from_f64(0.05)) / (this.luma + from_f64(0.05))
}
contrast_ratio(luma1.luma, luma2.luma)
}
}

Expand Down
14 changes: 5 additions & 9 deletions palette/src/hwb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::ops::{Add, AddAssign, Sub, SubAssign};
use encoding::pixel::RawPixel;
use encoding::Srgb;
use rgb::RgbSpace;
use {clamp, from_f64};
use {clamp, contrast_ratio};
use {Alpha, Hsv, RgbHue, Xyz};
use {
Component, FloatComponent, FromColor, FromF64, GetHue, Hue, IntoColor, Limited, Mix, Pixel,
Expand Down Expand Up @@ -592,15 +592,11 @@ where
{
type Scalar = T;

fn get_contrast_ratio(&self, color: &Self) -> T {
let this = self.into_luma();
let other = color.into_luma();
fn get_contrast_ratio(&self, other: &Self) -> T {
let luma1 = self.into_luma();
let luma2 = other.into_luma();

if this.luma > other.luma {
(this.luma + from_f64(0.05)) / (other.luma + from_f64(0.05))
} else {
(other.luma + from_f64(0.05)) / (this.luma + from_f64(0.05))
}
contrast_ratio(luma1.luma, luma2.luma)
}
}

Expand Down
14 changes: 5 additions & 9 deletions palette/src/lab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

use encoding::pixel::RawPixel;
use white_point::{WhitePoint, D65};
use {clamp, from_f64};
use {clamp, contrast_ratio, from_f64};
use {Alpha, LabHue, Lch, Xyz};
use {
Component, ComponentWise, FloatComponent, GetHue, IntoColor, Limited, Mix, Pixel,
Expand Down Expand Up @@ -634,15 +634,11 @@ where
{
type Scalar = T;

fn get_contrast_ratio(&self, color: &Self) -> T {
let this = self.into_luma();
let other = color.into_luma();
fn get_contrast_ratio(&self, other: &Self) -> T {
let luma1 = self.into_luma();
let luma2 = other.into_luma();

if this.luma > other.luma {
(this.luma + from_f64(0.05)) / (other.luma + from_f64(0.05))
} else {
(other.luma + from_f64(0.05)) / (this.luma + from_f64(0.05))
}
contrast_ratio(luma1.luma, luma2.luma)
}
}

Expand Down
14 changes: 5 additions & 9 deletions palette/src/lch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use color_difference::ColorDifference;
use color_difference::{get_ciede_difference, LabColorDiff};
use encoding::pixel::RawPixel;
use white_point::{WhitePoint, D65};
use {clamp, from_f64};
use {clamp, contrast_ratio, from_f64};
use {Alpha, Lab, LabHue, Xyz};
use {
Component, FloatComponent, FromColor, GetHue, Hue, IntoColor, Limited, Mix, Pixel,
Expand Down Expand Up @@ -525,15 +525,11 @@ where
{
type Scalar = T;

fn get_contrast_ratio(&self, color: &Self) -> T {
let this = self.into_luma();
let other = color.into_luma();
fn get_contrast_ratio(&self, other: &Self) -> T {
let luma1 = self.into_luma();
let luma2 = other.into_luma();

if this.luma > other.luma {
(this.luma + from_f64(0.05)) / (other.luma + from_f64(0.05))
} else {
(other.luma + from_f64(0.05)) / (this.luma + from_f64(0.05))
}
contrast_ratio(luma1.luma, luma2.luma)
}
}

Expand Down
2 changes: 1 addition & 1 deletion palette/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub use convert::{ConvertFrom, ConvertInto, FromColor, IntoColor, OutOfBounds};
pub use encoding::pixel::Pixel;
pub use hues::{LabHue, RgbHue};
pub use matrix::Mat3;
pub use relative_contrast::RelativeContrast;
pub use relative_contrast::{contrast_ratio, RelativeContrast};

//Helper macro for checking ranges and clamping.
#[cfg(test)]
Expand Down
11 changes: 5 additions & 6 deletions palette/src/luma/luma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use encoding::pixel::RawPixel;
use encoding::{Linear, Srgb, TransferFn};
use luma::LumaStandard;
use white_point::WhitePoint;
use {clamp, from_f64};
use {clamp, contrast_ratio};
use {Alpha, Xyz, Yxy};
use {
Blend, Component, ComponentWise, FloatComponent, FromColor, FromComponent, IntoColor, Limited,
Expand Down Expand Up @@ -733,11 +733,10 @@ where
type Scalar = T;

fn get_contrast_ratio(&self, other: &Self) -> T {
if self.luma > other.luma {
(self.into_linear().luma + from_f64(0.05)) / (other.into_linear().luma + from_f64(0.05))
} else {
(other.into_linear().luma + from_f64(0.05)) / (self.into_linear().luma + from_f64(0.05))
}
let luma1 = self.into_luma();
let luma2 = other.into_luma();

contrast_ratio(luma1.luma, luma2.luma)
}
}

Expand Down
26 changes: 22 additions & 4 deletions palette/src/relative_contrast.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use component::Component;
use core::ops::{Add, Div};
use {from_f64, FromF64};

/// A trait for calculating relative contrast between two colors.
Expand Down Expand Up @@ -32,10 +34,10 @@ use {from_f64, FromF64};
/// }
/// ```
///
/// The possible range of ratios is from 1:1 to 21:1. There is a Success
/// Criterion for Contrast (Minimum) and a Success Criterion for Contrast
/// (Enhanced), SC 1.4.3 and SC 1.4.6 respectively, which are concerned with
/// text and images of text. SC 1.4.11 is a Success Criterion for "non-text
/// The possible range of contrast ratios is from 1:1 to 21:1. There is a
/// Success Criterion for Contrast (Minimum) and a Success Criterion for
/// Contrast (Enhanced), SC 1.4.3 and SC 1.4.6 respectively, which are concerned
/// with text and images of text. SC 1.4.11 is a Success Criterion for "non-text
/// contrast" such as user interface components and other graphics. The relative
/// contrast is calculated by `(L1 + 0.05) / (L2 + 0.05)`, where `L1` is the
/// luminance of the brighter color and `L2` is the luminance of the darker
Expand All @@ -49,6 +51,7 @@ use {from_f64, FromF64};
/// [Success Criterion 1.4.6 Contrast (Enhanced) (Level AAA)](https://www.w3.org/WAI/WCAG21/Understanding/contrast-enhanced)
///
/// [Success Criterion 1.4.11 Non-text Contrast (Level AA)](https://www.w3.org/WAI/WCAG21/Understanding/non-text-contrast.html)

pub trait RelativeContrast {
/// The type of the contrast ratio.
type Scalar: FromF64 + PartialOrd;
Expand Down Expand Up @@ -82,6 +85,21 @@ pub trait RelativeContrast {
}
}

/// Calculates a ratio between two `luma` values.
pub fn contrast_ratio<T>(luma1: T, luma2: T) -> T
where
T: Add<Output = T>,
T: Div<Output = T>,
T: FromF64,
T: Component,
{
if luma1 > luma2 {
(luma1 + from_f64(0.05)) / (luma2 + from_f64(0.05))
} else {
(luma2 + from_f64(0.05)) / (luma1 + from_f64(0.05))
}
}

#[cfg(test)]
mod test {
use core::str::FromStr;
Expand Down
14 changes: 5 additions & 9 deletions palette/src/rgb/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use luma::LumaStandard;
use matrix::{matrix_inverse, multiply_xyz_to_rgb, rgb_to_xyz_matrix};
use rgb::{RgbSpace, RgbStandard, TransferFn};
use white_point::WhitePoint;
use {clamp, from_f64};
use {clamp, contrast_ratio, from_f64};
use {
Blend, Component, ComponentWise, FloatComponent, FromComponent, GetHue, Limited, Mix, Pixel,
RelativeContrast, Shade,
Expand Down Expand Up @@ -1048,15 +1048,11 @@ where
{
type Scalar = T;

fn get_contrast_ratio(&self, color: &Self) -> T {
let this = self.into_luma();
let other = color.into_luma();
fn get_contrast_ratio(&self, other: &Self) -> T {
let luma1 = self.into_luma();
let luma2 = other.into_luma();

if this.luma > other.luma {
(this.luma + from_f64(0.05)) / (other.luma + from_f64(0.05))
} else {
(other.luma + from_f64(0.05)) / (this.luma + from_f64(0.05))
}
contrast_ratio(luma1.luma, luma2.luma)
}
}

Expand Down
8 changes: 2 additions & 6 deletions palette/src/xyz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use luma::LumaStandard;
use matrix::{multiply_rgb_to_xyz, rgb_to_xyz_matrix};
use rgb::{Rgb, RgbSpace, RgbStandard};
use white_point::{WhitePoint, D65};
use {clamp, from_f64};
use {clamp, contrast_ratio, from_f64};
use {Alpha, Lab, Luma, Yxy};
use {Component, ComponentWise, FloatComponent, Limited, Mix, Pixel, RelativeContrast, Shade};

Expand Down Expand Up @@ -608,11 +608,7 @@ where
type Scalar = T;

fn get_contrast_ratio(&self, other: &Self) -> T {
if self.y > other.y {
(self.y + from_f64(0.05)) / (other.y + from_f64(0.05))
} else {
(other.y + from_f64(0.05)) / (self.y + from_f64(0.05))
}
contrast_ratio(self.y, other.y)
}
}

Expand Down
8 changes: 2 additions & 6 deletions palette/src/yxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use encoding::pixel::RawPixel;
use luma::LumaStandard;
use white_point::{WhitePoint, D65};
use {clamp, from_f64};
use {clamp, contrast_ratio};
use {Alpha, Luma, Xyz};
use {
Component, ComponentWise, FloatComponent, IntoColor, Limited, Mix, Pixel, RelativeContrast,
Expand Down Expand Up @@ -577,11 +577,7 @@ where
type Scalar = T;

fn get_contrast_ratio(&self, other: &Self) -> T {
if self.luma > other.luma {
(self.luma + from_f64(0.05)) / (other.luma + from_f64(0.05))
} else {
(other.luma + from_f64(0.05)) / (self.luma + from_f64(0.05))
}
contrast_ratio(self.luma, other.luma)
}
}

Expand Down

0 comments on commit fda8324

Please sign in to comment.