Skip to content

Commit

Permalink
Make blackess and whiteness comply with spec.
Browse files Browse the repository at this point in the history
Changed the `blackness(..)` and `whiteness(..)` functions to return
blackness/whiteness of the integer rgb approximations of the color
rather than the exact color.
  • Loading branch information
kaj committed Jul 3, 2021
1 parent de26773 commit 4a436b3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ project adheres to

* Changed the `red(..)`, `green(..)`, and `blue(..)` functions to
always return integers. Issue #114, thanks @Keats.

* Changed the `blackness(..)` and `whiteness(..)` functions to return
blackness/whiteness of the integer rgb approximations of the color
rather than the exact color.

## Release 0.22.0 - 2021-06-20

Expand Down
10 changes: 8 additions & 2 deletions src/sass/functions/color/hwb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ use crate::{Error, Scope, ScopeRef};
pub fn register(f: &mut Scope) {
def_va!(f, hwb(kwargs), hwb);
def!(f, blackness(color), |s| {
Ok(percentage(get_color(s, "color")?.to_hwba().blackness()))
// Blackness of the rgb approximation that can be represented in css.
let (r, g, b, _a) = get_color(s, "color")?.to_rgba().to_bytes();
let max_c = *[r, g, b].iter().max().unwrap();
Ok(percentage(Rational::new((255 - max_c).into(), 255)))
});
def!(f, whiteness(color), |s| {
Ok(percentage(get_color(s, "color")?.to_hwba().whiteness()))
// Whiteness of the rgb approximation that can be represented in css.
let (r, g, b, _a) = get_color(s, "color")?.to_rgba().to_bytes();
let min_c = *[r, g, b].iter().min().unwrap();
Ok(percentage(Rational::new(min_c.into(), 255)))
});
}

Expand Down
5 changes: 0 additions & 5 deletions tests/spec/core_functions/color/blackness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ mod error {
}
}
#[test]
#[ignore] // wrong result
fn fraction() {
assert_eq!(
runner().ok("@use \'sass:color\';\
Expand All @@ -89,7 +88,6 @@ mod middle {
use super::runner;

#[test]
#[ignore] // wrong result
fn half_whiteness() {
assert_eq!(
runner().ok("@use \'sass:color\';\
Expand All @@ -100,7 +98,6 @@ mod middle {
);
}
#[test]
#[ignore] // wrong result
fn high_whiteness() {
assert_eq!(
runner().ok("@use \'sass:color\';\
Expand All @@ -111,7 +108,6 @@ mod middle {
);
}
#[test]
#[ignore] // wrong result
fn zero_whiteness() {
assert_eq!(
runner().ok("@use \'sass:color\';\
Expand All @@ -133,7 +129,6 @@ fn min() {
);
}
#[test]
#[ignore] // wrong result
fn named() {
assert_eq!(
runner().ok("@use \'sass:color\';\
Expand Down
5 changes: 0 additions & 5 deletions tests/spec/core_functions/color/whiteness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ mod error {
}
}
#[test]
#[ignore] // wrong result
fn fraction() {
assert_eq!(
runner().ok("@use \'sass:color\';\
Expand All @@ -89,7 +88,6 @@ mod middle {
use super::runner;

#[test]
#[ignore] // wrong result
fn half_blackness() {
assert_eq!(
runner().ok("@use \'sass:color\';\
Expand All @@ -100,7 +98,6 @@ mod middle {
);
}
#[test]
#[ignore] // wrong result
fn high_blackness() {
assert_eq!(
runner().ok("@use \'sass:color\';\
Expand All @@ -111,7 +108,6 @@ mod middle {
);
}
#[test]
#[ignore] // wrong result
fn zero_blackness() {
assert_eq!(
runner().ok("@use \'sass:color\';\
Expand All @@ -133,7 +129,6 @@ fn min() {
);
}
#[test]
#[ignore] // wrong result
fn named() {
assert_eq!(
runner().ok("@use \'sass:color\';\
Expand Down

0 comments on commit 4a436b3

Please sign in to comment.